structural 0.4.3

Field accessor traits,and emulation of structural types.
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
/*!

The Structural derive macro implements the Structural trait, as well as accessor traits.

The accessor traits are
 [`GetField`]/[`GetFieldMut`]/[`IntoField`] for structs,
and [`GetVariantField`]/[`GetVariantFieldMut`]/[`IntoVariantField`] for enums.

Every instance of `<DerivingType>` in the documentation is the name of the type.
If have a `Money` type,`<DerivingType>_Foo` means `Money_Foo`.

# Enums

For complementary documentation on using the `Structural` derive macro with enums
[look here](../enums/index.html)

# Default Behavior for Structs

By default,this derive generates:

- Implementation of the structural trait for the deriving type,
with documentation describing all the accessor trait impls for the type.

- Implementations of the accessor traits ([`GetField`]/[`GetFieldMut`]/[`IntoField`])
for pub fields.

- Implementation of the [`DropFields`] trait,
if the type has by-value accessors (implements [`IntoField`]) for any field.

- A trait named `<DerivingType>_SI`,aliasing the accessor traits for the type,
implemented for all types with the same accessor trait impls.

- A trait named `<DerivingType>_VSI`,
to use the struct (or any other struct that implements the same accessor traits)
in a newtype variant,by annotating the variant with
`#[struc(newtype(bounds="<DerivingType>_VSI<@variant>"))]`.

Many of these can be overriden.

[`GetField`]: ../../field/trait.GetField.html
[`GetFieldMut`]: ../../field/trait.GetFieldMut.html
[`IntoField`]: ../../field/trait.IntoField.html
[`GetVariantField`]: ../../field/trait.GetVariantField.html
[`GetVariantFieldMut`]: ../../field/trait.GetVariantFieldMut.html
[`IntoVariantField`]: ../../field/trait.IntoVariantField.html
[`TStr`]: ../../struct.TStr.html

[`StructuralExt::into_fields`]: ../../trait.StructuralExt.html#method.into_fields

[`StrucWrapper::vals`]: ../../struct.StrucWrapper.html#method.vals

[`DropFields`]: ../../field/ownership/trait.DropFields.html

[`DropFields::drop_fields`]:
../../field/ownership/trait.DropFields.html#tymethod.drop_fields

[`DropFields::pre_move`]:
../../field/ownership/trait.DropFields.html#tymethod.pre_move

[`PrePostDropFields`]: ../../field/ownership/trait.PrePostDropFields.html

[`PrePostDropFields::pre_drop`]:
../../field/ownership/trait.PrePostDropFields.html#method.pre_drop

[`PrePostDropFields::post_drop`]:
../../field/ownership/trait.PrePostDropFields.html#method.post_drop

<span id="drop-behavior-section"></span>
# Drop behavior

When converting a type into multiple fields by value
(using [`StructuralExt::into_fields`] or [`StrucWrapper::vals`]),
the regular destructor (`Drop::drop`) won't run,
instead the steps [in the section below](#drop-order-section) will happen.

If your type has code that must run when it's dropped,
you can use the [`#[struc(pre_move="foo_function")]`](#strucpre_move) attribute to
run that code before the type is converted into multiple fields by value,

For an example of using the `#[struc(pre_move="foo")]` attribute,[look here](#with-pre-move).

<span id="drop-order-section"></span>
### Drop order

The order of operations when invoking [`StructuralExt::into_fields`] or [`StrucWrapper::vals`]
is this by default:

- Call [`DropFields::pre_move`].

- Move out the fields.

- Call [`PrePostDropFields::pre_drop`].

- Drop the public fields (those with accessor impls) that weren't moved out,in declaration order.

- Drop the private fields(fields that don't have accessor impls),in declaration order.

- Call [`PrePostDropFields::post_drop`].

- Return the moved out fields

# Container Attributes

### `#[struc(debug_print)]`

Prints the output of the derive macro by panicking.

### `#[struc(bound="T:Trait")]`

Adds a bound to every accessor trait impl.

### `#[struc(no_trait)]`

Disables the generation of the `*SI` traits.

[Here is an example using this attribute](#disabling-the-trait-alias)

### `#[struc(no_docs)]`

Removes the docs for the generated traits,and impl of `Structural`.

The documentation describes variants and fields that the accessor trait impls represent.

### `#[struc(from_structural)]`

Causes the [`FromStructural`] and [`TryFromStructural`] traits to be derived.

The trait impls require that the converted-from type has
by-value accessors with the same names and type as this type's public fields.

Private fields must be annotated with one of the [`#[struc(init_*)]`](#init-attributes)
attributes.

### `#[non_exhaustive]`

This is only usable on enums.

The structural macro recognizes this built-in attribute,
and marks the enum as having non-exhaustive variants,
meaning that:

- You will not be able to exhaustively match on it with the `switch` macro

- It will not implement the `VariantCount` trait.

- It will not have a `*_ESI` trait generated for it.

- Using the `#[struc(variant_count_alias)]` attribute will cause an error.


### `#[struc(variant_count_alias)]`

This is only usable on enums.

This generates a type alias with the amount of variants in the enum.

Small example:<br>
For this enum:`pub enum Foo{Bar,Baz}`<br>
This macro would generate:`pub type Foo_VC=TS!(2);`<br>
As well as documentaion explaining what the alias is.

### `#[struc(pre_move="foo")]`

This only has an effect for types with at least one by-value accessor impl.

Changes the implementation of [`DropFields::pre_move`] to run the `foo` function,
allowing custom code to run before the type is converted into multiple fields by value.

For enums: `foo` is called before calling `DropFields::pre_move` on
the delegated-to field in newtype variants.

For an example of using the `#[struc(pre_move="foo")]` attribute,[look here](#with-pre-move).

For a reference on the what happens when converting a type into multiple fields by value
[go here](#drop-behavior-section).

### `#[struc(pre_post_drop_fields)]`

This only has an effect for types with at least one by-value accessor impl.

Changes the implementation of [`DropFields`] to run
[`PrePostDropFields::pre_drop`] before and [`PrePostDropFields::post_drop`]
after the non-moved-out fields are dropped in [`DropFields::drop_fields`].

This requires a manual implementation of [`PrePostDropFields`].

For a reference on the what happens when converting a type into multiple fields by value
[go here](#drop-behavior-section).

# Variant Attributes

### `#[struc(rename="<new_name>")]`

Changes the name for the variant in the accessor trait impls.

The name can be anything,including non-ascii identifiers.

[For an example of renaming variants to non-ascii identifiers look here](#non-ascii-idents)

### `#[struc(replace_bounds="bounds")]`

Replaces (in the generated trait) the bounds for this particular variant with
the ones in the attribute.

All `@variant` in the bounds will be replaced with a [`TStr`] containing the
name of the variant(eg: `TS!(Foo)` for the `Foo` variant ),

### `#[struc(newtype)]`

Marks a variant as a newtype variant,
delegating access to fields in the variant to the single field of the variant.

This attribute can have an optional argument:

- `#[struc(newtype(bounds="Baz_VSI<'a,u8,@variant>"))]`:

All `@variant` in the bounds will be replaced with a [`TStr`] containing the
name of the variant(eg: `TS!(Foo)` for the `Foo` variant ),

Example:`#[struc(newtype(bounds = "Foo_VSI<@variant>"))]` <br>
Example:`#[struc(newtype(bounds = "Bar_VSI<T,U,@variant>"))]` <br>
Example:`#[struc(newtype(bounds = "Baz_VSI<'a,u8,@variant>"))]` <br>

# Field Attributes

### `#[struc(rename="<new_name>")]`

Changes the name for the field in the accessor trait impls.

The name can be anything,including non-ascii identifiers.

[For an example of renaming fields to non-ascii identifiers look here](#non-ascii-idents)

### `#[struc(impl="<trait bounds>")]`

This requires the `nightly_impl_fields` cargo feature
(or `impl_fields` if associated type bounds stabilized after the latest release).

Changes the generated `*SI` traits (which aliases the accessor traits for this type)
not to refer to the type of this field,
instead it will be required to implement the bounds passed to this attribute.

Note that these bounds are only added to the generated `*SI` traits.

[Here is the example for this attribute.](#impl-trait-fields)

### `#[struc(delegate_to)]`

This can only be used with structs.

Delegates the implementation of the Structural and accessor traits to this field.

You can only delegate the implementation and Structural and accessor traits
to a single field.

Using this attribute will disable the generation of traits.

Optional arguments for `delegate_to`:

- `bound="T:bound"`: Adds the constraint to all the trait impls.
- `mut_bound="T:bound"`: Adds the constraint to the `GetField` impl.
- `into_bound="T:bound"`: Adds the constraint to the `IntoField` impl.

<span id="init-attributes"></span>

### `#[struc(init_with_fn = "<callable_expression>")]`

This can only be used in combination with the `#[struc(from_structural)]` container attribute.

Initialize the field in the `FromStructural` impl  with the return value of
calling the `<callable_expression>` expression.

The `init_*` attributes are necessary when deriving `FromStructural` in structs
with private fields.

Public fields with `init_*` attributes aren't added as bounds for the converted-from type,
which means that removeing the `init_*` attributes is a breaking change.

Examples:<br>
- `#[struc(init_with_fn = "foo::bar")]` <br>
- `#[struc(init_with_fn = r#"|| expensive(100, "hello") "#)]` <br>

### `#[struc(init_with_val = "<expression>")]`

This can only be used in combination with the `#[struc(from_structural)]` container attribute.

Initialize the field in the `FromStructural` impl  with the `<expression>` expression.

[`init_with_lit`](#init_with_lit) is better to initialize the field with a string literal,
since this attribute requires escaping the string literal, or using raw strings.

The `init_*` attributes are necessary when deriving `FromStructural` in structs

Public fields with `init_*` attributes aren't added as bounds for the converted-from type,
which means that removeing the `init_*` attributes is a breaking change.
with private fields.

Examples:<br>
- `#[struc(init_with_val = "()")]`<br>
- `#[struc(init_with_val = "100")]`<br>
- `#[struc(init_with_val = r"100")]`<br>
- `#[struc(init_with_val = "()")]`<br>

<span id="init_with_lit"></span>
### `#[struc(init_with_lit = <literal>)]`

This can only be used in combination with the `#[struc(from_structural)]` container attribute.

Initialize the field in the `FromStructural` impl  with the `<literal>` literal.
Note that only literals parseable as `syn::Lit` can be used here,
numbers, strings, bools, etc.

The `init_*` attributes are necessary when deriving `FromStructural` in structs
with private fields.

Public fields with `init_*` attributes aren't added as bounds for the converted-from type,
which means that removeing the `init_*` attributes is a breaking change.

Examples:<br>
- `#[struc(init_with_lit = "foo")]`<br>
- `#[struc(init_with_lit = 100)]`<br>


### `#[struc(init_with_default)]`

This can only be used in combination with the `#[struc(from_structural)]` container attribute.

Initialize the field with its default value, requires the field type to implement `Default`.

The `init_*` attributes are necessary when deriving `FromStructural` in structs
with private fields.

Public fields with `init_*` attributes aren't added as bounds for the converted-from type,
which means that removeing the `init_*` attributes is a breaking change.

# Container/Variant/Field Attributes

Unless stated otherwise,
when these attributes are put on the container or variant it will have the same effect as
being put on the field,and are overriden by attributes directly on the field.

### `#[struc(access="")]`

Changes the implemented accessor traits for the field(s).

`#[struc(access="ref")]`:
Generates impls of the `GetField` trait for the field(s).

`#[struc(access="mut")]`:
Generates impls of the `GetField`+`GetFieldMut` traits for the field(s).

`#[struc(access="move")]`:
Generates impls of the `GetField`+`IntoField` traits for the field(s).

`#[struc(access="mut move")]`:
Generates impls of the `GetField`+`GetFieldMut`+`IntoField` traits for the field(s).

When this attribute is used on a non-pub field,
it'll mark the field as public for the purpose of generating accessor trait impls.

When these attribute are used on enums it generates impls for the
`GetVariantField`/`GetVariantFieldMut`/`IntoVariantField`
traits instead of `GetField`/`GetFieldMut`/`IntoField`.

# Container/Field Attributes

Unless stated otherwise,
when these attributes are put on the container it will have the same effect as
being put on the field,and are overriden by attributes directly on the field.

### `#[struc(public)]`

Marks the fields as public,generating the accessor traits for the field.

### `#[struc(not_public)]`

Marks the fields as private,not generating the accessor traits for the field.


# Examples


### Accessing Fields

This example shows many of the ways that fields can be accessed.

```
use structural::{StructuralExt,Structural,fp};

fn main(){
    let array=[
        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,30,31,
    ];

    with_struct(Foo{
        name: "foo",
        year: 2020,
        tuple: Some((3,5,8)),
        array,
    });

    with_struct(Bar{
        name: "foo",
        surname:"metavariable",
        year: 2020,
        tuple: Some((3,5,8)),
        array,
    });
}

fn with_struct<This>(mut foo:This)
where
    This: Foo_SI + Clone,
{
    ////////////////////////////////////////////////////
    ////            field_ method
    assert_eq!( foo.field_(fp!(name)), &"foo" );
    assert_eq!( foo.field_(fp!(year)), &2020 );

    assert_eq!( foo.field_(fp!(tuple)), &Some((3,5,8)) );
    assert_eq!( foo.field_(fp!(tuple?)), Some(&(3,5,8)) );
    assert_eq!( foo.field_(fp!(tuple?.0)), Some(&3) );
    assert_eq!( foo.field_(fp!(tuple?.1)), Some(&5) );
    assert_eq!( foo.field_(fp!(tuple?.2)), Some(&8) );

    ////////////////////////////////////////////////////
    ////            field_mut method
    assert_eq!( foo.field_mut(fp!(name)), &mut "foo" );
    assert_eq!( foo.field_mut(fp!(year)), &mut 2020 );

    assert_eq!( foo.field_mut(fp!(tuple)), &mut Some((3,5,8)) );
    assert_eq!( foo.field_mut(fp!(tuple?)), Some(&mut (3,5,8)) );
    assert_eq!( foo.field_mut(fp!(tuple?.0)), Some(&mut 3) );
    assert_eq!( foo.field_mut(fp!(tuple?.1)), Some(&mut 5) );
    assert_eq!( foo.field_mut(fp!(tuple?.2)), Some(&mut 8) );


    ////////////////////////////////////////////////////
    ////            into_field method
    assert_eq!( foo.clone().into_field(fp!(name)), "foo" );
    assert_eq!( foo.clone().into_field(fp!(year)), 2020 );

    assert_eq!( foo.clone().into_field(fp!(tuple)), Some((3,5,8)) );
    assert_eq!( foo.clone().into_field(fp!(tuple?)), Some((3,5,8)) );
    assert_eq!( foo.clone().into_field(fp!(tuple?.0)), Some(3) );
    assert_eq!( foo.clone().into_field(fp!(tuple?.1)), Some(5) );
    assert_eq!( foo.clone().into_field(fp!(tuple?.2)), Some(8) );

    ////////////////////////////////////////////////////
    ////            fields method
    assert_eq!( foo.fields(fp!(name, year)), (&"foo",&2020) );
    assert_eq!( foo.fields(fp!(=>name,year)), (&"foo",&2020) );

    // Where you place the `?`  matters,
    // if it's after the `=>`,it returns an `Option` for every single field.
    assert_eq!(
        foo.fields(fp!(tuple=> ?.0, ?.1, ?.2)),
        (Some(&3),Some(&5),Some(&8))
    );
    // If the `?` is before the `=>`,
    // it returns an `Option` wrapping all references to the fields.
    assert_eq!( foo.fields(fp!(tuple?=>0,1,2)), Some((&3,&5,&8)) );

    assert_eq!(
        foo.fields(fp!(array=>0,1,2,3,4,5,6,7)),
        (&10,&11,&12,&13,&14,&15,&16,&17),
    );

    // If you access more than 8 fields,the fields method returns tuples of tuples,
    // in which the nested tuples reference 8 fields each.
    assert_eq!(
        foo.fields(fp!(array=>0,1,2,3,4,5,6,7,8)),
        (
            (&10,&11,&12,&13,&14,&15,&16,&17),
            (&18,),
        )
    );
    assert_eq!(
        foo.fields(fp!(array=>0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)),
        (
            (&10,&11,&12,&13,&14,&15,&16,&17),
            (&18,&19,&20,&21,&22,&23,&24,&25),
            (&26,&27),
        )
    );


    ////////////////////////////////////////////////////
    ////            fields_mut method
    assert_eq!( foo.fields_mut(fp!(name, year)), (&mut "foo",&mut 2020) );
    assert_eq!( foo.fields_mut(fp!(=>name,year)), (&mut "foo",&mut 2020) );

    assert_eq!(
        foo.fields_mut(fp!(tuple=> ?.0, ?.1, ?.2)),
        (Some(&mut 3),Some(&mut 5),Some(&mut 8))
    );
    assert_eq!( foo.fields_mut(fp!(tuple?=>0,1,2)), Some((&mut 3, &mut 5, &mut 8)) );

    assert_eq!(
        foo.fields_mut(fp!(array=>0,1,2,3,4,5,6,7)),
        (&mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17),
    );

    // If you access more than 8 fields,the `fields_mut` method returns tuples of tuples,
    // in which the nested tuples reference 8 fields each.
    assert_eq!(
        foo.fields_mut(fp!(array=>0,1,2,3,4,5,6,7,8)),
        (
            (&mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17),
            (&mut 18,),
        )
    );
    assert_eq!(
        foo.fields_mut(fp!(array=>0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)),
        (
            (&mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17),
            (&mut 18, &mut 19, &mut 20, &mut 21, &mut 22, &mut 23, &mut 24, &mut 25),
            (&mut 26, &mut 27),
        )
    );

    ////////////////////////////////////////////////////
    ////            into_fields method
    assert_eq!( foo.clone().into_fields(fp!(name, year)), ("foo",2020) );
    assert_eq!( foo.clone().into_fields(fp!(=>name,year)), ("foo",2020) );

    assert_eq!( foo.clone().into_fields(fp!(tuple?=>0,1,2)), Some((3, 5, 8)) );

    assert_eq!(
        foo.clone().into_fields(fp!(array=>0,1,2,3,4,5,6,7)),
        (10, 11, 12, 13, 14, 15, 16, 17),
    );

    // If you access more than 8 fields,the `into_fields` method returns tuples of tuples,
    // in which the nested tuples have 8 fields each.
    assert_eq!(
        foo.clone().into_fields(fp!(array=>0,1,2,3,4,5,6,7,8)),
        (
            (10, 11, 12, 13, 14, 15, 16, 17),
            (18,),
        )
    );
    assert_eq!(
        foo.clone().into_fields(fp!(array=>0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)),
        (
            (10, 11, 12, 13, 14, 15, 16, 17),
            (18, 19, 20, 21, 22, 23, 24, 25),
            (26, 27),
        )
    );


}

#[derive(Structural,Clone)]
#[struc(public)]
struct Foo{
    name: &'static str,
    year: i64,
    tuple: Option<(u32,u32,u32)>,
    array: [u8;32],
}

#[derive(Structural,Clone)]
# #[struc(no_trait)]
#[struc(public)]
struct Bar{
    name:&'static str,
    surname:&'static str,
    year:i64,
    tuple: Option<(u32,u32,u32)>,
    array: [u8;32],
}


```

### Basic example

```rust
use structural::{Structural,StructuralExt,structural_alias,fp};


fn reads_pair<O>(pair:&O)
where
    // This uses the trait generated by `#[derive(Structural)]`,
    // aliasing the accessor traits implemented for `Hello`,
    // allowing any type with (at least) those fields to be passed here.
    O:Hello_SI
{
    let (a,b)=pair.fields(fp!( a, b ));
    assert_eq!(a,&11);
    assert_eq!(b,&33);
}


#[derive(Debug,Structural,PartialEq,Eq)]
#[struc(public)]
struct Hello{
    a:u32,
    b:u32
}

#[derive(Structural)]
#[struc(access="mut move")]
#[struc(public)]
struct World{
    run:String,
    a:u32,
    b:u32,
}

fn main(){
    reads_pair(&Hello{ a:11, b:33 });

    reads_pair(&World{ run:"nope".into(), a:11, b:33 });
}


```

### Mutating fields

```rust
use structural::{Structural,StructuralExt,structural_alias,fp};


structural_alias!{
    trait Tuple2<T>{
        0:T,
        1:T,
    }
}


fn mutates_pair<O>(pair:&mut O)
where
    O:Tuple2<u32>
{
    let a=pair.field_mut(fp!(0));
    assert_eq!(a,&mut 14);
    *a*=2;

    let b=pair.field_mut(fp!(1));
    assert_eq!(b,&mut 16);
    *b*=2;
}


#[derive(Debug,Structural,PartialEq,Eq)]
struct Point(
    #[struc(public)]
    u32,

    #[struc(public)]
    u32,

    #[struc(not_public)]
    pub u32,
);

fn main(){
    let mut point=Point(14,16,11);
    let mut tuple=(14,16);

    mutates_pair(&mut point);
    mutates_pair(&mut tuple);

    assert_eq!(point,Point(28,32,11));
    assert_eq!(tuple,(28,32));
}

```

### Disabling the trait alias

This example demonstrates how one disables the generation of the
`<DerivingType>_SI` trait to declare it manually.

```rust
use structural::{Structural,IntoFieldMut,StructuralExt,FP};

#[derive(Debug,Structural,PartialEq,Eq)]
#[struc(no_trait)]
#[struc(access="mut move")]
struct Hello{
    pub hello:u32,
    pub world:String,
}


pub trait Hello_SI:
    IntoFieldMut<FP!(hello), Ty=u32>+
    IntoFieldMut<FP!(world), Ty=String>
{}

impl<T> Hello_SI for T
where
    T:?Sized+
        IntoFieldMut<FP!(hello), Ty=u32>+
        IntoFieldMut<FP!(world), Ty=String>
{}

```

### Impl trait fields

This is an example of using the `#[struc(impl="<trait_bounds>")]` attribute

This requires the `nightly_impl_fields` cargo feature
(or `impl_fields` if associated type bounds stabilized after the latest release).

*/
#![cfg_attr(not(feature="nightly_impl_fields"),doc="```ignore")]
#![cfg_attr(feature="nightly_impl_fields",doc="```rust")]
/*!

// Remove this if associated type bounds (eg: `T: Iterator<Item: Debug>`)
// work without it.
#![feature(associated_type_bounds)]

use std::borrow::Borrow;

use structural::{Structural,fp,make_struct,StructuralExt};


#[derive(Structural)]
#[struc(public)]
struct Person{
    #[struc(impl="Borrow<str>")]
    name:String,

    #[struc(impl="Copy+Into<u64>")]
    height_nm:u64
}

/// `Person_SI` was generated for `Person` by the `Structural` derive macro.
fn takes_person(this:&impl Person_SI){
    let (name,height)=this.fields(fp!(name,height_nm));
    assert_eq!( name.borrow(), "bob" );

    assert_eq!( (*height).into(), 1_500_000_000 );
}


// Notice how `name` is a `&'static str` instead of a `String`,
// and `height_nm` is a `u32` instead of a `u64`?
// This is possible because the concrete types of the fields weren't used in
// the `Person_SI` trait.
takes_person(&make_struct!{
    name:"bob",
    height_nm: 1_500_000_000_u32,
});

takes_person(&Person{
    name:"bob".to_string(),
    height_nm: 1_500_000_000_u64,
});

```

### Delegation

This is an example of using the `#[struc(delegate_to)]` attribute.

```
use structural::{fp,make_struct,StructuralExt,Structural};


#[derive(Structural,Clone)]
struct Foo<T>{
    #[struc(delegate_to)]
    value:T
}

# // ensuring that Foo_SI wasn't generated
# trait Foo_SI{}

#[derive(Structural,Clone)]
#[struc(public,access="ref")]
struct AnimalCounts{
    cows:u32,
    chickens:u32,
    pigs:u32,
}


fn total_count(animals:&dyn AnimalCounts_SI)->u64{
    *animals.field_(fp!(cows)) as u64+
    *animals.field_(fp!(chickens)) as u64+
    *animals.field_(fp!(pigs)) as u64
}

{
    let count=total_count(&Foo{
        value:make_struct!{
            cows:100,
            chickens:200,
            pigs:300,
        }
    });

    assert_eq!( count, 600 );
}

{
    let count=total_count(&Foo{
        value:AnimalCounts{
            cows:0,
            chickens:500,
            pigs:0,
        }
    });

    assert_eq!( count, 500 );
}

{
    let count=total_count(&AnimalCounts{
        cows:0,
        chickens:500,
        pigs:1_000_000_000,
    });

    assert_eq!( count, 1_000_000_500 );
}





```

### Delegation,with bounds

This is an example of using the `#[struc(delegate_to())]` attribute with
extra bounds in the accessor trait impls.

```
use structural::{fp,make_struct,StructuralExt,Structural};

use std::{
    fmt::Debug,
    ops::Add,
};


#[derive(Structural,Debug,Copy,Clone,PartialEq)]
struct Foo<T>{
    #[struc(delegate_to(
        bound="T:PartialEq",
        mut_bound="T:Copy",
        into_bound="T:Debug",
    ))]
    value:T
}

#[derive(Structural,Debug,Copy,Clone,PartialEq)]
#[struc(public)]
struct AnimalCounts<T>{
    cows:T,
    chickens:T,
    pigs:T,
}

fn total_count<T>(animals:&dyn AnimalCounts_SI<T>)->T
where
    T: Clone+Add<Output=T>,
{
    let (a,b,c)=animals.cloned_fields(fp!( cows, chickens, pigs ));
    a + b + c
}

{
    let count=total_count(&Foo{
        value:AnimalCounts{
            cows:100,
            chickens:200,
            pigs:300,
        }
    });

    assert_eq!( count, 600 );
}

// This doesn't compile because
// AddableString doesn't satisfy the Copy bound added by `mut_bound="T:Copy"`
/*
{
    let count=total_count(&Foo{
        value: AnimalCounts::<AddableString> {
            cows: "foo".into(),
            chickens: "bar".into(),
            pigs: "baz".into(),
        }
    });

    assert_eq!( count.0, "foobarbaz" );
}
*/


