Skip to content

docs(vec): clarify into_boxed_slice allocation guarantee - #163882

Open
Walentalien wants to merge 3 commits into
rust-lang:mainfrom
Walentalien:fix-vec-into-boxed-slice-docs
Open

Walentalien wants to merge 3 commits into
rust-lang:mainfrom
Walentalien:fix-vec-into-boxed-slice-docs

Conversation

@Walentalien

@Walentalien Walentalien commented Oct 6, 2026 •

Copy link
Copy Markdown

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.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 6, 2026
@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

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.

@Walentalien Walentalien changed the title docs(vec): clarify into_boxed_slice allocation quarantee docs(vec): clarify into_boxed_slice allocation guarantee Oct 6, 2026
@Walentalien
Walentalien force-pushed the fix-vec-into-boxed-slice-docs branch from 308f488 to b457180 Compare October 6, 2026 15:31
@Walentalien
Walentalien force-pushed the fix-vec-into-boxed-slice-docs branch from b457180 to 2b7e7cd Compare October 6, 2026 15:41
@Walentalien
Walentalien marked this pull request as ready for review October 6, 2026 16:30
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 6, 2026
Signed-off-by: Walentalien <voliinyk@student.agh.edu.pl>
Comment thread library/alloc/src/boxed/convert.rs Outdated
/// 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.

@xizheyin xizheyin Oct 9, 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.

If the vector is a vector of (), it won't shrink or reallocate.

View changes since the review

@xizheyin xizheyin Oct 9, 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.

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 `()`.

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.

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vec::into_boxed_slice no-excess guarantee regressed in 1.77.0

4 participants