File: atomic_ref.rs

package info (click to toggle)
rustc 1.63.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 445,532 kB
  • sloc: xml: 147,972; javascript: 9,201; sh: 8,612; python: 6,901; ansic: 5,674; cpp: 4,961; makefile: 3,611; asm: 1,438; ruby: 68
file content (26 lines) | stat: -rw-r--r-- 970 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::marker::PhantomData;
use std::sync::atomic::{AtomicPtr, Ordering};

/// This is essentially an `AtomicPtr` but is guaranteed to always be valid
pub struct AtomicRef<T: 'static>(AtomicPtr<T>, PhantomData<&'static T>);

impl<T: 'static> AtomicRef<T> {
    pub const fn new(initial: &'static T) -> AtomicRef<T> {
        AtomicRef(AtomicPtr::new(initial as *const T as *mut T), PhantomData)
    }

    pub fn swap(&self, new: &'static T) -> &'static T {
        // We never allow storing anything but a `'static` reference so it's safe to
        // return it for the same.
        unsafe { &*self.0.swap(new as *const T as *mut T, Ordering::SeqCst) }
    }
}

impl<T: 'static> std::ops::Deref for AtomicRef<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        // We never allow storing anything but a `'static` reference so it's safe to lend
        // it out for any amount of time.
        unsafe { &*self.0.load(Ordering::SeqCst) }
    }
}