diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index e208c2384a16d..bcd0dc6eea29f 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -283,13 +283,12 @@ 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. + /// For allocation behavior, see [`Vec::into_boxed_slice`]. /// /// # Errors /// /// Returns the original `Vec` in the `Err` variant if - /// `boxed_slice.len()` does not equal `N`. + /// `vec.len()` does not equal `N`. /// /// # Examples /// diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 99bdeea251bf4..6700e391050b1 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1619,8 +1619,14 @@ impl Vec { /// 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 /// @@ -1630,7 +1636,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); @@ -1654,7 +1660,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 @@ -4703,7 +4709,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 @@ -4720,28 +4725,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. /// - /// 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) -> Self { v.into_boxed_slice() }