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
use std::cell::{Cell, UnsafeCell};
/// A `Cell` wrapper that implements `Sync`.
pub(crate) struct SyncCell<T> {
t: Cell<T>,
}
// SAFETY: All `&self` methods require exclusive access.
unsafe impl<T: Send> Sync for SyncCell<T> {}
impl<T> SyncCell<T> {
pub(crate) fn new(t: T) -> Self {
Self { t: Cell::new(t) }
}
/// # Safety
///
/// The caller must have exclusive access to self.
pub(crate) unsafe fn replace(&self, value: T) -> T {
self.t.replace(value)
}
/// # Safety
///
/// The caller must have exclusive access to self.
pub(crate) unsafe fn set(&self, value: T) {
self.t.set(value);
}
/// # Safety
///
/// The caller must have exclusive access to self.
pub(crate) unsafe fn get(&self) -> T
where
T: Copy,
{
self.t.get()
}
}
/// An `UnsafeCell` wrapper that implements `Sync`.
pub(crate) struct SyncUnsafeCell<T> {
t: UnsafeCell<T>,
}
// SAFETY: SyncUnsafeCell does not grant safe access to T.
unsafe impl<T> Sync for SyncUnsafeCell<T> {}
impl<T> SyncUnsafeCell<T> {
pub(crate) fn new(t: T) -> Self {
Self {
t: UnsafeCell::new(t),
}
}
pub(crate) fn get(&self) -> *mut T {
self.t.get()
}
}