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
use crateIsTStr;
use PhantomData;
/// For querying the amount of variants of an enum.
///
/// # Safety
///
/// Count must be a [TStr](../struct.TStr.html)
/// with the amount of enum variants written in decimal.
/// For example:`TS!(9)` would be used for an enum with 9 variants.
///
/// Implementors who specify fewer variants than the enum actually has
/// may result in undefined behavior when the enum is matched in the `switch` macro.
///
/// # Example
///
/// This example demonstrates using `VariantCount` as a bound to
/// restrict a pre-existing structural alias.
///
/// ```rust
/// use structural::{Structural,TS,structural_alias,switch};
/// use structural::enums::VariantCount;
///
/// # fn main(){
/// {
/// // Enum
///
/// assert_eq!( nonexhaustive(Enum::Foo), Some(0) );
/// assert_eq!( exhaustive_a(Enum::Foo), 0 );
/// assert_eq!( exhaustive_b(Enum::Foo), 0 );
///
/// let bar=Enum::Bar(0);
/// assert_eq!( nonexhaustive(bar.clone()), Some(1) );
/// assert_eq!( exhaustive_a(bar.clone()), 1 );
/// assert_eq!( exhaustive_b(bar), 1 );
///
/// let baz=Enum::Baz{wha:"whoah".into()};
/// assert_eq!( nonexhaustive(baz.clone()), Some(2) );
/// assert_eq!( exhaustive_a(baz.clone()), 2 );
/// assert_eq!( exhaustive_b(baz), 2 );
/// }
/// {
/// // HyperEnum:
/// // This enum has a superset of the variants required by `Ternary`.
/// // The commented out lines below don't compile
/// // because the `exhaustive_*` functions require the enum to only have
/// // the `Foo`,`Bar`,and `Baz` variants
///
/// assert_eq!( nonexhaustive(HyperEnum::Foo), Some(0) );
/// // assert_eq!( exhaustive_a(HyperEnum::Foo), 0 );
/// // assert_eq!( exhaustive_b(HyperEnum::Foo), 0 );
///
/// assert_eq!( nonexhaustive(HyperEnum::Bar), Some(1) );
/// // assert_eq!( exhaustive_a(HyperEnum::Bar), 1 );
/// // assert_eq!( exhaustive_b(HyperEnum::Bar), 1 );
///
/// assert_eq!( nonexhaustive(HyperEnum::Baz), Some(2) );
/// // assert_eq!( exhaustive_a(HyperEnum::Baz), 2 );
/// // assert_eq!( exhaustive_b(HyperEnum::Baz), 2 );
///
/// assert_eq!( nonexhaustive(HyperEnum::Boom), None );
/// }
/// # }
///
/// // This function returns the index of the current variant of the enum,
/// // but because `Ternary` is a nonexhaustive structural trait,
/// // it returns None to handle the case where the enum is
/// // none of the three variants.
/// //
/// fn nonexhaustive<T>(this: T)->Option<u8>
/// where
/// T: Ternary,
/// {
/// // The VariantCount bound allow this switch to be exhaustive.
/// switch!{this;
/// Foo=>Some(0),
/// Bar=>Some(1),
/// Baz=>Some(2),
/// // This branch is required,
/// // because `Ternary` doesn't require the enum to have exactly 3 variants
/// _=>None,
/// }
/// }
///
/// // This function returns the index of the current variant of the enum,
/// fn exhaustive_a<T>(this: T)->u8
/// where
/// T: Ternary + VariantCount<Count=TS!(3)>,
/// {
/// // The VariantCount bound allow this switch to be exhaustive.
/// switch!{this;
/// Foo=>0,
/// Bar=>1,
/// Baz=>2,
/// }
/// }
///
/// fn exhaustive_b<T>(this: T)->u8
/// where
/// // `TernaryExhaustive` is equivalent to `Ternary + VariantCount<Count=TS!(3)>`.
/// //
/// // You would use a `+ VariantCount<Count=_>` bound if all of thse happen:
/// // - The structural alias came from somewhere else.
/// // - It's a nonexhaustive structural alias.
/// // - You don't want to do declare another alias,like `TernarySuper`.
/// T: TernaryExhaustive
/// {
/// exhaustive_a(this)
/// }
///
/// structural_alias!{
/// // `#[struc(and_exhaustive_enum(...))]` generates a subtrait with
/// //`VariantCount<Count=TS!($variant_count)>` as an additional bound
/// // (the `$variant_count` stands for the number of variants in the structural alias)
/// #[struc(and_exhaustive_enum(name="TernaryExhaustive"))]
/// pub trait Ternary{
/// Foo,
/// Bar,
/// Baz,
/// }
///
/// pub trait TernarySuper: Ternary + VariantCount<Count=TS!(3)> {}
/// }
///
/// #[derive(Structural,Clone)]
/// # #[struc(no_trait)]
/// enum Enum{
/// Foo,
/// Bar(u32),
/// Baz{wha:String},
/// }
///
/// #[derive(Structural,Clone)]
/// # #[struc(no_trait)]
/// enum HyperEnum{
/// Foo,
/// Bar,
/// Baz,
/// Boom,
/// }
///
///
/// ```
pub unsafe
/// Queries the amount of variants of `This`.
///
/// This evaluates to a [TStr](../struct.TStr.html),like `TS!(9)`
///
/// # Example
///
/// This demonstrates `VariantCountOut` by
/// making a function that requires two enums to have the same number of variants.
///
/// ```rust
/// use structural::Structural;
/// use structural::enums::{VariantCount,VariantCountOut};
///
/// same_sized_enums( &Enum1::Foo, &Enum2::A );
///
/// // This does not compile because `Enum1` has 3 variants and `Result` has 2 variants.
/// // same_sized_enums( &Enum1::Foo, &Result::<(),()>::Ok(()) );
///
/// // This function asserts at compile-time that both enums have the same number of variants.
/// fn same_sized_enums<L,R>(left:&L,right:&R)
/// where
/// L:VariantCount,
/// R:VariantCount<Count=VariantCountOut<L>>,
/// {}
///
///
/// #[derive(Structural)]
/// # #[struc(no_trait)]
/// enum Enum1{
/// Foo,
/// Bar,
/// Baz,
/// }
///
/// #[derive(Structural)]
/// # #[struc(no_trait)]
/// enum Enum2{
/// A,
/// B,
/// C,
/// }
/// ```
pub type VariantCountOut<This> = Count;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Exploits autoref-based specialization,
/// based on whether the `T` in this `PhantomData<T>` implements `VariantCount` .
///
/// This trait's single method returns either:
///
/// - `ExpectedVariantCount<_>`: If `T` implements `VariantCount`
///
/// . `ExpectedDefaultBranch`: If `T` does not implement `VariantCount`
///
/// Helper type for the `switch` macro,
/// to assert that all variants have been matched
/// when the user does not write a default branch.
>);
/////////////////////////////////////
/// Helper type for the `switch` macro,
;
//////////////////////////////////////////////////////////////////////////
///////////////////////
///////////////////////