Stream: t-lang/wg-unsafe-code-guidelines

Topic: Should `Box` really behave like `&mut`?


RalfJ (May 27 2019 at 17:14, on Zulip):

Box asserting uniqueness like &mut does has some very annoying consequences:

    pub fn into_vec<T>(mut b: Box<[T]>) -> Vec<T> {
        unsafe {
            let xs = Vec::from_raw_parts(b.as_mut_ptr(), b.len(), b.len());
            mem::forget(b);
            xs
        }
    }

This is now incorrect because mem::forget(b) is a "reborrow"-like use of b and thus asserts uniqueness, breaking the raw pointer that was created just before with as_mut_ptr :/

RalfJ (May 27 2019 at 17:15, on Zulip):

(and not doing the "reborrow" wouldn't help, we mark Box as noalias and as such we promise to mem::forget that this is a unique pointer...

RalfJ (May 27 2019 at 17:24, on Zulip):

(the fix is to use Box::into_raw)

Last update: Nov 19 2019 at 18:55UTC