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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use super::UnsafeCell;

/// A checked version of [`std::cell::Cell`], implemented on top of
/// [`loom::cell::UnsafeCell`][unsafecell].
///
/// Unlike [`loom::cell::UnsafeCell`][unsafecell], this provides an API that's
/// largely compatible with the standard counterpart.
///
/// [unsafecell]: crate::cell::UnsafeCell
#[derive(Debug)]
pub struct Cell<T> {
    cell: UnsafeCell<T>,
}

// unsafe impl<T> Send for Cell<T> where T: Send {}

impl<T> Cell<T> {
    /// Creates a new instance of `Cell` wrapping the given value.
    #[track_caller]
    pub fn new(v: T) -> Self {
        Self {
            cell: UnsafeCell::new(v),
        }
    }

    /// Sets the contained value.
    #[track_caller]
    pub fn set(&self, val: T) {
        let old = self.replace(val);
        drop(old);
    }

    /// Swaps the values of two Cells.
    #[track_caller]
    pub fn swap(&self, other: &Self) {
        if core::ptr::eq(self, other) {
            return;
        }
        self.cell.with_mut(|my_ptr| {
            other.cell.with_mut(|their_ptr| unsafe {
                core::ptr::swap(my_ptr, their_ptr);
            })
        })
    }

    /// Replaces the contained value, and returns it.
    #[track_caller]
    pub fn replace(&self, val: T) -> T {
        self.cell
            .with_mut(|ptr| unsafe { core::mem::replace(&mut *ptr, val) })
    }

    /// Returns a copy of the contained value.
    #[track_caller]
    pub fn get(&self) -> T
    where
        T: Copy,
    {
        self.cell.with(|ptr| unsafe { *ptr })
    }

    /// Takes the value of the cell, leaving `Default::default()` in its place.
    #[track_caller]
    pub fn take(&self) -> T
    where
        T: Default,
    {
        self.replace(T::default())
    }

    /// Unwraps the value, consuming the cell.
    #[track_caller]
    pub fn into_inner(self) -> T {
        self.cell.into_inner()
    }
}

impl<T: Default> Default for Cell<T> {
    #[track_caller]
    fn default() -> Cell<T> {
        Cell::new(T::default())
    }
}

impl<T: Copy> Clone for Cell<T> {
    #[track_caller]
    fn clone(&self) -> Cell<T> {
        Cell::new(self.get())
    }
}

impl<T> From<T> for Cell<T> {
    #[track_caller]
    fn from(src: T) -> Cell<T> {
        Cell::new(src)
    }
}

impl<T: PartialEq + Copy> PartialEq for Cell<T> {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}

impl<T: Eq + Copy> Eq for Cell<T> {}

impl<T: PartialOrd + Copy> PartialOrd for Cell<T> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.get().partial_cmp(&other.get())
    }
}

impl<T: Ord + Copy> Ord for Cell<T> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.get().cmp(&other.get())
    }
}