Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions library/alloc/src/boxed/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,12 @@ impl<T, const N: usize, A: Allocator> TryFrom<Vec<T, A>> for Box<[T; N], A> {

/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
///
/// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N`,
/// but will require a reallocation otherwise.
/// For allocation behavior, see [`Vec::into_boxed_slice`].
///
/// # Errors
///
/// Returns the original `Vec<T>` in the `Err` variant if
/// `boxed_slice.len()` does not equal `N`.
/// `vec.len()` does not equal `N`.
///
/// # Examples
///
Expand Down
27 changes: 11 additions & 16 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,8 +1619,14 @@ impl<T, A: Allocator> Vec<T, A> {

/// Converts the vector into a boxed slice.
///
/// This conversion is in-place if `vec.len() == vec.capacity()`. Otherwise excess capacity
/// is discarded by reallocating before doing the conversion.
/// This conversion is in-place if `vec.len() == vec.capacity()`.
///
/// Before doing the conversion, this method adjusts the vector's allocation as
/// needed so that, if a backing allocation is present, the layout of the returned
/// slice [*fits*] that allocation. Thus, the vector's former capacity is not needed
/// to determine a layout suitable for deallocation.
///
/// [*fits*]: crate::alloc::Allocator#memory-fitting
///
/// # Examples
///
Expand All @@ -1630,7 +1636,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// let slice = v.into_boxed_slice();
/// ```
///
/// Any excess capacity is removed:
/// The vector's spare capacity is not retained:
///
/// ```
/// let mut vec = Vec::with_capacity(10);
Expand All @@ -1654,7 +1660,7 @@ impl<T, A: Allocator> Vec<T, A> {
}

/// Converts the Vec into a boxed array. This conversion will discard any spare capacity,
/// if there is any, see [`Vec::shrink_to_fit`].
/// if there is any, see [`Vec::into_boxed_slice`].
/// If you merely wish for a reference to an array, use [`as_array`](https://doc.rust-lang.org/stable/std/primitive.slice.html#method.as_array).
///
/// # Errors
Expand Down Expand Up @@ -4703,7 +4709,6 @@ where
}
}

// note: test pulls in std, which causes errors here

@Mark-Simulacrum Mark-Simulacrum Oct 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these comments deleted?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those comments used to explain the #[cfg(not(test))] attribute here.

But it's no longer there since this refactor

This is cleanup after refactor.

#[stable(feature = "vec_from_box", since = "1.18.0")]
impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
/// Converts a boxed slice into a vector by transferring ownership of
Expand All @@ -4720,28 +4725,18 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
}
}

// note: test pulls in std, which causes errors here
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_from_vec", since = "1.20.0")]
impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> {
/// Converts a vector into a boxed slice.
///
/// This conversion is in-place if `vec.len() == vec.capacity()`. Otherwise, excess capacity
/// is discarded by reallocating before doing the conversion.
/// This is equivalent to [`Vec::into_boxed_slice`].
///
/// # Examples
///
/// ```
/// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
/// ```
///
/// Any excess capacity is removed:
/// ```
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3]);
///
/// assert_eq!(Box::from(vec), vec![1, 2, 3].into_boxed_slice());
/// ```
fn from(v: Vec<T, A>) -> Self {
v.into_boxed_slice()
}
Expand Down
Loading