Repository navigation
docs(vec): clarify into_boxed_slice allocation guarantee - #163882
Walentalien wants to merge 3 commits into
Conversation
|
Thanks for the pull request, and welcome! The Rust Project has assigned @Mark-Simulacrum (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks. Please see the contribution instructions and our LLM policy for more information. |
308f488 to
b457180
Compare
b457180 to
2b7e7cd
Compare
Signed-off-by: Walentalien <voliinyk@student.agh.edu.pl>
| /// 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. |
There was a problem hiding this comment.
If the vector is a vector of (), it won't shrink or reallocate.
There was a problem hiding this comment.
would be more rigor?
If `vec.len() == N` but there is remaining capacity, the allocated memory will be shrunk or reallocated, unless the type within the `vec` is `()`.
There was a problem hiding this comment.
Agreed. But even more rigor would me including all zero-size types in comment not just unit type ().
Also In case when len is zero but capacity is allocated and we convert into Boxed vec deallocation of allocated capacity will happen. Consider:
let vec: Vec<u8> = Vec::with_capacity(10);
let boxed: Box<[u8; 0]> = vec.try_into().unwrap();So new comment will be:
/// 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.
Signed-off-by: Walentalien <voliinyk@student.agh.edu.pl>
LLM usage disclosure:
LLMs used for research and review.
Fixes #125941
r? @Mark-Simulacrum
@Mark-Simulacrum reviewed #137546 but that PR was closed due to inactivity, that review was taken into account while working on this contribution.