From 2b7e7cd0387c6d5dd2782d00cce29981f8c06cd7 Mon Sep 17 00:00:00 2001 From: Walentalien Date: Tue, 6 Oct 2026 16:28:25 +0200 Subject: [PATCH 1/4] docs(vec): clarify into_boxed_slice allocation guarantee --- library/alloc/src/vec/mod.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 8bcad62226a0b..06cb03d62de38 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1688,10 +1688,13 @@ impl Vec { /// 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 /// @@ -1701,7 +1704,7 @@ impl Vec { /// 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); @@ -1725,7 +1728,7 @@ impl Vec { } /// 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 @@ -4543,7 +4546,6 @@ where } } -// note: test pulls in std, which causes errors here #[stable(feature = "vec_from_box", since = "1.18.0")] impl From> for Vec { /// Converts a boxed slice into a vector by transferring ownership of @@ -4560,30 +4562,18 @@ impl From> for Vec { } } -// 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 From> 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) -> Self { v.into_boxed_slice() } From 2a0e4503b94bc5c9f574ff87cc0efbf731dbf82b Mon Sep 17 00:00:00 2001 From: Walentalien Date: Tue, 6 Oct 2026 18:32:33 +0200 Subject: [PATCH 2/4] clarify allocation guarantee Signed-off-by: Walentalien --- library/alloc/src/boxed/convert.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index 526e8b18a8031..d269e438bb8ef 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -286,13 +286,14 @@ impl TryFrom> for Box<[T; N], A> { /// Attempts to convert a `Vec` into a `Box<[T; N]>`. /// - /// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N`, - /// but will require a reallocation otherwise. + /// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N` + /// and `vec.len() == N`. If the `vec.len() == N` but there is spare capacity, + /// the allocation will be either shrunk or reallocated. /// /// # Errors /// /// Returns the original `Vec` in the `Err` variant if - /// `boxed_slice.len()` does not equal `N`. + /// `vec.len()` does not equal `N`. /// /// # Examples /// From cc9755d08b585fc05c176cb5dce982fb847dd631 Mon Sep 17 00:00:00 2001 From: Walentalien Date: Sat, 10 Oct 2026 16:24:46 +0200 Subject: [PATCH 3/4] add further clarification of allocation guarantee Signed-off-by: Walentalien --- library/alloc/src/boxed/convert.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index d269e438bb8ef..a58d5cf22af3c 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -288,7 +288,8 @@ impl TryFrom> for Box<[T; N], A> { /// /// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N` /// and `vec.len() == N`. If the `vec.len() == N` but there is spare capacity, - /// the allocation will be either shrunk or reallocated. + /// the allocation will be either shrunk or reallocated, or deallocated if `N==0`. + /// Vectors of zero-sized types have no backing allocation to modify. /// /// # Errors /// From 84b5d1bf126b11ad9c6488aa209fdcc68ce9ab23 Mon Sep 17 00:00:00 2001 From: "user.email" Date: Sun, 25 Oct 2026 21:08:24 +0100 Subject: [PATCH 4/4] remove clarification of allocation guarantee Signed-off-by: user.email --- library/alloc/src/boxed/convert.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index a58d5cf22af3c..325ddd35d06b8 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -286,10 +286,7 @@ impl TryFrom> for Box<[T; N], A> { /// Attempts to convert a `Vec` into a `Box<[T; N]>`. /// - /// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N` - /// and `vec.len() == N`. If the `vec.len() == N` but there is spare capacity, - /// the allocation will be either shrunk or reallocated, or deallocated if `N==0`. - /// Vectors of zero-sized types have no backing allocation to modify. + /// For allocation behavior, see [`Vec::into_boxed_slice`]. /// /// # Errors ///