Skip to content

Avoid allocations and copying in Vec::leak #89337

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

Merged
merged 4 commits into from
Oct 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid allocations and copying in Vec::leak
Don't shrink the Vec (by calling into_boxed_slice) before leaking it.
  • Loading branch information
mbrubeck committed Sep 28, 2021
commit 58b1a127d66b71e1a2180b509856f2c52feca9b3
3 changes: 2 additions & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,8 @@ impl<T, A: Allocator> Vec<T, A> {
where
A: 'a,
{
Box::leak(self.into_boxed_slice())
let mut me = ManuallyDrop::new(self);
unsafe { slice::from_raw_parts_mut(me.as_mut_ptr(), me.len) }
}

/// Returns the remaining spare capacity of the vector as a slice of
Expand Down