-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinput.rs
211 lines (178 loc) · 5.89 KB
/
input.rs
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
use std::hash::{Hash, Hasher};
use crate::constraint::Join;
use crate::track::{Track, Tracked, TrackedMut, Validate};
/// Ensure a type is suitable as input.
#[inline]
pub fn assert_hashable_or_trackable<In: Input>(_: &In) {}
/// An input to a cached function.
///
/// This is implemented for hashable types, `Tracked<_>` types and `Args<(...)>`
/// types containing tuples up to length twelve.
pub trait Input {
/// The constraints for this input.
type Constraint: Default + Clone + Join + 'static;
/// The input with new constraints hooked in.
type Tracked<'r>
where
Self: 'r;
/// The extracted outer constraints.
type Outer: Join<Self::Constraint>;
/// Hash the key parts of the input.
fn key<H: Hasher>(&self, state: &mut H);
/// Validate the tracked parts of the input.
fn validate(&self, constraint: &Self::Constraint) -> bool;
/// Replay mutations to the input.
fn replay(&mut self, constraint: &Self::Constraint);
/// Hook up the given constraint to the tracked parts of the input and
/// return the result alongside the outer constraints.
fn retrack<'r>(
self,
constraint: &'r Self::Constraint,
) -> (Self::Tracked<'r>, Self::Outer)
where
Self: 'r;
}
impl<T: Hash> Input for T {
// No constraint for hashed inputs.
type Constraint = ();
type Tracked<'r> = Self where Self: 'r;
type Outer = ();
#[inline]
fn key<H: Hasher>(&self, state: &mut H) {
Hash::hash(self, state);
}
#[inline]
fn validate(&self, _: &()) -> bool {
true
}
#[inline]
fn replay(&mut self, _: &Self::Constraint) {}
#[inline]
fn retrack<'r>(self, _: &'r ()) -> (Self::Tracked<'r>, Self::Outer)
where
Self: 'r,
{
(self, ())
}
}
impl<'a, T> Input for Tracked<'a, T>
where
T: Track + ?Sized,
{
// Forward constraint from `Trackable` implementation.
type Constraint = <T as Validate>::Constraint;
type Tracked<'r> = Tracked<'r, T> where Self: 'r;
type Outer = Option<&'a Self::Constraint>;
#[inline]
fn key<H: Hasher>(&self, _: &mut H) {}
#[inline]
fn validate(&self, constraint: &Self::Constraint) -> bool {
self.value.validate_with_id(constraint, self.id)
}
#[inline]
fn replay(&mut self, _: &Self::Constraint) {}
#[inline]
fn retrack<'r>(
self,
constraint: &'r Self::Constraint,
) -> (Self::Tracked<'r>, Self::Outer)
where
Self: 'r,
{
let tracked = Tracked {
value: self.value,
constraint: Some(constraint),
id: self.id,
};
(tracked, self.constraint)
}
}
impl<'a, T> Input for TrackedMut<'a, T>
where
T: Track + ?Sized,
{
// Forward constraint from `Trackable` implementation.
type Constraint = T::Constraint;
type Tracked<'r> = TrackedMut<'r, T> where Self: 'r;
type Outer = Option<&'a Self::Constraint>;
#[inline]
fn key<H: Hasher>(&self, _: &mut H) {}
#[inline]
fn validate(&self, constraint: &Self::Constraint) -> bool {
self.value.validate(constraint)
}
#[inline]
fn replay(&mut self, constraint: &Self::Constraint) {
self.value.replay(constraint);
}
#[inline]
fn retrack<'r>(
self,
constraint: &'r Self::Constraint,
) -> (Self::Tracked<'r>, Self::Outer)
where
Self: 'r,
{
let tracked = TrackedMut { value: self.value, constraint: Some(constraint) };
(tracked, self.constraint)
}
}
/// Wrapper for multiple inputs.
pub struct Args<T>(pub T);
macro_rules! args_input {
($($param:tt $alt:tt $idx:tt ),*) => {
#[allow(unused_variables, non_snake_case)]
impl<$($param: Input),*> Input for Args<($($param,)*)> {
type Constraint = ($($param::Constraint,)*);
type Tracked<'r> = ($($param::Tracked<'r>,)*) where Self: 'r;
type Outer = ($($param::Outer,)*);
#[inline]
fn key<T: Hasher>(&self, state: &mut T) {
$((self.0).$idx.key(state);)*
}
#[inline]
fn validate(&self, constraint: &Self::Constraint) -> bool {
true $(&& (self.0).$idx.validate(&constraint.$idx))*
}
#[inline]
fn replay(&mut self, constraint: &Self::Constraint) {
$((self.0).$idx.replay(&constraint.$idx);)*
}
#[inline]
fn retrack<'r>(
self,
constraint: &'r Self::Constraint,
) -> (Self::Tracked<'r>, Self::Outer)
where
Self: 'r,
{
$(let $param = (self.0).$idx.retrack(&constraint.$idx);)*
(($($param.0,)*), ($($param.1,)*))
}
}
#[allow(unused_variables, clippy::unused_unit)]
impl<$($param: Join<$alt>, $alt),*> Join<($($alt,)*)> for ($($param,)*) {
#[inline]
fn join(&self, constraint: &($($alt,)*)) {
$(self.$idx.join(&constraint.$idx);)*
}
#[inline]
fn take(&self) -> Self {
($(self.$idx.take(),)*)
}
}
};
}
args_input! {}
args_input! { A Z 0 }
args_input! { A Z 0, B Y 1 }
args_input! { A Z 0, B Y 1, C X 2 }
args_input! { A Z 0, B Y 1, C X 2, D W 3 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6, H S 7 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6, H S 7, I R 8 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6, H S 7, I R 8, J Q 9 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6, H S 7, I R 8, J Q 9, K P 10 }
args_input! { A Z 0, B Y 1, C X 2, D W 3, E V 4, F U 5, G T 6, H S 7, I R 8, J Q 9, K P 10, L O 11 }