External Publication
Visit Post

Impact of Static Variables on API Soundness

Rust Internals [Unofficial] February 26, 2026
Source

Also, instead of marking your current API functions as unsafe, what you can do is mark whatever new functions you add that could break the invariants as unsafe, e.g.:

static ORIGINAL: u32 = 0;
static PTR: AtomicPtr<u32> = AtomicPtr::new(&raw const ORIGINAL as *mut u32);

pub fn existing_function() -> u32 {
    // Safety: PTR is known to contain a valid pointer and we used Acquire/Release correctly
    unsafe {
        *PTR.load(Ordering::Acquire)
    }
}

/// Safety: you must not pass in an invalid `v`
pub unsafe fn new_function(v: *const u32) {
    PTR.store(v as *mut u32, Ordering::Release);
}

Discussion in the ATmosphere

Loading comments...