Constify atomics

I think it would be useful for the Atomic* to become completely const fn. const-eval is assumed to behave in a single-threaded manner, such that all values are assumed to only be accessible by a single thread or that the values (like shared references in const items) are explicitly prohibited from holding shared values containing UnsafeCell (which includes all atomic types).

This would allow code to use Arc in const fn once allocations work, as well as allowing code like:

pub struct MyCounter(AtomicU32);

impl MyCounter {
    pub const fn new() -> Self {
        MyCounter(AtomicU32::new())
    }
    pub const fn inc(&self) {
        self.0.fetch_add(1, Ordering::AcqRel);
    }
}

pub const I_HAVE_A_ONE: MyCounter = {
    let v = MyCounter::new();
    v.inc();
    v
};

This came up here: RFC: Alignment niches for references types. by moulins · Pull Request #3204 · rust-lang/rfcs · GitHub

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.