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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
pub mod atomic {
pub use sealed::AtomicPtrNull;
#[cfg(not(all(loom, test)))]
pub use core::sync::atomic::{fence, AtomicBool, AtomicPtr};
#[cfg(all(loom, test))]
pub use loom::sync::atomic::{fence, AtomicBool, AtomicPtr};
impl<T> AtomicPtrNull for AtomicPtr<T> {
type Target = T;
#[rustfmt::skip]
#[cfg(not(all(loom, test)))]
const NULL_MUT: AtomicPtr<Self::Target> = {
Self::new(core::ptr::null_mut())
};
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
fn null_mut() -> AtomicPtr<Self::Target> {
Self::new(core::ptr::null_mut())
}
}
mod sealed {
use super::AtomicPtr;
/// A trait that extends [`AtomicPtr`] to allow creating `null` values.
pub trait AtomicPtrNull {
/// The type of the data pointed to.
type Target;
/// A compiler time evaluable [`AtomicPtr`] poiting to `null`.
#[cfg(not(all(loom, test)))]
#[allow(clippy::declare_interior_mutable_const)]
const NULL_MUT: AtomicPtr<Self::Target>;
/// Returns a Loom based [`AtomicPtr`] poiting to `null` (non-const).
#[cfg(all(loom, test))]
fn null_mut() -> AtomicPtr<Self::Target>;
}
}
}
pub mod cell {
pub use sealed::{UnsafeCellOptionWith, UnsafeCellWith};
#[cfg(not(all(loom, test)))]
pub use core::cell::UnsafeCell;
#[cfg(all(loom, test))]
pub use loom::cell::UnsafeCell;
impl<T: ?Sized> UnsafeCellWith for UnsafeCell<T> {
type Target = T;
#[cfg(not(all(loom, test)))]
unsafe fn with_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&Self::Target) -> Ret,
{
// SAFETY: Caller guaranteed that there are no mutable aliases.
f(unsafe { &*self.get() })
}
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
unsafe fn with_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&Self::Target) -> Ret,
{
// SAFETY: Caller guaranteed that there are no mutable aliases.
self.with(|ptr| f(unsafe { &*ptr }))
}
#[cfg(not(all(loom, test)))]
unsafe fn with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&mut Self::Target) -> Ret,
{
// SAFETY: Caller guaranteed that there are no mutable aliases.
f(unsafe { &mut *self.get() })
}
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
unsafe fn with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&mut Self::Target) -> Ret,
{
// SAFETY: Caller guaranteed that there are no mutable aliases.
self.with_mut(|ptr| f(unsafe { &mut *ptr }))
}
}
impl<T: ?Sized> UnsafeCellOptionWith for Option<&UnsafeCell<T>> {
type Target = T;
#[cfg(not(all(loom, test)))]
unsafe fn as_deref_with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(Option<&mut T>) -> Ret,
{
let ptr = self.map(UnsafeCell::get);
// SAFETY: Caller guaranteed that there are no mutable aliases.
f(ptr.map(|ptr| unsafe { &mut *ptr }))
}
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
unsafe fn as_deref_with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(Option<&mut T>) -> Ret,
{
let ptr = self.map(UnsafeCell::get_mut);
// SAFETY: Caller guaranteed that there are no mutable aliases.
f(ptr.as_ref().map(|ptr| unsafe { ptr.deref() }))
}
}
mod sealed {
/// A trait that extends [`UnsafeCell`] to allow running closures against
/// its underlying data.
pub trait UnsafeCellWith {
/// The type of the underlying data.
type Target: ?Sized;
/// Runs `f` against a shared reference borrowed from a [`UnsafeCell`].
///
/// # Safety
///
/// Caller must guarantee there are no mutable aliases to the
/// underlying data.
unsafe fn with_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&Self::Target) -> Ret;
/// Runs `f` against a mutable reference borrowed from a [`UnsafeCell`].
///
/// # Safety
///
/// Caller must guarantee there are no mutable aliases to the
/// underlying data.
unsafe fn with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(&mut Self::Target) -> Ret;
}
/// A trait that extends `Option<&UnsafeCell>` to allow running closures
/// against its underlying data.
pub trait UnsafeCellOptionWith {
type Target: ?Sized;
/// Runs `f` against a mutable reference borrowed from a
/// [`Option<&UnsafeCell>`].
///
/// # Safety
///
/// Caller must guarantee there are no mutable aliases to the
/// underlying data.
unsafe fn as_deref_with_mut_unchecked<F, Ret>(&self, f: F) -> Ret
where
F: FnOnce(Option<&mut Self::Target>) -> Ret;
}
}
}
pub mod debug_abort {
#[cfg(all(test, panic = "unwind"))]
use std::panic::Location;
/// Runs the closure, aborting the process if a unwinding panic occurs.
#[cfg(all(test, panic = "unwind"))]
#[track_caller]
pub fn on_unwind<T, F: FnOnce() -> T>(may_unwind: F) -> T {
let location = Location::caller();
let abort = DebugAbort { location };
let value = may_unwind();
core::mem::forget(abort);
value
}
/// Runs the closure, without aborting the process if a unwinding panic
/// occurs.
#[cfg(not(all(test, panic = "unwind")))]
#[cfg(not(tarpaulin_include))]
pub fn on_unwind<T, F: FnOnce() -> T>(may_unwind: F) -> T {
may_unwind()
}
/// A test only type that will abort the program execution once dropped.
///
/// To avoid aborting the proccess, callers must `forget` all instances of
/// the `Abort` type.
#[cfg(all(test, panic = "unwind"))]
struct DebugAbort {
location: &'static Location<'static>,
}
#[cfg(all(test, panic = "unwind"))]
#[cfg(not(tarpaulin_include))]
impl Drop for DebugAbort {
fn drop(&mut self) {
panic!("thread exits are forbidden inside {:?}, aborting", self.location);
}
}
}
pub mod hint {
#[cfg(not(all(loom, test)))]
pub use core::hint::spin_loop;
#[cfg(all(loom, test))]
pub use loom::hint::spin_loop;
}
#[cfg(test)]
pub mod sync {
#[cfg(not(all(loom, test)))]
pub use std::sync::Arc;
#[cfg(all(loom, test))]
pub use loom::sync::Arc;
}
pub mod thread {
#[cfg(all(any(feature = "yield", test), not(loom)))]
pub use std::thread::yield_now;
#[cfg(all(loom, test))]
pub use loom::thread::yield_now;
#[cfg(all(feature = "thread_local", not(loom)))]
pub use std::thread::LocalKey;
#[cfg(all(feature = "thread_local", loom, test))]
pub use loom::thread::LocalKey;
}