#[derive(Debug,Clone,PartialEq)]
struct AddableString(String);

impl<'s> From<&'s str> for AddableString{
    fn from(s:&'s str)-> AddableString {
        AddableString( s.to_string() )
    }
}

impl Add for AddableString{
    type Output=Self;

    fn add(self,other:Self)->Self{
        AddableString( self.0 + other.0.as_str() )
    }
}

```

### Non-ascii idents

This is an example of using non-ascii identifiers.

Unfortunately,without enabling the "use_const_str" feature to use const generics internally,
compile-time errors are significantly less readable than with ascii identifiers.

```rust
use structural::{fp,make_struct,StructuralExt,Structural};

////////////////////////////////////////////////////
//                    structs

#[derive(Structural)]
#[struc(public)]
struct Family{
    #[struc(rename="儿子数")]
    sons: u32,
    #[struc(rename="女儿们")]
    daughters: u32,
}

let mut this=Family{
    sons: 34,
    daughters: 55,
};

assert_eq!( this.fields(fp!("儿子数","女儿们")), (&34,&55) );
assert_eq!( this.fields_mut(fp!("儿子数","女儿们")), (&mut 34,&mut 55) );

////////////////////////////////////////////////////
//                    Enums

#[derive(Structural)]
enum Vegetable{
    #[struc(rename="Ziemniak")]
    Potato{
        #[struc(rename="centymetry objętości")]
        volume_cm: u32,
    },
    #[struc(rename="生菜")]
    Letuce{
        #[struc(rename="树叶")]
        leaves: u32,
    }
}

