# Constify atomics

**URL:** <https://internals.rust-lang.org/t/constify-atomics/16192>\
**Category:** libs\
**Created:** [February 21, 2022, 8:31pm UTC](https://internals.rust-lang.org/t/constify-atomics/16192 "2022-02-21T20:31:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)\
**Post date:** [February 21, 2022, 8:31pm UTC](https://internals.rust-lang.org/t/constify-atomics/16192/1 "2022-02-21T20:31:32Z")

</div>

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:

```rust
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](https://github.com/rust-lang/rfcs/pull/3204#discussion_r811404364)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 22, 2022, 8:32pm UTC](https://internals.rust-lang.org/t/constify-atomics/16192/2 "2022-05-22T20:32:07Z")

</div>

Closed on May 22, 2022, 8:32pm UTC

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