1use std::cell::UnsafeCell;
2use std::fmt;
3use std::ops::Deref;
4
5/// `AtomicU32` providing an additional `unsync_load` function.
6pub(crate) struct AtomicU32 {
7 inner: UnsafeCell<std::sync::atomic::AtomicU32>,
8}
9
10unsafe impl Send for AtomicU32 {}
11unsafe impl Sync for AtomicU32 {}
12
13impl AtomicU32 {
14 pub(crate) const fn new(val: u32) -> AtomicU32 {
15 let inner: UnsafeCell = UnsafeCell::new(std::sync::atomic::AtomicU32::new(val));
16 AtomicU32 { inner }
17 }
18
19 /// Performs an unsynchronized load.
20 ///
21 /// # Safety
22 ///
23 /// All mutations must have happened before the unsynchronized load.
24 /// Additionally, there must be no concurrent mutations.
25 pub(crate) unsafe fn unsync_load(&self) -> u32 {
26 core::ptr::read(self.inner.get() as *const u32)
27 }
28}
29
30impl Deref for AtomicU32 {
31 type Target = std::sync::atomic::AtomicU32;
32
33 fn deref(&self) -> &Self::Target {
34 // safety: it is always safe to access `&self` fns on the inner value as
35 // we never perform unsafe mutations.
36 unsafe { &*self.inner.get() }
37 }
38}
39
40impl fmt::Debug for AtomicU32 {
41 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
42 self.deref().fmt(fmt)
43 }
44}
45