structural 0.3.0-alpha

Abstractions over fields.
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Every one of these modules is independent
#![allow(clippy::wildcard_imports)]

use crate::{
    enums::{IsVariant, VariantCount},
    FieldType, GetField, GetFieldMut, GetVariantField, GetVariantFieldMut, IntoField, IntoFieldMut,
    IntoVariantField, IntoVariantFieldMut, Structural, StructuralExt,
};

use std_::fmt::Debug;

mod with_super_traits {
    use super::*;

    structural_alias! {
        trait Trait:Copy{
            a:u8,
        }
    }
    trait AssertImplies: Trait {}

    impl<This> AssertImplies for This where This: Copy + IntoFieldMut<TS!(a), Ty = u8> {}

    /// This function ensures that the supertraits and field accessors in Trait
    /// are implied by `T:Trait`.
    #[allow(dead_code)]
    fn func<T: Trait>(v: T) {
        let _copy = v;
        let _: &u8 = v.field_(fp!(a));
    }
}

/////////////////////////////////////////////

mod with_where_clause {
    use super::*;

    structural_alias! {
        trait WithWhereClause<T:Clone>:Copy
        where
            T:Debug
        {
            a:T,
        }
    }

    trait AssertImplies<T>: WithWhereClause<T>
    where
        T: Clone + Debug,
    {
    }

    impl<This, T> AssertImplies<T> for This
    where
        T: Clone + Debug,
        This: Copy + IntoFieldMut<TS!(a), Ty = T>,
    {
    }
}

/////////////////////////////////////////////

mod all_access {
    use super::*;

    structural_alias! {
        trait Foo<T>{
                 a:u32,
            ref  b:T,
            mut  c:i64,
            move d:&'static str,
        }
    }

    trait Dummy {
        fn well<This, T>()
        where
            This: Foo<T>;
    }

    impl Dummy for () {
        fn well<This, T>()
        where
            This: IntoFieldMut<TS!(a), Ty = u32>
                + GetField<FP!(b), Ty = T>
                + GetFieldMut<FP!(c), Ty = i64>
                + IntoField<FP!(d), Ty = &'static str>,
        {
        }
    }
}

// Testing the presence of `IsVariant`
mod with_variants {
    use super::*;

    structural_alias! {
        pub trait Foo{
            A{},
            B{},
        }
    }

    field_path_aliases! {
        mod names{
            A,
            B,
        }
    }

    assert_equal_bounds! {
        trait Dummy,
        (Foo),
        (
            IsVariant<names::A>+
            IsVariant<names::B>
        )
    }
}

// Testing the presence of `IsVariant`
mod variants_with_accesses {
    use super::*;

    field_path_aliases! {
        mod paths{
            a,
            A,B,C,
        }
    }

    tstr_aliases! {
        mod strings{
            a,b,c,d,e,
            A,AOpt,B,C,
        }
    }

    structural_alias! {
        pub trait Foo{
            A{
                a:(u8,u8),
                ref b:(u8,u16),
                mut c:(u8,u32),
                move d:(u8,u64),
                mut move e:(u16,u8),
            },
            AOpt{
                a:Option<(u8,u8)>,
                ref b:Option<(u8,u16)>,
                mut c:Option<(u8,u32)>,
                move d:Option<(u8,u64)>,
                mut move e:Option<(u16,u8)>,
            },
            mut move B{
                a:i8,
                ref b:i16,
            },
            ref C{
                a:u8,
                move b:u16,
            },
            a:(),
        }
    }

    assert_equal_bounds! {
        trait Dummy,
        (Foo),
        (
            IsVariant<paths::A>+
            IsVariant<paths::B>+
            IsVariant<paths::C>+
            IntoVariantFieldMut< strings::A, strings::a, Ty= (u8,u8) >+
            GetVariantField< strings::A, strings::b, Ty= (u8,u16) >+
            GetVariantFieldMut< strings::A, strings::c, Ty= (u8,u32) >+
            IntoVariantField< strings::A, strings::d, Ty= (u8,u64) >+
            IntoVariantFieldMut< strings::A, strings::e, Ty= (u16,u8) >+

            IntoVariantFieldMut< strings::AOpt, strings::a, Ty= Option<(u8,u8)> >+
            GetVariantField< strings::AOpt, strings::b, Ty= Option<(u8,u16)> >+
            GetVariantFieldMut< strings::AOpt, strings::c, Ty= Option<(u8,u32)> >+
            IntoVariantField< strings::AOpt, strings::d, Ty= Option<(u8,u64)> >+
            IntoVariantFieldMut< strings::AOpt, strings::e, Ty= Option<(u16,u8)> >+

            IntoVariantFieldMut< strings::B, strings::a, Ty= i8 >+
            GetVariantField< strings::B, strings::b, Ty= i16 >+
            GetVariantField< strings::C, strings::a, Ty= u8 >+
            IntoVariantField< strings::C, strings::b, Ty= u16 >+
            IntoFieldMut< paths::a, Ty= () >
        )
    }
}

mod exhaustive_enums {
    use super::*;

    field_path_aliases! {
        mod paths{
            A,B,C,
        }
    }

    tstr_aliases! {
        mod strings{
            n0="0",
            n1="1",
            n2="2",
            n3="3",
        }
    }

    macro_rules! exhaustive_enum_no_args_test {
        (
            mod $module:ident
            variants( $($variant:ident),* )
            variant_count_str=$variant_count_str:ident
        ) => {
            mod $module{
                use super::*;

                structural_alias!{
                    #[struc(exhaustive_enum)]
                    trait Foo{
                        $($variant{},)*
                    }
                }

                assert_equal_bounds! {
                    trait Dummy0,
                    (Foo),
                    (
                                    $( IsVariant<paths::$variant>+ )*
                        VariantCount<Count=strings::$variant_count_str>
                    )
                }
            }
        };
    }

