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
/*!
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.
- 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>"))]`.
All 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
# 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.
### `#[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.
# 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 `<DerivingType>_SI` trait (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 `<DerivingType>_SI` trait.
[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.
# 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.
# 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(){
with_struct(Foo{
name: "foo",
year: 2020,
tuple: Some((3,5,8)),
});
with_struct(Bar{
name: "foo",
surname:"metavariable",
year: 2020,
tuple: Some((3,5,8)),
});
}
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)) );
////////////////////////////////////////////////////
//// 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)) );
}
#[derive(Structural,Clone)]
#[struc(public)]
struct Foo{
name: &'static str,
year: i64,
tuple: Option<(u32,u32,u32)>,
}
#[derive(Structural,Clone)]
# #[struc(no_trait)]
#[struc(public)]
struct Bar{
name:&'static str,
surname:&'static str,
year:i64,
tuple: Option<(u32,u32,u32)>,
}
```
### 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).
*/
/*!
// 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) );
```
*/