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
use core::{
cell::Cell,
task::{RawWaker, RawWakerVTable, Waker},
};
pub(crate) fn counting_waker(drops: &Cell<u32>) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|data| RawWaker::new(data, &VTABLE),
|_| {},
|_| {},
|data| {
// SAFETY: the data is the `Cell` from `counting_waker`, which every test keeps
// alive for as long as its wakers.
let drops = unsafe { &*data.cast::<Cell<u32>>() };
drops.set(drops.get() + 1);
},
);
// SAFETY: the vtable only reads the data as a `&Cell<u32>`, and `drops` outlives the
// waker (see above).
unsafe { Waker::new(core::ptr::from_ref(drops).cast(), &VTABLE) }
}
pub(crate) fn flag_waker(woken: &Cell<bool>) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|data| RawWaker::new(data, &VTABLE),
set_woken,
set_woken,
|_| {},
);
// SAFETY: the vtable only reads the data as a `&Cell<bool>`, and `woken` outlives the waker
// (see `set_woken`).
unsafe { Waker::new(core::ptr::from_ref(woken).cast(), &VTABLE) }
}
fn set_woken(data: *const ()) {
// SAFETY: the data is the `Cell` from `flag_waker`, which every test keeps alive for as long
// as its wakers.
let woken = unsafe { &*data.cast::<Cell<bool>>() };
woken.set(true);
}