    exhaustive_enum_no_args_test! {
        mod exhaustive_0
        variants()
        variant_count_str=n0
    }
    exhaustive_enum_no_args_test! {
        mod exhaustive_1
        variants(A)
        variant_count_str=n1
    }
    exhaustive_enum_no_args_test! {
        mod exhaustive_2
        variants(A,B)
        variant_count_str=n2
    }
    exhaustive_enum_no_args_test! {
        mod exhaustive_3
        variants(A,B,C)
        variant_count_str=n3
    }

    macro_rules! exhaustive_enum_with_args_test {
        (
            mod $module:ident
            $( exhaustive_enum_args $ee_arguments:tt )?
            trait $trait_:ident
            trait $nonex_trait:ident
            variants( $($variant:ident),* )
            variant_count_str=$variant_count_str:ident
        ) => {
            mod $module{
                use super::*;

                structural_alias!{
                    #[struc(and_exhaustive_enum $( $ee_arguments )? )]
                    trait $trait_{
                        $($variant{},)*
                    }
                }

                assert_equal_bounds! {
                    trait Dummy0,
                    ($trait_),
                    (
                                    $( IsVariant<paths::$variant>+ )*
                    )
                }

                assert_equal_bounds! {
                    trait Dummy1,
                    ($nonex_trait),
                    (
                        $trait_+
                        VariantCount<Count=strings::$variant_count_str>
                    )
                }
            }
        };
    }

    exhaustive_enum_with_args_test! {
        mod argless_and_exhaustive_2
        trait Foo
        trait Foo_Exhaustive
        variants( A,B )
        variant_count_str=n2
    }

    exhaustive_enum_with_args_test! {
        mod empty_args_and_exhaustive_2
        exhaustive_enum_args()
        trait Foo
        trait Foo_Exhaustive
        variants( A,B )
        variant_count_str=n2
    }

    exhaustive_enum_with_args_test! {
        mod suffix_arg_and_exhaustive_1
        exhaustive_enum_args( suffix="_Wha" )
        trait Foo
        trait Foo_Wha
        variants( A )
        variant_count_str=n1
    }

    exhaustive_enum_with_args_test! {
        mod name_arg_and_exhaustive_1
        exhaustive_enum_args( name="Bar" )
        trait Foo
        trait Bar
        variants( A )
        variant_count_str=n1
    }
}

mod tuple_and_unit_variants {
    use super::*;

    field_path_aliases! {
        mod paths{
            A,
        }
    }

    tstr_aliases! {
        mod strings{
            n0="0",
            n1="1",
            n2="2",
            n3="3",
            n4="4",
            n5="5",
            n6="6",
            A,
        }
    }

    structural_alias! {
        pub trait Tuple0{
            A()
        }

        pub trait Tuple1{
            ref A(u8)
        }

        pub trait Tuple2{
            mut A(u8,ref Option<u16>)
        }
        pub trait Tuple5{
            A(
                u8,
                ref u16,
                mut u32,
                move u64,
                mut move i8,
                mut move Option<i16>,
            ),
        }

        pub trait Unit1{
            A,
        }


    }

    assert_equal_bounds! {
        trait Dummy0,
        (Tuple0),
        (
            IsVariant<paths::A>+
        )
    }

    assert_equal_bounds! {
        trait Dummy1,
        (Tuple1),
        (
            IsVariant<paths::A>+
            GetVariantField<strings::A,strings::n0, Ty=u8>
        )
    }

    assert_equal_bounds! {
        trait Dummy2,
        (Tuple2),
        (
            IsVariant<paths::A>+
            GetVariantFieldMut<strings::A,strings::n0, Ty=u8>+
            GetVariantField<strings::A,strings::n1, Ty=Option<u16>>+
        )
    }
    assert_equal_bounds! {
        trait Dummy5,
        (Tuple5),
        (
            IsVariant<paths::A>+
            IntoVariantFieldMut<strings::A,strings::n0, Ty=u8>+
            GetVariantField<strings::A,strings::n1, Ty=u16>+
            GetVariantFieldMut<strings::A,strings::n2, Ty=u32>+
            IntoVariantField<strings::A,strings::n3, Ty=u64>+
            IntoVariantFieldMut<strings::A,strings::n4, Ty=i8>+
            IntoVariantFieldMut<strings::A,strings::n5, Ty=Option<i16>>+
        )
    }
    assert_equal_bounds! {
        trait UnitDummy5,
        (Unit1),
        (
            IsVariant<paths::A>
        )
    }
}

mod with_defaulted_items {
    use super::*;

    structural_alias! {
        pub trait Foo{
            fn hi()->u32{
                101
            }

            A{},

            const FOO:&'static str="what";

            ref a:u32,
        }
    }

    #[derive(Structural)]
    enum G {
        A,
    }

    impl FieldType<TS!(a)> for G {
        type Ty = u32;
    }
    impl GetField<TS!(a)> for G {
        fn get_field_(&self, _: TS!(a)) -> &u32 {
            &404
        }
    }

    fn with_foo<T>(this: &T)
    where
        T: Foo,
    {
        assert_eq!(G::hi(), 101);
        assert_eq!(G::FOO, "what");

        assert!(this.is_variant(fp!(A)));
        assert_eq!(this.field_(fp!(a)), &404);
    }

    #[test]
    fn defaulted_items() {
        with_foo(&G::A);
    }
}