let mut potato=Vegetable::Potato{ volume_cm: 13 };
let mut letuce=Vegetable::Letuce{ leaves: 21 };

assert_eq!( potato.field_(fp!(::"Ziemniak"."centymetry objętości")), Some(&13) );
assert_eq!( potato.field_(fp!(::"生菜"."树叶")), None );

assert_eq!( letuce.field_(fp!(::"Ziemniak"."centymetry objętości")), None );
assert_eq!( letuce.field_(fp!(::"生菜"."树叶")), Some(&21) );

assert_eq!( potato.field_mut(fp!(::"Ziemniak"."centymetry objętości")), Some(&mut 13) );
assert_eq!( potato.field_mut(fp!(::"生菜"."树叶")), None );

assert_eq!( letuce.field_mut(fp!(::"Ziemniak"."centymetry objętości")), None );
assert_eq!( letuce.field_mut(fp!(::"生菜"."树叶")), Some(&mut 21) );

```

<span id="with-pre-move"></span>
### Using the `#[struc(pre_move="foo")]` attribute

This demonstrates how you can run code when converting a type into multiple fields by value.

```rust
use structural::{fp, StructuralExt, Structural};

{
    let mut vector=Vec::new();
    drop(WithDropLogic{
        vector: &mut vector,
        x: 0,
        y: 0,
        z: 0
    });
    // The vector was written to in WithDropLogic's impl of `Drop::drop`
    assert_eq!(vector, [1001]);
}
{
    let mut vector=Vec::new();
    let this=WithDropLogic{
        vector: &mut vector,
        x: 3,
        y: 5,
        z: 8
    };
    assert_eq!(into_xyz(this), (3, 5, 8));
    // The vector was written to in `WithDropLogic::drop_`,
    // because `DropFields::pre_move` delegates to it.
    // (`WithDropLogic::drop_` was passed to the `#[struc(pre_move="...")]` attribute)
    assert_eq!(vector, [1001]);
}
{
    let this=Variables{
        x: 13,
        y: 21,
        z: 34,
    };
    assert_eq!(into_xyz(this), (13, 21, 34));
}

// The `Variables_SI` trait was generated by the `Structural` derive on `Variables`,
// aliasing its accessor trait impls.
fn into_xyz(this: impl Variables_SI)->(u32,u32,u32) {
    this.into_fields(fp!(x,y,z))
}


#[derive(Structural)]
struct Variables{
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

#[derive(Structural)]
# #[struc(no_trait)]
#[struc(pre_move="WithDropLogic::drop_")]
struct WithDropLogic<'a>{
    vector: &'a mut Vec<u32>,
    pub x: u32,
    pub y: u32,
    pub z: u32,
}

impl WithDropLogic<'_>{
    fn drop_(&mut self){
        self.vector.push(1001);
    }
}

impl Drop for WithDropLogic<'_>{
    fn drop(&mut self){
        self.drop_();
    }
}


```

[`FromStructural`]: ../../convert/trait.FromStructural.html
[`TryFromStructural`]: ../../convert/trait.TryFromStructural.html


*/