-
Notifications
You must be signed in to change notification settings - Fork 13.2k
/
Copy pathderiving-associated-types.rs
210 lines (195 loc) · 4.03 KB
/
deriving-associated-types.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
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(core)]
pub trait DeclaredTrait {
type Type;
}
impl DeclaredTrait for i32 {
type Type = i32;
}
pub trait WhereTrait {
type Type;
}
impl WhereTrait for i32 {
type Type = i32;
}
// Make sure we don't add a bound that just shares a name with an associated
// type.
pub mod module {
pub type Type = i32;
}
#[derive(PartialEq, Debug)]
struct PrivateStruct<T>(T);
#[derive(PartialEq, Debug)]
struct TupleStruct<A, B: DeclaredTrait, C>(
module::Type,
Option<module::Type>,
A,
PrivateStruct<A>,
B,
B::Type,
Option<B::Type>,
<B as DeclaredTrait>::Type,
Option<<B as DeclaredTrait>::Type>,
C,
C::Type,
Option<C::Type>,
<C as WhereTrait>::Type,
Option<<C as WhereTrait>::Type>,
<i32 as DeclaredTrait>::Type,
) where C: WhereTrait;
#[derive(PartialEq, Debug)]
pub struct Struct<A, B: DeclaredTrait, C> where C: WhereTrait {
m1: module::Type,
m2: Option<module::Type>,
a1: A,
a2: PrivateStruct<A>,
b: B,
b1: B::Type,
b2: Option<B::Type>,
b3: <B as DeclaredTrait>::Type,
b4: Option<<B as DeclaredTrait>::Type>,
c: C,
c1: C::Type,
c2: Option<C::Type>,
c3: <C as WhereTrait>::Type,
c4: Option<<C as WhereTrait>::Type>,
d: <i32 as DeclaredTrait>::Type,
}
#[derive(PartialEq, Debug)]
enum Enum<A, B: DeclaredTrait, C> where C: WhereTrait {
Unit,
Seq(
module::Type,
Option<module::Type>,
A,
PrivateStruct<A>,
B,
B::Type,
Option<B::Type>,
<B as DeclaredTrait>::Type,
Option<<B as DeclaredTrait>::Type>,
C,
C::Type,
Option<C::Type>,
<C as WhereTrait>::Type,
Option<<C as WhereTrait>::Type>,
<i32 as DeclaredTrait>::Type,
),
Map {
m1: module::Type,
m2: Option<module::Type>,
a1: A,
a2: PrivateStruct<A>,
b: B,
b1: B::Type,
b2: Option<B::Type>,
b3: <B as DeclaredTrait>::Type,
b4: Option<<B as DeclaredTrait>::Type>,
c: C,
c1: C::Type,
c2: Option<C::Type>,
c3: <C as WhereTrait>::Type,
c4: Option<<C as WhereTrait>::Type>,
d: <i32 as DeclaredTrait>::Type,
},
}
fn main() {
let e: TupleStruct<
i32,
i32,
i32,
> = TupleStruct(
0,
None,
0,
PrivateStruct(0),
0,
0,
None,
0,
None,
0,
0,
None,
0,
None,
0,
);
assert_eq!(e, e);
let e: Struct<
i32,
i32,
i32,
> = Struct {
m1: 0,
m2: None,
a1: 0,
a2: PrivateStruct(0),
b: 0,
b1: 0,
b2: None,
b3: 0,
b4: None,
c: 0,
c1: 0,
c2: None,
c3: 0,
c4: None,
d: 0,
};
assert_eq!(e, e);
let e = Enum::Unit::<i32, i32, i32>;
assert_eq!(e, e);
let e: Enum<
i32,
i32,
i32,
> = Enum::Seq(
0,
None,
0,
PrivateStruct(0),
0,
0,
None,
0,
None,
0,
0,
None,
0,
None,
0,
);
assert_eq!(e, e);
let e: Enum<
i32,
i32,
i32,
> = Enum::Map {
m1: 0,
m2: None,
a1: 0,
a2: PrivateStruct(0),
b: 0,
b1: 0,
b2: None,
b3: 0,
b4: None,
c: 0,
c1: 0,
c2: None,
c3: 0,
c4: None,
d: 0,
};
assert_eq!(e, e);
}