Alloc crate guarantees


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);
        alloc::dealloc(p as _, layout);

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