Skip to content

Commit b1d5313

Browse files
authored
Rollup merge of rust-lang#61244 - RalfJung:box, r=rkruppe
Box::into_vec: use Box::into_raw instead of mem::forget `Box::into_raw` does, in one step, turn the `Box` into a raw ptr and avoid deallocation. Seems cleaner than separating the two. Also, `mem::forget` gets the `Box` with a `noalias` argument, but it is not actually correct that this is an exclusive pointer. So a stricter version of Stacked Borrows would complain here. (I can't actually make Stacked Borrows that strict yet though due to other issues.)
2 parents 89c83e3 + 645f685 commit b1d5313

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/liballoc/slice.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,16 @@ pub use hack::to_vec;
137137
// `core::slice::SliceExt` - we need to supply these functions for the
138138
// `test_permutations` test
139139
mod hack {
140-
use core::mem;
141-
142140
use crate::boxed::Box;
143141
use crate::vec::Vec;
144142
#[cfg(test)]
145143
use crate::string::ToString;
146144

147-
pub fn into_vec<T>(mut b: Box<[T]>) -> Vec<T> {
145+
pub fn into_vec<T>(b: Box<[T]>) -> Vec<T> {
148146
unsafe {
149-
let xs = Vec::from_raw_parts(b.as_mut_ptr(), b.len(), b.len());
150-
mem::forget(b);
147+
let len = b.len();
148+
let b = Box::into_raw(b);
149+
let xs = Vec::from_raw_parts(b as *mut T, len, len);
151150
xs
152151
}
153152
}

0 commit comments

Comments
 (0)