Skip to content

Can a pointer obtained by casting &UnsafeCell<T> to *mut T be written to? #281

Description

@Kestrer

UnsafeCell::get is currently implemented by simply casting the &UnsafeCell<T> to a *mut T. A comment inside it notes that this is only able to work because libcore is a special library. Should this comment still apply? Is there benefit in forcing all writes to an UnsafeCell to go through UnsafeCell::get, or should UnsafeCell be treated as just a type-level indicator that the inner data is interior mutable? Since UnsafeCell is #[repr(transparent)] I don't think it is UB to read from a casted pointer, but I'm not sure if the same logic applies to writes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions