tokio/loom/std/
atomic_u16.rs

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