-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconstraint.rs
More file actions
166 lines (149 loc) · 4.77 KB
/
constraint.rs
File metadata and controls
166 lines (149 loc) · 4.77 KB
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
use std::collections::hash_map::Entry;
use std::hash::Hash;
use parking_lot::Mutex;
use crate::Track;
use crate::passthroughhasher::PassthroughHashMap;
use crate::track::{Call, Sink};
/// Records calls performed on a trackable type.
///
/// Allows to validate that a different instance of the trackable type yields
/// the same outputs for the recorded calls.
///
/// The constraint can be hooked up to a tracked type through
/// [`Track::track_with`].
pub struct Constraint<C>(Mutex<ConstraintRepr<C>>);
/// The internal representation of a [`Constraint`].
struct ConstraintRepr<C> {
/// The immutable calls, ready for integration into a call tree.
immutable: CallSequence<C>,
/// The mutable calls, for insertion as part of the call tree output value.
mutable: Vec<C>,
}
impl<C> Constraint<C> {
/// Creates a new constraint.
pub fn new() -> Self {
Self::default()
}
/// Checks whether the given value matches the constraints by invoking the
/// recorded calls one by one.
pub fn validate<T>(&self, value: &T) -> bool
where
T: Track<Call = C>,
{
self.0
.lock()
.immutable
.vec
.iter()
.filter_map(|x| x.as_ref())
.all(|(call, ret)| value.call(call) == *ret)
}
/// Takes out the immutable and mutable calls, for insertion into a call
/// tree.
pub(crate) fn take(&self) -> (CallSequence<C>, Vec<C>) {
let mut inner = self.0.lock();
(std::mem::take(&mut inner.immutable), std::mem::take(&mut inner.mutable))
}
}
impl<C> Default for Constraint<C> {
fn default() -> Self {
Self(Mutex::new(ConstraintRepr {
immutable: CallSequence::new(),
mutable: Vec::new(),
}))
}
}
impl<C: Call> Sink for Constraint<C> {
type Call = C;
fn emit(&self, call: C, ret: u128) -> bool {
let mut inner = self.0.lock();
if call.is_mutable() {
inner.mutable.push(call);
true
} else {
inner.immutable.insert(call, ret)
}
}
}
/// A deduplicated sequence of calls to tracked functions, optimized for
/// efficient insertion into a call tree.
pub struct CallSequence<C> {
/// The raw calls. In order, but deduplicated via the `map`.
vec: Vec<Option<(C, u128)>>,
/// A map from hashes of calls to the indices in the vector.
map: PassthroughHashMap<u128, usize>,
/// A cursor for iteration in `Self::next`.
cursor: usize,
}
impl<C> CallSequence<C> {
/// Creates an empty sequence.
pub fn new() -> Self {
Self {
vec: Vec::new(),
map: PassthroughHashMap::default(),
cursor: 0,
}
}
}
impl<C: Hash> CallSequence<C> {
/// Inserts a pair of a call and its return hash.
///
/// Returns true when the pair was indeed inserted and false if the call was
/// deduplicated.
pub fn insert(&mut self, call: C, ret: u128) -> bool {
match self.map.entry(crate::hash::hash(&call)) {
Entry::Vacant(entry) => {
let i = self.vec.len();
self.vec.push(Some((call, ret)));
entry.insert(i);
true
}
#[allow(unused_variables)]
Entry::Occupied(entry) => {
#[cfg(debug_assertions)]
if let Some((_, ret2)) = &self.vec[*entry.get()] {
if ret != *ret2 {
panic!(
"comemo: found differing return values. \
is there an impure tracked function?"
)
}
}
false
}
}
}
/// Retrieves the next call in order.
pub fn next(&mut self) -> Option<(C, u128)> {
while self.cursor < self.vec.len() {
if let Some(pair) = self.vec[self.cursor].take() {
return Some(pair);
}
self.cursor += 1;
}
None
}
/// Retrieves the return hash of an arbitrary upcoming call. Removes the
/// call from the sequence; it will not be yielded by `next()` anymore.
pub fn extract(&mut self, call: &C) -> Option<u128> {
let h = crate::hash::hash(&call);
let i = *self.map.get(&h)?;
let res = self.vec[i].take().map(|(_, ret)| ret);
debug_assert!(self.cursor <= i || res.is_none());
res
}
}
impl<C> Default for CallSequence<C> {
fn default() -> Self {
Self::new()
}
}
impl<C: Hash> FromIterator<(C, u128)> for CallSequence<C> {
fn from_iter<T: IntoIterator<Item = (C, u128)>>(iter: T) -> Self {
let mut seq = CallSequence::new();
for (call, ret) in iter {
seq.insert(call, ret);
}
seq
}
}