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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
/**
This macro allows delegating the implementation of the accessor traits.

This macro delegates the implementation of those traits for all fields,
it doesn't provide a way to do so for only a list of fields.

# Safety

In order to use this macro soundly,
you must ensure that side effects (including mutation) do not happen while
getting the delegated-to variable,
and that the delegated-to variable has a consistent value when
methods from `structural` traits are called in a sequence
(with no method calls from non-`structural`-traits in between).

You must ensure that the variable that you delegate `Get*Field` to is the same as the one
you delegate `Get*FieldMut`/`Into*Field` to,
as well as ensuring that there are no other impls of the `Get*FieldMut`/`Into*Field` traits
accessing the same delegated-to variable.

# 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 pass `DropFields= { ..... pre_move = foo_function; }` to this macro
to run that code before the type is converted into multiple fields by value
(using [`StructuralExt::into_fields`] or [`StrucWrapper::vals`]),

<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 fields passed to `dropped_fields[]`, in the order that they were passed.

- Drop the delegated-to variable with [`DropFields::drop_fields`].

- Call [`PrePostDropFields::post_drop`].

- Return the moved out fields

[`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

# Example with all syntax

```rust
use structural::unsafe_delegate_structural_with;

# trait Trait{}
# impl<T> Trait for T{}

struct Foo<T>{
    a: (),
    b: (),
    c: (),
    d: Bar,
    value:T
}

struct Bar{
    e: (),
}

unsafe_delegate_structural_with!{
    impl[T,] Foo<T>
    // This where clause is required syntax
    where[
        T:Trait,
    ]

    // This is the identifier used for `self` in the blocks below.
    self_ident=this;

    // An optional macro argument that tells it how to specialize pointer methods.
    //
    // `specialization_params(Sized);` is the default when this the parameter is not passed,
    // it means that no specialization is used,always requiring `Self:Sized`.
    //
    // This is the default value for the parameter:
    // `specialization_params(Sized);`

    // This means that the type is `?Sized` and not specialization is used,
    // this may be slower in debug builds because this always uses a
    // function pointer call in raw-pointer-taking methods.
    //
    // `specialization_params(?Sized);`

    // This means that the type is `?Sized` by default.
    // The `cfg(anything)` argument enables specialization conditionally,
    //
    // When specialization is disabled theres only a default impl for `Self:?Sized`
    // which may be slower in debug builds,
    // because this uses a function pointer call in raw-pointer methods.
    //
    // When specialization is enabled,the impl is specializes on `Self:Sized`
    // to remove the overhead of raw-pointer methods.
    //
    // `specialization_params(cfg(anything));`


    // This is the type of the variable we delegate to,
    // this is required because Rust doesn't have a `typeof`/`decltype` construct.
    delegating_to_type=T;

    // This block of code is used to get the reference to the delegated-to variable
    // in GetField.
    GetField {
        &this.value
    }

    // This block of code is used to get a mutable reference to the delegated-to variable
    // in GetFieldMut
    //
    // This block must always evaluate to a mutable reference for the same variable,
    // and it must not be the same variable as other implementations of the GetFieldMut trait.
    //
    GetFieldMut
    where [
        // This is an optional where clause
        // The last where predicate must have a trailing comma.
        T:Trait,
    ]{
        &mut this.value
    }
    // This gets a raw mutable pointer to the variable this delegates to.
    as_delegating_raw{
        &mut (*this).value as *mut T
    }

    // This block of code is used to get the delegated-to variable in IntoField::into_field.
    IntoField
    where [
        // This is an optional where clause
        // The last where predicate must have a trailing comma.
        T:Trait,
    ]{
        this.value
    }
    // This block of code is used to get the delegated-to variable in
    // IntoField::move_out_field_.
    move_out_field{
        &mut this.value
    }

    // `DropFields = .... ` determines what happens in the DropFields implementation
    // for the type, which isn't as trivial as delegating to the delegated_to type.
    //
    DropFields = {
        // Which fields are dropped in `DropFields::drop_fields`,
        // in the order that they're dropped.
        //
        // If you're dropping a nested field,it must be wrapped in parentheses(ie:`(a.b.c)`).
        dropped_fields[ a,b,c,(d.e) ]

        // An optional argument for code to run right before fields are moved.
        //
        // `some_function` is called in the implementation of
        // `DropFields::pre_move` generated for this type.
        //
        // The passed in function must have this signature: `fn(&mut Self)`
        // in this example it would be `fn<T: Trait>(&mut Foo<T>)`
        //
        // `pre_move = some_function;`

        // An optional argument which determines whether there's pre/post field-drop logic
        // in `DropFields::drop_fields`.
        //
        // With `pre_post_drop_fields=true;` this inserts
        // a `PrePostDropFields::pre_drop(self)` call before the fields are dropped,
        // and a `PrePostDropFields::post_drop(self)` call after the fields are dropped,
        //
        // To use this the type must implement the `PrePostDropFields` trait
        // to do something before and after the listed fields (and the delegated_to variable)
        // are dropped.
        //
        // This is the default value:
        // `pre_post_drop_fields=false;`

        // Whether to drop the fields in the delegated_to variable,with `DropFields::drop_fields`.
        //
        // With `drop_delegated_drop_variable=true;`,
        // this calls `DropFields::drop_fields` on the delegated to variable.
        //
        // With `drop_delegated_drop_variable=false;`,
        // it does not drop the delegated to variable in an way.
        //
        drop_delegated_to_variable=true;
    }

    // Requires that the DropFields trait is implemented manually.
    // DropFields = manual;

    // This is an optional parameter,
    // which determines how the FromStructural and TryFromStructural traits are delegated.
    //
    // If this parameter is not passed,
    // then neither FromStructural and TryFromStructural is implemented.
    FromStructural
    // An optional where clause, with additional constraints
    where [ u32: Copy, ]
    {
        // An optional parameter, defaults to the delegated-to type.
        converted_from = T;

        // The function that constructs this type from the `converted_from` type.
        constructor = Foo::new;
    }
}


impl<T> Foo<T>{
    pub fn new(value: T)->Self{
        Self{
            a: (),
            b: (),
            c: (),
            d: Bar{e: ()},
            value,
        }
    }
}

```

# Example

This example is of a type wrapping a `ManuallyDrop<T>`,delegating to the `T` inside it.

```rust
use std::{
    fmt::Debug,
    mem::ManuallyDrop,
};

use structural::{StructuralExt,GetFieldMut,unsafe_delegate_structural_with,make_struct,fp};

struct Bar<T>{
    value:ManuallyDrop<T>
}

impl<T> Bar<T>{
    pub fn new(value:T)->Self{
        Self{
            value:ManuallyDrop::new(value)
        }
    }
}


unsafe_delegate_structural_with!{
    // You must write a trailing comma.
    impl[T,] Bar<T>
    where[
        // This requires T to implement Clone
        // for `Bar<T>` to implement the accessor traits
        T:Clone,
    ]
    self_ident=this;
    specialization_params(Sized);
    delegating_to_type=T;

    GetField {
#       // This ensures that the `T:Clone` bound is put on the impl block.
#       T::clone;
        &*this.value
    }

    GetFieldMut
    where [T:Debug,]
    {
#       // This ensures that the `T:Clone+Debug` bounds are put on the impl block.
#       T::clone;
#       <T as Debug>::fmt;
        &mut *this.value
    }
    as_delegating_raw{
        &mut (*this).value as *mut ManuallyDrop<T> as *mut T
    }


    IntoField
    where [T:Debug,]
    {
#       // This ensures that the `T:Clone+Debug` bounds are put on the impl block.
#       T::clone;
#       <T as Debug>::fmt;
        ManuallyDrop::into_inner(this.value)
    }
    move_out_field{
        &mut *this.value
    }

    DropFields = {
        dropped_fields[]
    }
}

{
    let mut bar=Bar::new((2,3,5,8,13));
    assert_eq!(
        bar.fields(fp!(4,3,2,1,0)),
        ( &13, &8, &5, &3, &2 )
    );

    assert_eq!(
        bar.fields_mut(fp!(1,2,3,4)),
        ( &mut 3, &mut 5, &mut 8, &mut 13 )
    );

    assert_eq!(bar.into_field(fp!(1)),3);
}

{
    let mut bar=Bar::new(make_struct!{
        #![derive(Clone,Debug)] //This derives Clone and Debug for the anonymous struct

        a:"hello",
        b:"world",
        c:"tour",
    });
    assert_eq!(
        bar.fields(fp!(a,b,c)),
        ( &"hello", &"world", &"tour" )
    );

    assert_eq!(
        bar.fields_mut(fp!(c,b,a)),
        ( &mut"tour", &mut"world", &mut"hello" )
    );

    assert_eq!( bar.into_field(fp!(c)), "tour" );
}


```


*/
#[macro_export]
macro_rules! unsafe_delegate_structural_with {
    (
        $( #[doc=$doc:expr] )*
        impl $impl_params:tt $self:ty
        where $where_clause:tt
        self_ident=$this:ident;
        $( specialization_params($($raw_mut_impl:tt)*); )?
        delegating_to_type=$delegating_to_type:ty;

        $($rest:tt)*
    ) => (
        $crate::unsafe_delegate_structural_with_inner!{
            $( #[doc=$doc] )*
            impl $impl_params $self
            where $where_clause
            self_ident=$this;
            specialization_params( $( $($raw_mut_impl)* )? );
            delegating_to_type=$delegating_to_type;

            $($rest)*
        }
    )
}

#[macro_export]
#[doc(hidden)]
macro_rules! unsafe_delegate_structural_with_inner {
    (
        $( #[doc=$doc:expr] )*
        impl $impl_params:tt $self:ty
        where $where_clause:tt
        self_ident=$this:ident;
        specialization_params $raw_mut_impl:tt;
        delegating_to_type=$delegating_to_type:ty;

        GetField $get_field_closure:block
        $(
            GetFieldMut
            $( where[ $($mut_where_clause:tt)* ] )?
            $unsafe_get_field_mut_closure:block
            as_delegating_raw $as_field_mutref_closure:block
        )?
        $(
            IntoField
            $( where[ $($into_where_clause:tt)* ] )?
            $into_field_closure:block
            move_out_field $move_out_field_closure:block

            DropFields = $drop_fields_param:tt $(;)?
        )?

        $(
            FromStructural
            $( where $from_struc_where:tt )?
            {
                $(converted_from=$converted_from:ty;)?
                constructor = $constructor:expr;
            }
        )?
    ) => (

        $crate::unsafe_delegate_structural_with_inner!{
            inner-structural;
            $( #[doc=$doc] )*
            impl $impl_params $self
            where $where_clause
            self_ident=$this;
            delegating_to_type=$delegating_to_type;
            GetField $get_field_closure
        }

        $crate::unsafe_delegate_structural_with_inner!{
            inner;
            impl $impl_params $self
            where $where_clause
            self_ident=$this;
            delegating_to_type=$delegating_to_type;
            GetField $get_field_closure
        }

        $(
            $crate::unsafe_delegate_structural_with_inner!{
                inner;
                impl $impl_params $self
                where $where_clause
                where[ $( $($mut_where_clause)* )? ]
                self_ident=$this;
                specialization_params $raw_mut_impl;
                delegating_to_type=$delegating_to_type;

                GetFieldMut $unsafe_get_field_mut_closure
                as_delegating_raw $as_field_mutref_closure
            }
        )?

        $(
            $crate::unsafe_delegate_structural_with_inner!{
                inner;
                impl $impl_params $self
                where $where_clause
                where [ $( $($into_where_clause)* )? ]
                self_ident=$this;
                delegating_to_type=$delegating_to_type;
                IntoField $into_field_closure
                move_out_field $move_out_field_closure
            }

            $crate::unsafe_delegate_structural_with_inner!{
                @drop_fields_impls
                impl $impl_params $self
                where $where_clause
                where [ $( $($into_where_clause)* )? ]
                self_ident=$this;
                delegating_to_type=$delegating_to_type;
                IntoField $into_field_closure
                move_out_field $move_out_field_closure

                DropFields = $drop_fields_param
            }
        )?

        $(
            $crate::unsafe_delegate_structural_with_inner!{
                inner;
                impl $impl_params $self
                where $where_clause
                $(where $from_struc_where)?

                FromStructural {
                    $(converted_from = $converted_from;)?
                    converted_from = $delegating_to_type;
                    constructor = $constructor;
                }
            }
        )?
    );
    (
        inner-structural;
        $( #[doc=$doc:expr] )*
        impl[$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        self_ident=$this:ident;
        delegating_to_type=$delegating_to_type:ty;

        GetField $get_field_closure:block
    )=>{
        $( #[doc=$doc] )*
        impl<$($impl_params)*> $crate::Structural for $self
        where
            $delegating_to_type: $crate::Structural,
            $($where_clause)*
        {}
    };
    (inner;
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        self_ident=$this:ident;
        delegating_to_type=$delegating_to_type:ty;

        GetField $get_field_closure:block
    )=>{
        unsafe impl<$($impl_params)* __V>
            $crate::pmr::IsVariant<$crate::TStr<__V>>
        for $self
        where
            $delegating_to_type: $crate::pmr::IsVariant<$crate::TStr<__V>>,
            $($where_clause)*
        {
            fn is_variant_(&self,name:$crate::TStr<__V>)->bool{
                let $this=self;
                let field:&$delegating_to_type=$get_field_closure;
                $crate::pmr::IsVariant::is_variant_(field,name)
            }
        }

        unsafe impl<$($impl_params)*> $crate::pmr::VariantCount for $self
        where
            $delegating_to_type: $crate::pmr::VariantCount,
            $($where_clause)*
        {
            type Count=$crate::pmr::VariantCountOut<$delegating_to_type>;
        }

        unsafe impl<$($impl_params)* __V,__F,__Ty>
            $crate::GetVariantField<$crate::TStr<__V>,__F>
        for $self
        where
            $delegating_to_type:
                $crate::GetVariantField<$crate::TStr<__V>,__F,Ty=__Ty>,
            $($where_clause)*
        {
            #[inline(always)]
            fn get_vfield_(
                &self,
                vname: $crate::TStr<__V>,
                fname: __F,
            ) -> Option<&$crate::GetVariantFieldType<
                    $delegating_to_type,
                    $crate::TStr<__V>,
                    __F
                >>
            {
                let $this=self;
                let field:&$delegating_to_type=$get_field_closure;
                $crate::GetVariantField::get_vfield_(field,vname,fname)
            }
        }

        impl<$($impl_params)* NP,__Ty> $crate::FieldType<NP> for $self
        where
            $delegating_to_type: $crate::FieldType<NP,Ty=__Ty>,
            $($where_clause)*
        {
            type Ty=$crate::GetFieldType<$delegating_to_type, NP>;
        }

        impl<$($impl_params)* __F,__Ty>
            $crate::GetField< __F>
            for $self
        where
            $delegating_to_type: $crate::GetField<__F,Ty=__Ty>,
            $($where_clause)*
        {
            #[inline(always)]
            fn get_field_(
                &self,
                fname: __F,
            )->&__Ty{
                let $this=self;
                let field:&$delegating_to_type=$get_field_closure;
                $crate::GetField::get_field_(field,fname)
            }
        }
    };
    (inner;
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        where [$($mut_where_clause:tt)*]
        self_ident=$this:ident;
        specialization_params($($raw_mut_impl:tt)*);
        delegating_to_type=$delegating_to_type:ty;

        GetFieldMut $unsafe_get_field_mut_closure:block
        as_delegating_raw $as_field_mutref_closure:block
    )=>{
        $crate::unsafe_delegate_structural_with_inner!{
            inner-mut-0;

            (
                impl [$($impl_params)*] $self
                where [$($where_clause)*]
                where [$($mut_where_clause)*]
                self_ident=$this;
                specialization_params($($raw_mut_impl)*);
                delegating_to_type=$delegating_to_type;

                GetFieldMut $unsafe_get_field_mut_closure
                as_delegating_raw $as_field_mutref_closure
            )
        }
    };
    (inner-mut-0; $inner_mut_stuff:tt )=>{
        $crate::unsafe_delegate_structural_with_inner!{
            inner-mut-1;

            $inner_mut_stuff
            $inner_mut_stuff
        }
    };
    (inner-mut-1;
        (
            impl [$($impl_params:tt)*] $self:ty
            where [$($where_clause:tt)*]
            where [$($mut_where_clause:tt)*]
            self_ident=$this:ident;
            specialization_params( $(Sized)? );
            delegating_to_type=$delegating_to_type:ty;

            GetFieldMut $unsafe_get_field_mut_closure:block
            as_delegating_raw $as_field_mutref_closure:block
        )

        $inner_mut_stuff:tt
    )=>{
        $crate::unsafe_delegate_structural_with_inner!{
            inner-mut-2;

            $inner_mut_stuff

            struct_fn(
                #[inline(always)]
                unsafe fn get_field_raw_mut(
                    $this:*mut  (),
                    fname: __F,
                )->*mut __Ty
                where
                    Self:Sized
                {
                    let $this=$this as *mut  Self;
                    let $this:*mut $delegating_to_type=
                        $as_field_mutref_closure;
                    <$delegating_to_type as
                        $crate::GetFieldMut<__F>
                    >::get_field_raw_mut( $this as *mut  (),fname )
                }
            )

            enum_fn(
                #[inline(always)]
                unsafe fn get_vfield_raw_mut_(
                    $this: *mut  (),
                    vname: $crate::TStr<__V>,
                    fname: __F,
                ) -> Option<$crate::pmr::NonNull<__Ty>>
                where
                    Self: Sized
                {
                    let $this=$this as *mut  Self;
                    let $this:*mut $delegating_to_type=
                        $as_field_mutref_closure;
                    <$delegating_to_type as
                        $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                    >::get_vfield_raw_mut_( $this as *mut  (),vname,fname)
                }
            )

            impl()
        }
    };
    (inner-mut-1;
        (
            impl [$($impl_params:tt)*] $self:ty
            where [$($where_clause:tt)*]
            where [$($mut_where_clause:tt)*]
            self_ident=$this:ident;
            specialization_params( ?Sized );
            delegating_to_type=$delegating_to_type:ty;

            GetFieldMut $unsafe_get_field_mut_closure:block
            as_delegating_raw $as_field_mutref_closure:block
        )

        $inner_mut_stuff:tt
    )=>{
        $crate::unsafe_delegate_structural_with_inner!{
            inner-mut-2;

            $inner_mut_stuff

            struct_fn(
                #[inline(always)]
                unsafe fn get_field_raw_mut(
                    $this:*mut  (),
                    fname: __F,
                )->*mut __Ty
                where
                    Self:Sized
                {
                    let $this=$this as *mut  Self;
                    let $this:*mut $delegating_to_type=
                        $as_field_mutref_closure;
                    let func=<
                        $delegating_to_type as
                        $crate::GetFieldMut<__F>
                    >::get_field_raw_mut_fn(&*$this);
                    func( $this as *mut  (),fname )
                }
            )

            enum_fn(
                #[inline(always)]
                unsafe fn get_vfield_raw_mut_(
                    $this: *mut  (),
                    vname: $crate::TStr<__V>,
                    fname: __F,
                ) -> Option<$crate::pmr::NonNull<__Ty>>
                where
                    Self: Sized
                {
                    let $this=$this as *mut  Self;
                    let $this:*mut $delegating_to_type=
                        $as_field_mutref_closure;
                    let func=<
                        $delegating_to_type as
                        $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                    >::get_vfield_raw_mut_fn(&*$this);
                    func( $this as *mut  (),vname,fname )
                }
            )

            impl()
        }
    };
    (inner-mut-1;
        (
            impl [$($impl_params:tt)*] $self:ty
            where [$($where_clause:tt)*]
            where [$($mut_where_clause:tt)*]
            self_ident=$this:ident;
            specialization_params( specialize_cfg( $($specialize_cfg:tt)* ) );
            delegating_to_type=$delegating_to_type:ty;

            GetFieldMut $unsafe_get_field_mut_closure:block
            as_delegating_raw $as_field_mutref_closure:block
        )

        $inner_mut_stuff:tt
    )=>{
        $crate::unsafe_delegate_structural_with_inner!{
            inner-mut-2;

            $inner_mut_stuff


            struct_fn(
                #[inline(always)]
                unsafe fn get_field_raw_mut(
                    $this:*mut  (),
                    fname: __F,
                )->*mut __Ty
                where
                    Self:Sized
                {
                    <Self as
                        $crate::pmr::SpecGetFieldMut<__F>
                    >::get_field_raw_mut_inner(
                        $this,
                        fname,
                    )
                }
            )

            enum_fn(
                #[inline(always)]
                unsafe fn get_vfield_raw_mut_(
                    $this: *mut  (),
                    vname: $crate::TStr<__V>,
                    fname: __F,
                ) -> Option<$crate::pmr::NonNull<__Ty>>
                where
                    Self: Sized
                {
                    <Self as
                        $crate::pmr::SpecGetVariantFieldMut<$crate::TStr<__V>,__F>
                    >::get_vfield_raw_mut_inner(
                        $this,
                        vname,
                        fname,
                    )
                }
            )

            impl(
                unsafe impl<$($impl_params)* __F,__Ty>
                    $crate::pmr::SpecGetFieldMut< __F>
                    for $self
                where
                    $delegating_to_type: $crate::GetFieldMut<__F,Ty=__Ty>,
                    $($mut_where_clause)*
                    $($where_clause)*
                {
                    $crate::default_if!{
                        #[inline(always)]
                        cfg(all($($specialize_cfg)*))
                        unsafe fn get_field_raw_mut_inner(
                            $this:*mut  (),
                            fname: __F,

                        )->*mut __Ty
                        where
                            Self:Sized
                        {
                            let $this=$this as *mut  Self;
                            let $this:*mut $delegating_to_type=
                                $as_field_mutref_closure;
                            let func=<
                                $delegating_to_type as
                                $crate::GetFieldMut<__F>
                            >::get_field_raw_mut_fn(&*$this);
                            func( $this as *mut  (),fname )
                        }
                    }
                }

                #[cfg(all($($specialize_cfg)*))]
                unsafe impl<$($impl_params)* __F,__Ty>
                    $crate::pmr::SpecGetFieldMut< __F>
                    for $self
                where
                    $delegating_to_type:
                        Sized +
                        $crate::GetFieldMut<__F,Ty=__Ty>,
                    $($mut_where_clause)*
                    $($where_clause)*
                {
                    unsafe fn get_field_raw_mut_inner(
                        $this:*mut  (),
                        fname: __F,

                    )->*mut __Ty
                    where
                        Self:Sized
                    {
                        let $this=$this as *mut  Self;
                        let $this:*mut $delegating_to_type=
                            $as_field_mutref_closure;
                        <$delegating_to_type as
                            $crate::GetFieldMut<__F>
                        >::get_field_raw_mut( $this as *mut  (),fname )
                    }
                }

                unsafe impl<$($impl_params)* __V,__F,__Ty>
                    $crate::pmr::SpecGetVariantFieldMut< $crate::TStr<__V>,__F>
                    for $self
                where
                    $delegating_to_type:
                        $crate::GetVariantFieldMut<$crate::TStr<__V>,__F,Ty=__Ty>,
                    $($mut_where_clause)*
                    $($where_clause)*
                {
                    $crate::default_if!{
                        #[inline(always)]
                        cfg(all($($specialize_cfg)*))
                        unsafe fn get_vfield_raw_mut_inner(
                            $this:*mut  (),
                            vname: $crate::TStr<__V>,
                            fname: __F,
                        )->Option<$crate::pmr::NonNull<
                            __Ty
                        >>
                        where
                            Self:Sized
                        {
                            let $this=$this as *mut  Self;
                            let $this:*mut $delegating_to_type=
                                $as_field_mutref_closure;
                            let func=<
                                $delegating_to_type as
                                $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                            >::get_vfield_raw_mut_fn(&*$this);
                            func( $this as *mut  (),vname,fname )
                        }
                    }
                }

                #[cfg(all($($specialize_cfg)*))]
                unsafe impl<$($impl_params)* __V,__F,__Ty>
                    $crate::pmr::SpecGetVariantFieldMut< $crate::TStr<__V>,__F>
                    for $self
                where
                    $delegating_to_type:
                        Sized +
                        $crate::GetVariantFieldMut<$crate::TStr<__V>,__F,Ty=__Ty>,
                    $($mut_where_clause)*
                    $($where_clause)*
                {
                    unsafe fn get_vfield_raw_mut_inner(
                        $this:*mut  (),
                        vname: $crate::TStr<__V>,
                        fname: __F,
                    )->Option<$crate::pmr::NonNull<__Ty>>
                    where
                        Self:Sized
                    {
                        let $this=$this as *mut  Self;
                        let $this:*mut $delegating_to_type=
                            $as_field_mutref_closure;
                        <$delegating_to_type as
                            $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                        >::get_vfield_raw_mut_( $this as *mut  (),vname,fname )
                    }
                }
            )
        }
    };
    (inner-mut-2;
        (
            impl [$($impl_params:tt)*] $self:ty
            where [$($where_clause:tt)*]
            where [$($mut_where_clause:tt)*]
            self_ident=$this:ident;
            specialization_params($($raw_mut_impl:tt)*);
            delegating_to_type=$delegating_to_type:ty;

            GetFieldMut $unsafe_get_field_mut_closure:block
            as_delegating_raw $as_field_mutref_closure:block
        )

        struct_fn( $($raw_ptr_fn:tt)* )
        enum_fn( $($raw_enum_ptr_fn:tt)* )
        impl( $($raw_ptr_impl:tt)* )
    )=>{
        unsafe impl<$($impl_params)* __F,__Ty>
            $crate::GetFieldMut<__F>
            for $self
        where
            $self: Sized,
            $delegating_to_type:
                $crate::GetFieldMut<__F,Ty=__Ty>,
            $($where_clause)*
            $($mut_where_clause)*
        {
            #[inline(always)]
            fn get_field_mut_(
                &mut self,
                fname: __F,
            )->&mut __Ty {
                let $this=self;
                let field:&mut $delegating_to_type=$unsafe_get_field_mut_closure;
                <$delegating_to_type as
                    $crate::GetFieldMut<_>
                >::get_field_mut_(field,fname)
            }

            $($raw_ptr_fn)*

            #[inline(always)]
            fn get_field_raw_mut_fn(
                &self
            )->$crate::field::GetFieldRawMutFn<
                __F,
                __Ty,
            >{
                <Self as $crate::GetFieldMut<__F>>::get_field_raw_mut
            }
        }

        unsafe impl<$($impl_params)* __V,__F,__Ty>
            $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
            for $self
        where
            $self: Sized,
            $delegating_to_type: $crate::GetVariantFieldMut<$crate::TStr<__V>,__F,Ty=__Ty>,
            $($where_clause)*
            $($mut_where_clause)*
        {
            #[inline(always)]
            fn get_vfield_mut_(
                &mut self,
                vname: $crate::TStr<__V>,
                fname: __F,
            ) -> Option<&mut __Ty>{
                let $this=self;
                let field:&mut $delegating_to_type=$unsafe_get_field_mut_closure;
                <$delegating_to_type as
                    $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                >::get_vfield_mut_(field,vname,fname)
            }

            $($raw_enum_ptr_fn)*

            #[inline(always)]
            fn get_vfield_raw_mut_unchecked_fn(
                &self
            )->$crate::pmr::GetFieldRawMutFn<__F,__Ty>{
                <Self as
                    $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                >::get_vfield_raw_mut_unchecked
            }

            #[inline(always)]
            fn get_vfield_raw_mut_fn(
                &self
            )->$crate::pmr::GetVFieldRawMutFn<
                    $crate::TStr<__V>,
                    __F,
                    __Ty
                >
            {
                <Self as
                    $crate::GetVariantFieldMut<$crate::TStr<__V>,__F>
                >::get_vfield_raw_mut_
            }

        }


        $($raw_ptr_impl)*
    };
    (inner;
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause_a:tt)*]
        $(where [$($where_clause_b:tt)*])?

        FromStructural {
            converted_from=$converted_from:ty;
            $(converted_from=$converted_from_b:ty;)?
            constructor = $constructor:expr;
        }
    )=>{
        impl<$($impl_params)* __From> $crate::pmr::FromStructural<__From> for $self
        where
            $($where_clause_a)*
            $($($where_clause_b)*)?
            $converted_from: $crate::pmr::FromStructural<__From>,
        {
            #[inline]
            fn from_structural(from: __From) -> Self {
                let delegating_to=
                    <$converted_from as
                        $crate::pmr::FromStructural<__From>
                    >::from_structural(from);
                $constructor(delegating_to)
            }
        }

        impl<$($impl_params)* __From,__Err> $crate::pmr::TryFromStructural<__From> for $self
        where
            $($where_clause_a)*
            $($($where_clause_b)*)?
            $converted_from: $crate::pmr::TryFromStructural<__From, Error=__Err>,
        {
            type Error=__Err;

            #[inline]
            fn try_from_structural(
                from: __From,
            ) -> Result<Self, $crate::pmr::TryFromError<__From,__Err>> {
                let res=
                    <$converted_from as
                        $crate::pmr::TryFromStructural<__From>
                    >::try_from_structural(from);
                match res {
                    Ok(x) => Ok($constructor(x)),
                    Err(e) => Err(e),
                }
            }
        }
    };
    (inner;
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        where [$($into_where_clause:tt)*]
        self_ident=$this:ident;
        delegating_to_type=$delegating_to_type:ty;

        IntoField $into_field_closure:block
        move_out_field $move_out_field_closure:block
    )=>{

        unsafe impl<$($impl_params)* __V,__F,__Ty>
            $crate::pmr::IntoVariantField<$crate::TStr<__V>,__F>
        for $self
        where
            $delegating_to_type:
                $crate::pmr::IntoVariantField<$crate::TStr<__V>,__F,Ty=__Ty>,
            $($into_where_clause)*
            $($where_clause)*
        {
            #[inline(always)]
            fn into_vfield_(self,vname:$crate::TStr<__V>,fname: __F)->Option<__Ty>{
                let $this=self;
                let field:$delegating_to_type=$into_field_closure;
                $crate::IntoVariantField::<$crate::TStr<__V>, __F>::into_vfield_(
                    field,
                    vname,
                    fname,
                )
            }

            #[inline(always)]
            unsafe fn move_out_vfield_(
                &mut self,
                vname:$crate::TStr<__V>,
                fname:__F,
                moved: &mut $crate::pmr::MovedOutFields,
            )->Option<__Ty>{
                let $this=self;
                let field:&mut $delegating_to_type=$move_out_field_closure;
                $crate::IntoVariantField::<$crate::TStr<__V>,__F>::move_out_vfield_(
                    field,
                    vname,
                    fname,
                    moved,
                )
            }
        }

        unsafe impl<$($impl_params)* __F,__Ty>
            $crate::IntoField< __F>
            for $self
        where
            $delegating_to_type:
                $crate::IntoField<__F,Ty=__Ty>,
            $($into_where_clause)*
            $($where_clause)*
        {
            #[inline(always)]
            fn into_field_(self,fname: __F)->__Ty{
                let $this=self;
                let field:$delegating_to_type=$into_field_closure;
                $crate::IntoField::<__F>::into_field_(field, fname)
            }

            #[inline(always)]
            unsafe fn move_out_field_(
                &mut self,
                fname:__F,
                moved: &mut $crate::pmr::MovedOutFields,
            )->__Ty{
                let $this=self;
                let field:&mut $delegating_to_type=$move_out_field_closure;
                $crate::IntoField::<__F>::move_out_field_(
                    field,
                    fname,
                    moved,
                )
            }
        }
    };
    (@drop_fields_impls
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        where [$($into_where_clause:tt)*]
        self_ident=$this:ident;
        delegating_to_type=$delegating_to_type:ty;

        IntoField $into_field_closure:block

        DropFields = manual
    )=>{

    };
    (@drop_fields_impls
        impl [$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        where [$($into_where_clause:tt)*]
        self_ident=$this:ident;
        delegating_to_type=$delegating_to_type:ty;

        IntoField $into_field_closure:block
        move_out_field $move_out_field_closure:block

        DropFields = {
            dropped_fields[$($field:tt)*]

            $(pre_move = $pre_move:expr;)?

            $(pre_post_drop_fields=$pp_drop_fields:ident;)?
            $(drop_delegated_to_variable=$drop_delegated:ident;)?
        }
    )=>{
        unsafe impl<$($impl_params)*> $crate::pmr::DropFields for $self
        where
            $delegating_to_type: $crate::pmr::DropFields,
            $($into_where_clause)*
            $($where_clause)*
        {
            #[inline(always)]
            fn pre_move(&mut self){
                #[allow(unused_variables,unused_mut)]
                let mut guard=$crate::pmr::RunOnDrop::new(
                    self,
                    #[inline(always)]
                    |$this|{
                        let field:&mut $delegating_to_type=$move_out_field_closure;
                        $crate::pmr::DropFields::pre_move(field);
                    }
                );

                $(
                    let func=$pre_move;
                    func(guard.reborrow_mut());
                )?
            }

            #[allow(unused_variables)]
            unsafe fn drop_fields(&mut self, moved: $crate::pmr::MovedOutFields) {
                let $this=self;

                $crate::delegate_to_into_helper!{
                    post_drop=$($pp_drop_fields)?,
                    self_ident=$this,
                }

                // Remember that:
                //
                // - the fields are dropped in the destructor for RunDrop
                //
                // - the delegated-to variable is dropped in the destructor for
                // RunDropFields.
                //
                // Destructors run in the opposite order in which variables are declared,
                // and after all statements.
                let mut $this=$crate::pmr::RunOnDrop::new(
                    $this,
                    #[inline(always)]
                    |$this|{
                        $crate::delegate_to_into_helper!{
                            drop_delegated_to=$($drop_delegated)?,
                            self_ident=$this,
                            delegating_to_type=$delegating_to_type,
                            delegating_to=$move_out_field_closure,
                            moved_fields_variable=moved,
                        }

                        $crate::reverse_code!{
                            $((
                                $crate::delegate_to_into_helper!{
                                    drop_field($field),
                                    self_ident=$this,
                                }
                            ))*
                        }
                    }
                );

                let $this=$this.reborrow_mut();
                $crate::delegate_to_into_helper!{
                    pre_drop=$($pp_drop_fields)?,
                    self_ident=$this,
                }
            }
        }
    };
    ( $($stuff:tt)* )=>{
        compile_error!{concat!(
            "Unrecognized `unsafe_delegate_structural_with_inner` arguments:",
            $(stringify!($stuff),)*
        )}
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! delegate_to_into_helper {
    (
        drop_field(($($nested:tt)*)),
        self_ident=$this:ident,
    )=>{
        let _a=$crate::pmr::RunDrop::new(&mut $this.$($nested)*);
    };
    (
        // Ignoring the optional commas separating the fields
        drop_field(,),
        self_ident=$this:ident,
    )=>{};
    (
        drop_field($field:tt),
        self_ident=$this:ident,
    )=>{
        let _a=$crate::pmr::RunDrop::new(&mut $this.$field);
    };
    //////////////////////////////////////////////////////////////////////
    (
        $(pre_drop)? $(post_drop)?=$(false)?,
        self_ident=$this:ident,
    ) => ();
    (
        pre_drop=true,
        self_ident=$this:ident,
    ) => (
        $crate::pmr::PrePostDropFields::pre_drop($this);
    );
    (
        post_drop=true,
        self_ident=$this:ident,
    ) => (
        let mut $this = $crate::pmr::RunPostDrop::new($this);
        let $this=$this.get_mut();
    );
    //////////////////////////////////////////////////////////////////////
    (
        drop_delegated_to=false,
        self_ident=$this:ident,
        delegating_to_type=$delegating_to_type:ty,
        delegating_to=$into_field_closure:block,
        moved_fields_variable=$moved_fields_var:ident,
    ) => ();
    (
        drop_delegated_to=$(true)?,
        self_ident=$this:ident,
        delegating_to_type=$delegating_to_type:ty,
        delegating_to=$into_field_closure:block,
        moved_fields_variable=$moved_fields_var:ident,
    ) => (
        let delegated_to:&mut $delegating_to_type = $into_field_closure;
        let _a=$crate::pmr::RunDropFields::new(delegated_to,$moved_fields_var);
    );
}

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