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
use crate::{Driver, Pinned, Value, Wrapped};
use alloc::{boxed::Box, vec::Vec};
pub(crate) struct Directive<'lt> {
closure: Box<dyn FnOnce(&mut Driver<'lt, Pinned>) + Send + 'lt>,
}
impl<'lt> Directive<'lt> {
pub(crate) fn new<
Tag,
Input: 'lt + Wrapped<Tag>,
NtsOutput: 'lt + Send + Sync,
TsOutput: 'lt + Wrapped<Tag>,
>(
input: Input,
op: impl FnOnce(Input::Unwrapped) -> DirectiveOutput<NtsOutput, TsOutput::Unwrapped>
+ Send
+ 'lt,
) -> (Directive<'lt>, Value<(NtsOutput, TsOutput)>) {
let (ret_slot, in_slot) = Value::new();
let closure = Box::new(move |driver: &mut Driver<'lt, Pinned>| {
#[cfg(feature = "fallback")]
if driver.is_fallback() {
let known = driver.known_values();
unsafe {
input.for_each_representative(|value| {
if !known.contains(&value) {
panic!("unknown value passed to driver: {:X}", value);
}
});
}
}
let unwrapped = unsafe { input.unwrap() };
let DirectiveOutput {
thread_safe_value,
thread_unsafe_value,
deleted_values,
} = op(unwrapped);
let ts_out = unsafe { TsOutput::wrap(thread_unsafe_value) };
#[cfg(feature = "fallback")]
{
let known = driver.known_values_mut();
deleted_values.into_iter().for_each(|value| {
known.remove(&value);
});
unsafe {
ts_out.for_each_representative(|value| {
known.insert(value);
});
}
}
in_slot.put((thread_safe_value, ts_out));
});
(Self { closure }, ret_slot)
}
pub(crate) fn run(self, driver: &mut Driver<'lt, Pinned>) {
(self.closure)(driver);
}
}
pub struct DirectiveOutput<NtsOutput, TsOutput> {
pub thread_safe_value: NtsOutput,
pub thread_unsafe_value: TsOutput,
pub deleted_values: Vec<usize>,
}