Repository navigation
docs(vec): clarify into_boxed_slice allocation guarantee #163882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
2b7e7cd
2a0e450
cc9755d
84b5d1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| /// | ||
|
|
@@ -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); | ||
|
|
@@ -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 | ||
|
|
@@ -4543,7 +4546,6 @@ where | |
| } | ||
| } | ||
|
|
||
| // note: test pulls in std, which causes errors here | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these comments deleted?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those comments used to explain the 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 | ||
|
|
@@ -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() | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.