External Publication
Visit Post

Include racy reads in Rust memory model with `MaybeInvalid<T>`

Rust Internals [Unofficial] May 9, 2026
Source

Writing this post gave me the idea of replacing AtomicPerByte for RFC 3301 by

struct AtomicCell<T>(UnsafeCell<T>);

unsafe impl<T: Send> Sync for AtomicCell<T> {}

impl<T> AtomicCell<T> {
    pub const fn new(value: T) -> Self { Self(UnsafeCell::new(value)) }
    /// Safety
    ///
    /// Calls to `store` must be serialized,
    /// i.e. two thread shall not call `store` concurrently.
    pub unsafe fn store(&self, value: T, ordering: Ordering) { /* .. */ }
    pub fn load(&self, ordering: Ordering) -> MaybeUninit<T> { /* .. */ }
    pub const fn get_mut(&mut self) -> &mut T { self.0.get_mut() }
    pub const fn as_ptr(&self) -> *mut T { self.0.get() }
}

It keeps the biggest advantage of the MaybeInvalid proposal, i.e. no torn writes, i.e. no drop issue, while not requiring the memory model to be modified. The proposal is written in a comment to RFC 3301.

So now my issues about AtomicPerByte are solved with AtomicCell, I have less interest into this MaybeInvalid proposal.

Discussion in the ATmosphere

Loading comments...