blob: 707f41364975497948b3d377b3c48beb76604f1e (
plain)
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
|
//! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`.
#[cfg(loom)]
use loom::cell::UnsafeCell as InnerUnsafeCell;
#[cfg(loom)]
pub use loom::cell::MutPtr;
#[cfg(not(loom))]
use core::cell::UnsafeCell as InnerUnsafeCell;
/// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with
/// loom's UnsafeCell.
#[derive(Debug)]
pub struct UnsafeCell<T>(InnerUnsafeCell<T>);
impl<T> UnsafeCell<T> {
/// Create a new `UnsafeCell`.
#[cfg(not(loom))]
pub const fn new(data: T) -> UnsafeCell<T> {
UnsafeCell(InnerUnsafeCell::new(data))
}
#[cfg(loom)]
pub fn new(data: T) -> UnsafeCell<T> {
UnsafeCell(InnerUnsafeCell::new(data))
}
/// Access the contents of the `UnsafeCell` through a tracked mut pointer.
pub fn get_mut(&self) -> MutPtr<T> {
#[cfg(loom)]
return self.0.get_mut();
#[cfg(not(loom))]
return MutPtr(self.0.get());
}
/// Access the contents of the `UnsafeCell` mutably.
pub fn as_mut(&mut self) -> &mut T {
#[cfg(not(loom))]
return self.0.get_mut();
#[cfg(loom)]
{
// SAFETY: we have exclusive access to `self`.
let ptr = self.get_mut();
let ptr = unsafe { ptr.deref() };
// SAFETY: we have exclusive access to `self` for the duration of
// the borrow.
unsafe { core::mem::transmute(ptr) }
}
}
}
#[cfg(not(loom))]
pub struct MutPtr<T>(*mut T);
#[cfg(not(loom))]
impl<T> MutPtr<T> {
#[allow(clippy::mut_from_ref)]
/// SAFETY: the caller must guarantee that the contained `*mut T` is not
/// null, and must uphold the same safety requirements as for
/// [`core::primitive::pointer::as_mut`] for the contained `*mut T`.
pub unsafe fn deref(&self) -> &mut T {
&mut *self.0
}
}
|