Skip to content

Does Box contain any UnsafeCells? #492

@joshlf

Description

@joshlf

Obviously it doesn't in practice, but is this guaranteed? In particular, is the following code guaranteed to be sound?

fn read_box_as_raw_ptr<T: Sized>(b: &Box<T>) -> *const T {
    let b_ptr: *const Box<T> = b;
    let ptr_ptr = b_ptr.cast::<*const T>();
    unsafe { core::ptr::read(ptr_ptr) }
}

In particular, this would be unsound if Box contained any UnsafeCells because it would run afoul of aliasing rules. The std::boxed docs guarantee that, for T: Sized, Box<T> is ABI-compatible with *const T, but this could in principle just be talking about value transmutations, which don't technically rule out UnsafeCell.

Specifically, my question is: Is it consistent to read the std::boxed docs as logically implying that Box does not contain any UnsafeCells? If not, I'll put up a PR 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions