Skip to content

Commit 58b1a12

Browse files
committed
Avoid allocations and copying in Vec::leak
Don't shrink the Vec (by calling into_boxed_slice) before leaking it.
1 parent 8f8092c commit 58b1a12

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

library/alloc/src/vec/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,8 @@ impl<T, A: Allocator> Vec<T, A> {
19761976
where
19771977
A: 'a,
19781978
{
1979-
Box::leak(self.into_boxed_slice())
1979+
let mut me = ManuallyDrop::new(self);
1980+
unsafe { slice::from_raw_parts_mut(me.as_mut_ptr(), me.len) }
19801981
}
19811982

19821983
/// Returns the remaining spare capacity of the vector as a slice of

0 commit comments

Comments
 (0)