-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtraits.rs
More file actions
358 lines (301 loc) · 6.02 KB
/
Copy pathtraits.rs
File metadata and controls
358 lines (301 loc) · 6.02 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Tests on traits
#![allow(dead_code)]
#![allow(unused_variables)]
// Simple trait
mod basic {
trait T1 {
fn f1(&self) -> usize;
fn f2(&self, other: &Self) -> usize;
}
// Simple Impl
struct S;
impl T1 for S {
fn f1(&self) -> usize {
42
}
fn f2(&self, other: &Self) -> usize {
43
}
}
// Simple ImplExpr
fn f<T: T1>(x: T) -> usize {
x.f1() + x.f2(&x)
}
}
// Bounds on parameters and on self
mod bounds {
trait T1 {
fn f1(&self) -> usize;
}
trait T2 {
fn f2(&self) -> usize;
}
trait Test<T: T1>: T2 {
fn f_test(&self, x: &T) -> usize;
}
struct S1;
impl T1 for S1 {
fn f1(&self) -> usize {
0
}
}
struct S2;
impl T2 for S2 {
fn f2(&self) -> usize {
1
}
}
impl Test<S1> for S2 {
fn f_test(&self, x: &S1) -> usize {
x.f1() + self.f2() + 1
}
}
fn test(x1: S1, x2: S2) -> usize {
x2.f_test(&x1) + x1.f1()
}
}
mod associated_types {
trait T1 {
type T;
fn f(&self, x: Self::T) -> Self::T;
}
trait T2 {
type T: T1;
fn f(&self, x: Self::T) -> usize;
}
trait Foo<T> {}
trait Bar {}
trait T3 {
type T: Bar;
type Tp<A: Bar>: Foo<Self::T>;
fn f<A: Bar>(&self, x: Self::T, y: Self::Tp<A>) -> usize;
}
struct S {}
impl T1 for S {
type T = i32;
fn f(&self, x: Self::T) -> Self::T {
2121
}
}
impl T2 for S {
type T = S;
fn f(&self, x: Self::T) -> usize {
21
}
}
impl Bar for i16 {}
impl<A> Foo<i16> for (u32, A) {}
// impl T3 for S {
// type T = i16;
// type Tp<A: Bar> = (u32, A);
// fn f<A: Bar>(&self, x: Self::T, y: Self::Tp<A>) -> usize {
// 12
// }
// }
trait Chain0 {}
trait Chain1 {
type A: Chain0;
type B: Chain0;
}
trait Chain2: Chain1 {}
trait Chain3: Chain2 {
fn f() -> Self::A;
}
impl Chain0 for u8 {}
impl Chain1 for u8 {
type A = u8;
type B = u8;
}
impl Chain2 for u8 {}
impl Chain3 for u8 {
fn f() -> u8 {
0
}
}
}
mod overlapping_methods {
trait T1 {
fn f(&self) -> usize;
}
trait T2 {
fn f(&self) -> usize;
}
trait T3 {
fn f(&self) -> usize;
}
impl T1 for u32 {
fn f(&self) -> usize {
0
}
}
impl T2 for u32 {
fn f(&self) -> usize {
1
}
}
impl T3 for u32 {
fn f(&self) -> usize {
2
}
}
fn test() -> usize {
let x: u32 = 9;
T1::f(&x) + T2::f(&x) + T3::f(&x)
}
}
mod inheritance {
trait T1 {
fn f1(&self) -> usize;
}
trait T2 {
fn f2(&self) -> usize;
}
trait T3: T2 + T1 {
fn f3(&self) -> usize;
}
trait Tp1 {
fn f1(&self) -> usize;
}
trait Tp2: Tp1 + T3 {
fn fp2(&self) -> usize;
}
struct S {}
impl T1 for S {
fn f1(&self) -> usize {
1
}
}
impl T2 for S {
fn f2(&self) -> usize {
2
}
}
impl T3 for S {
fn f3(&self) -> usize {
3
}
}
impl Tp1 for S {
fn f1(&self) -> usize {
10
}
}
impl Tp2 for S {
fn fp2(&self) -> usize {
Tp1::f1(self) + T1::f1(self) + T2::f2(self) + T3::f3(self)
}
}
fn test() -> usize {
let s = S {};
s.f3() + 1
}
}
mod default {
trait Easy {
fn dft(&self) -> usize {
32
}
}
impl Easy for usize {
fn dft(&self) -> usize {
self + 1
}
}
impl Easy for u32 {}
trait T1 {
fn f1(&self) -> usize;
fn f2(&self) -> usize {
1
}
fn f3<A>(&self, x: &A) -> usize {
1
}
fn f4<A: Easy>(&self, x: &A) -> usize {
x.dft() + 1
}
}
struct S<A>(usize, A);
// Override
impl T1 for S<usize> {
fn f1(&self) -> usize {
self.0 + self.1
}
fn f2(&self) -> usize {
self.1
}
}
impl T1 for S<bool> {
fn f1(&self) -> usize {
if self.1 { self.0 } else { 9 }
}
fn f2(&self) -> usize {
self.0 + 1
}
}
// No override
impl T1 for S<String> {
fn f1(&self) -> usize {
0
}
}
}
mod trait_level_args {
trait T1<A, B> {
fn f1<C, D>(&self) -> (); // A and B do not appear
fn f2<C, D>(&self, x: &A) -> (); // A appears
fn f3<C, D>(&self, x: &A, y: &B) -> (); // Both appear
}
impl T1<u32, u64> for usize {
fn f1<C, D>(&self) {}
fn f2<C, D>(&self, x: &u32) {}
fn f3<C, D>(&self, x: &u32, y: &u64) {}
}
fn test<A, B, C, D, U: T1<A, B>>(x: U, a: &A, b: &B) -> () {
x.f1::<C, D>();
x.f2::<C, D>(a);
x.f3::<C, D>(a, b);
}
}
mod trait_with_constraints {
trait T1 {}
trait T2 {
fn func(&self) -> bool
where
Self: T1;
}
impl<A: T1> T2 for A {
fn func(&self) -> bool
where
A: T1,
{
true
}
}
}
mod associated_constant {
pub trait Foo {
const f: bool;
const x: u8 = 0;
}
pub struct Bar;
impl Foo for Bar {
const f: bool = true;
const x: u8 = 1 + 1;
}
// https://github.com/cryspen/hax/issues/1940
trait Baz {
const One: u32 = 1;
}
fn foo<F: Baz>(n: u32) -> u32 {
n + F::One
}
}
// https://github.com/cryspen/hax/issues/1889
mod trait_constant_in_default_body {
pub trait Foo: std::marker::Sized {
const F: Self;
fn f() -> Self {
Self::F
}
}
}