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 @@ -286,13 +286,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
26 changes: 8 additions & 18 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,10 +1688,13 @@ impl<T, A: Allocator> Vec<T, A> {

/// Converts the vector into [`Box<[T]>`][owned slice].
///
/// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
/// 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.
///
/// [owned slice]: Box
/// [`shrink_to_fit`]: Vec::shrink_to_fit
/// [*fits*]: crate::alloc::Allocator#memory-fitting
///
/// # Examples
///
Expand All @@ -1701,7 +1704,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 @@ -1725,7 +1728,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 @@ -4543,7 +4546,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 @@ -4560,30 +4562,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.
///
/// Before doing the conversion, this method discards excess capacity like [`Vec::shrink_to_fit`].
///
/// [owned slice]: Box
/// [`Vec::shrink_to_fit`]: Vec::shrink_to_fit
/// 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