Alloc crate guarantees

#1

What are the guarantees for interoperability for different functionality within the alloc crate? Is this code valid forever?

fn free<'a, T: ?Sized + 'a>(val: Box<T>) {
    use std::{alloc, mem, ptr};

    unsafe {
        let p = Box::into_raw(val);
        let layout = alloc::Layout::for_value(&*p);
        ptr::drop_in_place(p);
        alloc::dealloc(p as _, layout);
    }
}
#2

I think this has come up before, but I can’t find the discussion, unfortunately.

I think we should guarantee that Box does “the obvious thing” to allocate. In particular, your code sample should work, as should code that manually allocates and initializes a value via the appropriate allocator and constructs a Box from it.

1 Like
#3

PR: https://github.com/rust-lang/rust/pull/58183