External Publication
Visit Post

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

Rust Internals [Unofficial] May 31, 2026
Source

RalfJung:

FWIW, I think the reason that "seqlock with inline asm for the data reads and writes" works in practice is that there is another story, one that actually works in my framework (assuming a language extension to Rust that we'll hopefully get one day): consider those accesses to be relaxed bytewise atomic.

newpavlov:

We could move the validity check into the assembly block, but honestly it feels like an unnecessary (and error-prone) busy-work to create a purer "story".

When the check is moved inside, then there is effectively equivalent Rust code which explains what happens:

use std::hint::black_box;
use std::sync::RwLock;

struct SeqlockStory<T>(RwLock<T>);

impl<T: Clone + Default> SeqlockStory<T> {
    fn load(&self) -> T {
        if let Ok(v) = self.0.try_read() { return v.clone(); }
        return black_box(T::default());  // outer code can't rely on
                                         // the exact returned value
    }
    fn store(&self, value: T) {
        *self.0.write().unwrap() = value;
    }
}

Discussion in the ATmosphere

Loading comments...