forked from rust-lang/chalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast.rs
287 lines (248 loc) · 6.03 KB
/
cast.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use ir::*;
use std::marker::PhantomData;
/// The `Cast` trait is used to make annoying upcasts between
/// logically equivalent types that imply wrappers. For example, one
/// could convert a `DomainGoal` into a `Goal` by doing:
///
/// ```ignore
/// let goal: Goal = domain_goal.cast();
/// ```
///
/// This is equivalent to the more explicit:
///
/// ```ignore
/// let goal: Goal = Goal::DomainGoal(domain_goal)
/// ```
///
/// Another useful trick is the `casted()` iterator adapter, which
/// casts each element in the iterator as it is produced (you must
/// have the `Caster` trait in scope for that).
///
/// # Invariant
///
/// `Cast` imposes a key invariant. You can only implement `T:
/// Cast<U>` if both `T` and `U` have the same semantic meaning. Also,
/// as part of this, they should always use the same set of free
/// variables (the `Canonical` implementation, for example, relies on
/// that).
crate trait Cast<T>: Sized {
fn cast(self) -> T;
}
macro_rules! reflexive_impl {
(for($($t:tt)*) $u:ty) => {
impl<$($t)*> Cast<$u> for $u {
fn cast(self) -> $u {
self
}
}
};
($u:ty) => {
impl Cast<$u> for $u {
fn cast(self) -> $u {
self
}
}
};
}
reflexive_impl!(TraitRef);
reflexive_impl!(LeafGoal);
reflexive_impl!(DomainGoal);
impl Cast<DomainGoal> for TraitRef {
fn cast(self) -> DomainGoal {
DomainGoal::Implemented(self)
}
}
impl Cast<LeafGoal> for TraitRef {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<DomainGoal> for Normalize {
fn cast(self) -> DomainGoal {
DomainGoal::Normalize(self)
}
}
impl Cast<LeafGoal> for Normalize {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<DomainGoal> for ProjectionEq {
fn cast(self) -> DomainGoal {
DomainGoal::ProjectionEq(self)
}
}
impl Cast<LeafGoal> for ProjectionEq {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<DomainGoal> for UnselectedNormalize {
fn cast(self) -> DomainGoal {
DomainGoal::UnselectedNormalize(self)
}
}
impl Cast<LeafGoal> for UnselectedNormalize {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<DomainGoal> for WellFormed {
fn cast(self) -> DomainGoal {
DomainGoal::WellFormed(self)
}
}
impl Cast<LeafGoal> for WellFormed {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<DomainGoal> for FromEnv {
fn cast(self) -> DomainGoal {
DomainGoal::FromEnv(self)
}
}
impl Cast<LeafGoal> for FromEnv {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self.cast())
}
}
impl Cast<Goal> for WellFormed {
fn cast(self) -> Goal {
let wcg: LeafGoal = self.cast();
wcg.cast()
}
}
impl Cast<Goal> for FromEnv {
fn cast(self) -> Goal {
let wcg: LeafGoal = self.cast();
wcg.cast()
}
}
impl Cast<Goal> for Normalize {
fn cast(self) -> Goal {
let wcg: LeafGoal = self.cast();
wcg.cast()
}
}
impl Cast<Goal> for ProjectionEq {
fn cast(self) -> Goal {
let wcg: LeafGoal = self.cast();
wcg.cast()
}
}
impl Cast<Goal> for UnselectedNormalize {
fn cast(self) -> Goal {
let wcg: LeafGoal = self.cast();
wcg.cast()
}
}
impl Cast<LeafGoal> for DomainGoal {
fn cast(self) -> LeafGoal {
LeafGoal::DomainGoal(self)
}
}
impl Cast<Goal> for TraitRef {
fn cast(self) -> Goal {
Goal::Leaf(self.cast())
}
}
impl Cast<Goal> for DomainGoal {
fn cast(self) -> Goal {
Goal::Leaf(self.cast())
}
}
impl Cast<Goal> for LeafGoal {
fn cast(self) -> Goal {
Goal::Leaf(self)
}
}
impl Cast<LeafGoal> for EqGoal {
fn cast(self) -> LeafGoal {
LeafGoal::EqGoal(self)
}
}
impl Cast<Ty> for ApplicationTy {
fn cast(self) -> Ty {
Ty::Apply(self)
}
}
impl Cast<Ty> for ProjectionTy {
fn cast(self) -> Ty {
Ty::Projection(self)
}
}
impl Cast<Parameter> for Ty {
fn cast(self) -> Parameter {
ParameterKind::Ty(self)
}
}
impl Cast<Parameter> for Lifetime {
fn cast(self) -> Parameter {
ParameterKind::Lifetime(self)
}
}
macro_rules! map_impl {
(impl[$($t:tt)*] Cast<$b:ty> for $a:ty) => {
impl<$($t)*> Cast<$b> for $a {
fn cast(self) -> $b {
self.map(|v| v.cast())
}
}
}
}
map_impl!(impl[T: Cast<U>, U] Cast<Option<U>> for Option<T>);
map_impl!(impl[T: Cast<U>, U] Cast<InEnvironment<U>> for InEnvironment<T>);
map_impl!(impl[T: Cast<U>, U, E] Cast<Result<U, E>> for Result<T, E>);
impl<T, U> Cast<Canonical<U>> for Canonical<T>
where
T: Cast<U>,
{
fn cast(self) -> Canonical<U> {
// Subtle point: It should be ok to re-use the binders here,
// because `cast()` never introduces new inference variables,
// nor changes the "substance" of the type we are working
// with. It just introduces new wrapper types.
Canonical {
value: self.value.cast(),
binders: self.binders,
}
}
}
impl<T, U> Cast<Vec<U>> for Vec<T>
where
T: Cast<U>,
{
fn cast(self) -> Vec<U> {
self.into_iter().casted().collect()
}
}
crate struct Casted<I, U> {
iterator: I,
_cast: PhantomData<U>,
}
impl<I: Iterator, U> Iterator for Casted<I, U>
where
I::Item: Cast<U>,
{
type Item = U;
fn next(&mut self) -> Option<Self::Item> {
self.iterator.next().map(|item| item.cast())
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iterator.size_hint()
}
}
/// An iterator adapter that casts each element we are iterating over
/// to some other type.
crate trait Caster<U>: Sized {
fn casted(self) -> Casted<Self, U>;
}
impl<I: Iterator, U> Caster<U> for I {
fn casted(self) -> Casted<Self, U> {
Casted {
iterator: self,
_cast: PhantomData,
}
}
}