libc 0.2.190

Raw FFI bindings to platform libraries like libc.
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
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
/// A macro for defining #[cfg] if-else statements.
///
/// This is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of
/// `#[cfg]` cases, emitting the implementation which matches first.
///
/// This allows you to conveniently provide a long list #[cfg]'d blocks of code without having to
/// rewrite each clause multiple times.
macro_rules! cfg_if {
    // match if/else chains with a final `else`
    ($(
        if #[cfg($($meta:meta),*)] { $($it:item)* }
    ) else * else {
        $($it2:item)*
    }) => {
        cfg_if! {
            @__items
            () ;
            $( ( ($($meta),*) ($($it)*) ), )*
            ( () ($($it2)*) ),
        }
    };

    // match if/else chains lacking a final `else`
    (
        if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
        $(
            else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
        )*
    ) => {
        cfg_if! {
            @__items
            () ;
            ( ($($i_met),*) ($($i_it)*) ),
            $( ( ($($e_met),*) ($($e_it)*) ), )*
            ( () () ),
        }
    };

    // Internal and recursive macro to emit all the items
    //
    // Collects all the negated `cfg`s in a list at the beginning and after the
    // semicolon is all the remaining items
    (@__items ($($not:meta,)*) ; ) => {};
    (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ),
     $($rest:tt)*) => {
        // Emit all items within one block, applying an appropriate #[cfg]. The
        // #[cfg] will require all `$m` matchers specified and must also negate
        // all previous matchers.
        cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }

        // Recurse to emit all other items in `$rest`, and when we do so add all
        // our `$m` matchers to the list of `$not` matchers as future emissions
        // will have to negate everything we just matched as well.
        cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
    };

    // Internal macro to Apply a cfg attribute to a list of items
    (@__apply $m:meta, $($it:item)*) => {
        $(#[$m] $it)*
    };
}

/// Create an internal crate prelude with `core` reexports and common types.
macro_rules! prelude {
    () => {
        mod types;

        /// Frequently-used types that are available on all platforms
        ///
        /// We need to reexport the core types so this works with `rust-dep-of-std`.
        mod prelude {
            // Exports from `core`
            #[allow(unused_imports)]
            pub(crate) use core::clone::Clone;
            #[allow(unused_imports)]
            pub(crate) use core::cmp::{
                Eq,
                PartialEq,
            };
            #[allow(unused_imports)]
            pub(crate) use core::default::Default;
            #[allow(unused_imports)]
            pub(crate) use core::iter::Iterator;
            #[allow(unused_imports)]
            pub(crate) use core::marker::{
                Copy,
                Send,
                Sync,
            };
            #[allow(unused_imports)]
            pub(crate) use core::option::Option::{
                self,
                None,
                Some,
            };
            #[allow(unused_imports)]
            pub(crate) use core::prelude::v1::derive;
            #[allow(unused_imports)]
            pub(crate) use core::{
                assert,
                cfg,
                compile_error,
                debug_assert,
                fmt,
                hash,
                iter,
                mem,
                panic,
                ptr,
                unimplemented,
            };

            #[allow(unused_imports)]
            pub(crate) use fmt::Debug;
            #[allow(unused_imports)]
            pub(crate) use mem::{
                align_of,
                align_of_val,
                size_of,
                size_of_val,
            };

            #[allow(unused_imports)]
            #[cfg(any(target_os = "linux", target_os = "android", target_os = "l4re"))]
            pub(crate) use crate::types::u32_cast_ioctl;
            #[allow(unused_imports)]
            pub(crate) use crate::types::{
                cstr,
                replace_array_items,
                u16_cast_short,
                u32_cast_int,
                u32_cast_long,
                u8_slice_cast_char_slice,
                ulong_cast_int,
                ulong_cast_uint,
                CEnumRepr,
                Padding,
            };
            // Commonly used types defined in this crate
            #[allow(unused_imports)]
            pub(crate) use crate::{
                c_char,
                c_double,
                c_float,
                c_int,
                c_long,
                c_longlong,
                c_short,
                c_uchar,
                c_uint,
                c_ulong,
                c_ulonglong,
                c_ushort,
                c_void,
                intptr_t,
                size_t,
                ssize_t,
                uintptr_t,
            };
        }
    };
}

/// Implement `Clone`, `Copy`, and `Debug` for one or more structs, as well as `PartialEq`, `Eq`,
/// and `Hash` if the `extra_traits` feature is enabled.
///
/// Also mark the type with `repr(C)`.
///
/// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not make sense, and
/// for unions.
macro_rules! s {
    ($(
        $(#[$attr:meta])*
        $pub:vis $t:ident $i:ident { $($field:tt)* }
    )*) => ($(
        s!(it: $(#[$attr])* $pub $t $i { $($field)* });
    )*);

    (it: $(#[$attr:meta])* $pub:vis union $i:ident { $($field:tt)* }) => (
        compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
    );

    (it: $(#[$attr:meta])* $pub:vis struct $i:ident { $($field:tt)* }) => (
        #[repr(C)]
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
            ::core::fmt::Debug,
        )]
        #[cfg_attr(
            feature = "extra_traits",
            ::core::prelude::v1::derive(
                ::core::cmp::PartialEq,
                ::core::cmp::Eq,
                ::core::hash::Hash,
            )
        )]
        #[allow(deprecated)]
        $(#[$attr])*
        $pub struct $i { $($field)* }
    );
}

/// Implement `Clone`, `Copy`, and `Debug` for a tuple struct, as well as `PartialEq`, `Eq`, and
/// `Hash` if the `extra_traits` feature is enabled.
///
/// Unlike `s!`, this does *not* mark the type with `repr(C)`. Users should provide their own
/// `repr` attribute via `$attr` as necessary.
macro_rules! s_paren {
    ($(
        $(#[$attr:meta])*
        $pub:vis struct $i:ident ( $($field:tt)* );
    )*) => ($(
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
            ::core::fmt::Debug,
        )]
        #[cfg_attr(
            feature = "extra_traits",
            ::core::prelude::v1::derive(
                ::core::cmp::PartialEq,
                ::core::cmp::Eq,
                ::core::hash::Hash,
            )
        )]
        $(#[$attr])*
        $pub struct $i ( $($field)* );
    )*);
}

/// Implement `Clone`, `Copy`, and `Debug` for one or more structs/unions, but exclude `PartialEq`,
/// `Eq`, and `Hash`.
///
/// Also mark the type with `repr(C)`.
///
/// Most structs will prefer to use [`s`].
macro_rules! s_no_extra_traits {
    ($(
        $(#$attr:tt)*
        $pub:vis $t:ident $i:ident { $($field:tt)* }
    )*) => ($(
        s_no_extra_traits!(it: $(#$attr)* $pub $t $i { $($field)* });
    )*);

    (it: $(#$attr:tt)* $pub:vis union $i:ident { $($field:tt)* }) => (
        union_with_debug! {
            $(#$attr)* $pub union $i { $($field)* }
        }
    );

    (it: $(#$attr:tt)* $pub:vis struct $i:ident { $($field:tt)* }) => (
        #[repr(C)]
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
            ::core::fmt::Debug,
        )]
        $(#$attr)*
        $pub struct $i { $($field)* }
    );
}

/// Like [`s`], but (1) generates a `Default` impl for every struct in the block, and (2) adds a
/// private field to the struct to replicate the effects of the `non_exhaustive` attribute while
/// rust-lang/rust#132699 gets sorted out.
///
/// To opt out of having the private field added, annotate the struct with an `exhaustive`
/// attribute, as in:
///
/// ```ignore
/// s2! {
///     #[exhaustive]
///     struct Something {
///         ...
///     }
/// }
/// ```
///
/// See [`custom_struct`] for details.
macro_rules! s2 {
    ($(
        $(#$attr:tt)*
        $pub:vis $t:ident $i:ident { $($field:tt)* }
    )*) => ($(
        s2!(it: $(#$attr)* $pub $t $i { $($field)* });
    )*);

    (it: $(#$attr:tt)* $pub:vis union $i:ident { $($field:tt)* }) => (
        compile_error!(
            "unions cannot derive extra traits, use `s_no_extra_traits2` \
             instead"
        );
    );

    (it: $(#$attr:tt)* $pub:vis struct $i:ident { $($field:tt)* }) => (
        custom_struct! {
            attrs: {
                #[repr(C)]
                #[::core::prelude::v1::derive(
                    ::core::clone::Clone,
                    ::core::marker::Copy,
                    ::core::fmt::Debug,
                )]
                #[cfg_attr(
                    feature = "extra_traits",
                    ::core::prelude::v1::derive(
                        ::core::cmp::PartialEq,
                        ::core::cmp::Eq,
                        ::core::hash::Hash,
                    )
                )]
                #[allow(deprecated)]
            }
            $(#$attr)* $pub struct $i { $($field)* }
        }
    );
}

/// Like [`s_no_extra_traits`], but (1) generates a `Default` impl for every struct in the block,
/// and (2) adds a private field to replicate the effects of the built-in `non_exhaustive` attribute
/// while rust-lang/rust#132699 gets sorted out.
///
/// Unions are emitted just like `s_no_extra_traits!` does, with no `Default`.  A struct field of
/// union type supplies its own default via `#[custom_default(...)]`.
///
/// To opt out of having the private field added, annotate the struct with an `exhaustive`
/// attribute, as in:
///
/// ```ignore
/// s_no_extra_traits2! {
///     #[exhaustive]
///     struct Something {
///         ...
///     }
/// }
/// ```
macro_rules! s_no_extra_traits2 {
    ($(
        $(#$attr:tt)*
        $pub:vis $t:ident $i:ident { $($field:tt)* }
    )*) => ($(
        s_no_extra_traits2!(it: $(#$attr)* $pub $t $i { $($field)* });
    )*);

    (it: $(#$attr:tt)* $pub:vis union $i:ident { $($field:tt)* }) => (
        union_with_debug! {
            $(#$attr)* $pub union $i { $($field)* }
        }
    );

    (it: $(#$attr:tt)* $pub:vis struct $i:ident { $($field:tt)* }) => (
        custom_struct! {
            attrs: {
                #[repr(C)]
                #[::core::prelude::v1::derive(
                    ::core::clone::Clone,
                    ::core::marker::Copy,
                    ::core::fmt::Debug,
                )]
            }
            $(#$attr)* $pub struct $i { $($field)* }
        }
    );
}

/// Emit a union plus its `Debug` impl.
///
/// Unions can't derive `Debug`, so it is written out here. Attributes are split like
/// [`custom_struct`] does. Everything goes on the union, but only the `cfg`s are repeated on the
/// impl, otherwise a union that is configured out leaves an impl behind.
macro_rules! union_with_debug {
    (
        $(#$attr:tt)*
        $vis:vis union $name:ident { $($body:tt)* }
    ) => {
        union_with_debug! {
            @split_attrs,
            cfg_attrs: { },
            other_attrs: { },
            remaining_attrs: { $(#$attr)* },
            found_default_via_unsafe_zeroed: false,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // a `cfg` also has to gate the impl
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #[cfg($($cfg:tt)*)]
            $($tail:tt)*
        },
        found_default_via_unsafe_zeroed: $found_default:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        union_with_debug! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* #[cfg($($cfg)*)] },
            other_attrs: { $($other_attrs)* },
            remaining_attrs: { $($tail)* },
            found_default_via_unsafe_zeroed: $found_default,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // a `cfg` also has to gate the impl
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #[unsafe(union_default_via_zeroed)]
            $($tail:tt)*
        },
        found_default_via_unsafe_zeroed: $found_default:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        union_with_debug! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* },
            remaining_attrs: { $($tail)* },
            found_default_via_unsafe_zeroed: true,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // anything else belongs to the union only
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #$other:tt
            $($tail:tt)*
        },
        found_default_via_unsafe_zeroed: $found_default:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        union_with_debug! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* #$other },
            remaining_attrs: { $($tail)* },
            found_default_via_unsafe_zeroed: $found_default,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // done
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: { },
        found_default_via_unsafe_zeroed: $found_default:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        #[repr(C)]
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
        )]
        $($other_attrs)*
        $($cfg_attrs)*
        $vis union $name { $($body)* }

        $($cfg_attrs)*
        #[allow(deprecated)]
        impl ::core::fmt::Debug for $name {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                f.debug_struct(::core::stringify!($name)).finish_non_exhaustive()
            }
        }

        emit_union_default_via_unsafe_zeroed! {
            found_default_via_unsafe_zeroed: $found_default,
            cfg_attrs: { $($cfg_attrs)* },
            name: $name,
        }
    };
}

/// Add a `Default` impl that is just `mem::zeroed`.
macro_rules! emit_union_default_via_unsafe_zeroed {
    (
        found_default_via_unsafe_zeroed: false,
        cfg_attrs: { $($cfg_attrs:tt)* },
        name: $name:ident,
    ) => {
    };

    (
        found_default_via_unsafe_zeroed: true,
        cfg_attrs: { $($cfg_attrs:tt)* },
        name: $name:ident,
    ) => {
        $($cfg_attrs)*
        impl ::core::default::Default for $name {
            fn default() -> Self {
                unsafe { ::core::mem::zeroed() }
            }
        }
    };
}

/// Emit a struct with the given derive attributes plus a generated `Default` impl. Ensure that the
/// record has an additional private field added to replicate `#[non_exhaustive]`, unless it is
/// annotated with `#[exhaustive]`.
///
/// Fields default to `Default::default()`. A field whose default can't be derived must carry
/// `#[custom_default(EXPR)]` as its *first* attribute, and `EXPR` is used instead.
///
/// This works by scanning each field for `#[custom_default]` attributes. If one exists, the
/// attribute's contents are added to `processed_field_defaults` and will be used in the expansion
/// for `Default`. If it does not exist, `Default::default()` is used instead. In either case, the
/// field is added to `processed_fields` with `#[custom_default]` stripped if necessary, and
/// `custom_struct` is invoked again with the remaining fields.
///
/// Attributes are split into `cfg_attrs` and `other_attrs` before the fields are scanned. Both go
/// on the struct, but only the `cfg`s are repeated on the `Default` impl. A `cfg` decides whether
/// the type exists at all, so without it a configured-out struct leaves an impl behind referring to
/// a type that isn't there.
///
/// Out of `other_attrs`, we scan for `#[exhaustive]`. If found, we remove it but take into account
/// that the record should be expanded _without_ an additional private field. The scan for both
/// `cfg` attributes and the (non-existent) `exhaustive` attribute is done in one linear pass.
macro_rules! custom_struct {
    // entry; `attrs` is the attribute block the caller wants on the struct
    // (repr, derives, etc.), which is merged with the struct's own attributes.
    (
        attrs: { $($attrs:tt)* }
        $(#$attr:tt)*
        $vis:vis struct $name:ident { $($body:tt)* }
    ) => {
        custom_struct! {
            @split_attrs,
            cfg_attrs: { },
            other_attrs: { },
            remaining_attrs: { $($attrs)* $(#$attr)* },
            found_exhaustive_attr: false,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // a `cfg` also has to gate the impl
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #[cfg($($cfg:tt)*)]
            $($tail:tt)*
        },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        custom_struct! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* #[cfg($($cfg)*)] },
            other_attrs: { $($other_attrs)* },
            remaining_attrs: { $($tail)* },
            found_exhaustive_attr: $found_exhaustive,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // `exhaustive` must be taken into account as many times as it appears,
    // though the effect is the same with a single annotation.
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #[exhaustive]
            $($tail:tt)*
        },
        found_exhaustive_attr: $_:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        custom_struct! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* },
            remaining_attrs: { $($tail)* },
            found_exhaustive_attr: true,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // anything else belongs to the struct only
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: {
            #$other:tt
            $($tail:tt)*
        },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        custom_struct! {
            @split_attrs,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* #$other },
            remaining_attrs: { $($tail)* },
            found_exhaustive_attr: $found_exhaustive,
            vis: $vis,
            name: $name,
            body: { $($body)* },
        }
    };

    // attributes are split, move on to the fields
    (
        @split_attrs,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        remaining_attrs: { },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        body: { $($body:tt)* },
    ) => {
        custom_struct! {
            @struct,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* },
            found_exhaustive_attr: $found_exhaustive,
            vis: $vis,
            name: $name,
            processed_fields: { },
            processed_field_defaults: { },
            remaining_fields: { $($body)* },
        }
    };

    // field led by #[custom_default(...)]
    (
        @struct,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        processed_fields: { $($processed_fields:tt)* },
        processed_field_defaults: { $($processed_field_defaults:tt)* },
        remaining_fields: {
            #[custom_default($default:expr)]
            $(#[$fattr:meta])*
            $fvis:vis $fname:ident: $fty:ty,
            $($tail:tt)*
        },
    ) => {
        custom_struct! {
            @struct,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* },
            found_exhaustive_attr: $found_exhaustive,
            vis: $vis,
            name: $name,
            processed_fields: { $($processed_fields)* $(#[$fattr])* $fvis $fname: $fty, },
            processed_field_defaults: {
                $($processed_field_defaults)*
                $(#[$fattr])* $fname: $default,
            },
            remaining_fields: { $($tail)* },
        }
    };

    // plain field
    (
        @struct,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        processed_fields: { $($processed_fields:tt)* },
        processed_field_defaults: { $($processed_field_defaults:tt)* },
        remaining_fields: {
            $(#[$fattr:meta])*
            $fvis:vis $fname:ident: $fty:ty,
            $($tail:tt)*
        },
    ) => {
        custom_struct! {
            @struct,
            cfg_attrs: { $($cfg_attrs)* },
            other_attrs: { $($other_attrs)* },
            found_exhaustive_attr: $found_exhaustive,
            vis: $vis,
            name: $name,
            processed_fields: { $($processed_fields)* $(#[$fattr])* $fvis $fname: $fty, },
            processed_field_defaults: {
                $($processed_field_defaults)*
                $(#[$fattr])* $fname: ::core::default::Default::default(),
            },
            remaining_fields: { $($tail)* },
        }
    };

    // done
    (
        @struct,
        cfg_attrs: { $($cfg_attrs:tt)* },
        other_attrs: { $($other_attrs:tt)* },
        found_exhaustive_attr: $found_exhaustive:tt,
        vis: $vis:vis,
        name: $name:ident,
        processed_fields: { $($processed_fields:tt)* },
        processed_field_defaults: { $($processed_field_defaults:tt)* },
        remaining_fields: { },
    ) => {
        emit_struct_definition! {
            found_exhaustive_attr: $found_exhaustive,
            body: {
                $($other_attrs)*
                $($cfg_attrs)*
                $vis $name { $($processed_fields)* }
            }
        }

        $($cfg_attrs)*
        // The impl names the type and its fields, which warns if either is
        // deprecated.
        #[allow(deprecated)]
        impl ::core::default::Default for $name {
            // Field attributes (`#[cfg]`, doc comments) get forwarded to the
            // initializer too. Docs are harmless there but trip the lint, so
            // silence it.
            #[allow(unused_doc_comments)]
            fn default() -> Self {
                emit_struct_default_body! {
                    found_exhaustive_attr: $found_exhaustive,
                    body: { $($processed_field_defaults)* }
                }
            }
        }
    };
}

/// Expands the definition of the record defined at [`custom_struct`], with either one of an
/// additional private field or with its verbatim fields.
macro_rules! emit_struct_definition {
    (
        found_exhaustive_attr: false,
        body: { $(#[$attr:meta])* $vis:vis $name:ident { $($field:tt)* } }
    ) => {
        #[allow(clippy::manual_non_exhaustive)]
        $(#[$attr])*
        $vis struct $name { $($field)* __non_exhaustive: () }
    };

    (
        found_exhaustive_attr: true,
        body: { $(#[$attr:meta])* $vis:vis $name:ident { $($field:tt)* } }
    ) => {
        $(#[$attr])*
        $vis struct $name { $($field)* }
    };
}

/// Expands the `Default` implementation of the record defined at [`custom_struct`], with either one
/// of an additional private field initialized to the unit value, or the record's fields verbatim.
macro_rules! emit_struct_default_body {
    (
        found_exhaustive_attr: false,
        body: { $($field_default:tt)* }
    ) => {
        Self { $($field_default)* __non_exhaustive: () }
    };

    (
        found_exhaustive_attr: true,
        body: { $($field_default:tt)* }
    ) => {
        Self { $($field_default)* }
    };
}

/// Create an uninhabited type that can't be constructed. It implements `Debug`, `Clone`, and
/// `Copy`, but these aren't meaningful for extern types so they should eventually be removed.
///
/// Really what we want here is something that also can't be named without indirection (in ADTs or
/// function signatures), but this doesn't exist.
macro_rules! extern_ty {
    ($(
        $(#[$attr:meta])*
        $vis:vis type $i:ident;
    )*) => ($(
        $(#[$attr])*
        /// This is an extern type ("opaque" or "incomplete" type in C).
        ///
        /// <div class="warning">
        /// This type's current representation allows inspecting some properties, such as via
        /// <code>size_of</code>, and it is technically possible to construct the type within
        /// <code>MaybeUninit</code>, However, this <strong>MUST NOT</strong> be relied upon
        /// because a future version of <code>libc</code> may switch to a proper
        /// <a href="https://rust-lang.github.io/rfcs/1861-extern-types.html">extern type</a>
        /// representation when available.
        /// </div>
        // ^ unfortunately warning blocks currently don't render markdown so we need to
        // use raw HTML.
        //
        // Representation based on the Nomicon:
        // <https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs>.
        //
        // FIXME(1.0): These traits are unreachable and should be removed.
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
            ::core::fmt::Debug,
        )]
        #[repr(C)]
        $vis struct $i {
            _data: (),
            _marker: ::core::marker::PhantomData<(*mut u8, ::core::marker::PhantomPinned)>,
        }
    )*);
}

/// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and
/// `PartialEq` if the `extra_traits` feature is enabled.
// FIXME(#4419): Replace all uses of `e!` with `c_enum!`
macro_rules! e {
    ($(
        $(#[$attr:meta])*
        pub enum $i:ident { $($field:tt)* }
    )*) => ($(
        #[cfg_attr(
            feature = "extra_traits",
            ::core::prelude::v1::derive(Eq, Hash, PartialEq)
        )]
        #[::core::prelude::v1::derive(
            ::core::clone::Clone,
            ::core::marker::Copy,
            ::core::fmt::Debug,
        )]
        $(#[$attr])*
        pub enum $i { $($field)* }
    )*);
}

/// Represent a C enum as Rust constants and a type.
///
/// C enums can't soundly be mapped to Rust enums since C enums are allowed to have duplicates or
/// unlisted values, but this is UB in Rust. This enum doesn't implement any traits, its main
/// purpose is to calculate the correct enum values.
///
/// Use the magic name `#anon` if the C enum doesn't create a type.
///
/// See <https://github.com/rust-lang/libc/issues/4419> for more.
macro_rules! c_enum {
    // Matcher for multiple enums
    ($(
        $(#[repr($repr:ty)])?
        $vis:vis enum $($ty_name:ident)? $(#$anon:ident)? {
            $(
                $(#[$meta:meta])*
                $field_vis:vis $variant:ident $(= $value:expr)?,
            )+
        }
    )+) => {
        $(c_enum!(@single;
            $(#[repr($repr)])?
            $vis enum $($ty_name)? $(#$anon)? {
                $(
                    $(#[$meta])*
                    $field_vis $variant $(= $value)?,
                )+
            }
        );)+
    };

    // Matcher for a single enum
    (@single;
        $(#[repr($repr:ty)])?
        $vis:vis enum $ty_name:ident {
            $(
                $(#[$meta:meta])*
                $field_vis:vis $variant:ident $(= $value:expr)?,
            )+
        }
    ) => {
        $vis type $ty_name = c_enum!(@ty $($repr)?);
        c_enum! {
            @variant;
            ty: $ty_name;
            default: 0;
            variants: [$(
                $(#[$meta])*
                $field_vis $variant $(= $value)?,
            )+]
        }
    };

    // Matcher for a single anonymous enum
    (@single;
        $(#[repr($repr:ty)])?
        $vis:vis enum #anon {
            $(
                $(#[$meta:meta])*
                $field_vis:vis $variant:ident $(= $value:expr)?,
            )+
        }
    ) => {
        c_enum! {
            @variant;
            ty: c_enum!(@ty $($repr)?);
            default: 0;
            variants: [$(
                $(#[$meta])*
                $field_vis $variant $(= $value)?,
            )+]
        }
    };

    // Matcher for variants: eats a single variant then recurses with the rest
    (@variant;
        ty: $_ty_name:ty;
        default: $_idx:expr;
        variants: []
    ) => { /* end of the chain */ };
    (
        @variant;
        ty: $ty_name:ty;
        default: $default_val:expr;
        variants: [
            $(#[$meta:meta])*
            $field_vis:vis $variant:ident $(= $value:expr)?,
            $($tail:tt)*
        ]
    ) => {
        $(#[$meta])*
        #[allow(deprecated)]
        $field_vis const $variant: $ty_name = {
            #[allow(unused_variables)]
            let r = $default_val;
            $(let r = $value;)?
            r
        };

        // The next value is always one more than the previous value, unless
        // set explicitly.
        c_enum! {
            @variant;
            ty: $ty_name;
            default: $variant + 1;
            variants: [$($tail)*]
        }
    };

    // Use a specific type if provided, otherwise default to `CEnumRepr`
    (@ty $repr:ty) => { $repr };
    (@ty) => { $crate::prelude::CEnumRepr };
}

/// Define a function that can be either `safe` or `unsafe` and optionally `const`. This always
/// marks the function inline and adds `extern "C"`.
macro_rules! f {
    ($(
        $(#[$attr:meta])*
        pub $(const $($const_dummy:literal)?)?
        $(unsafe $($unsafe_dummy:literal)?)? $(safe $($safe_dummy:literal)?)?
        fn $i:ident ($($arg:ident: $argty:ty),* $(,)?) -> $ret:ty
            $body:block
    )+) => {$(
        f! {
            @single
            $(#[$attr])*
            pub $(const $($const_dummy)?)?
            $(unsafe $($unsafe_dummy)?)? $(safe $($safe_dummy)?)?
            fn $i ($($arg: $argty),*) -> $ret
                $body
        }
    )+};

    (@single
        $(#[$attr:meta])*
        pub $(const $($const_dummy:literal)?)? unsafe
        fn $i:ident ($($arg:ident: $argty:ty),* $(,)?) -> $ret:ty
            $body:block
    ) => {
        #[inline]
        $(#[$attr])*
        pub $(const $($const_dummy)?)? unsafe extern "C"
        fn $i ($($arg: $argty),*) -> $ret
            $body
    };

    (@single
        $(#[$attr:meta])*
        pub $(const $($const_dummy:literal)?)? safe
        fn $i:ident ($($arg:ident: $argty:ty),* $(,)?) -> $ret:ty
            $body:block
    ) => {
        #[inline]
        $(#[$attr])*
        pub $(const $($const_dummy)?)? extern "C"
        fn $i ($($arg: $argty),*) -> $ret
            $body
    };

    (@single
        pub $(const $($const_dummy:literal)?)?
        fn $i:ident ($($arg:ident: $argty:ty),* $(,)?) -> $ret:ty
            $body:block
    ) => {
        compile_error!("either `safe` or `unsafe` must be specified");
    };
}

// This macro is used to deprecate items that should be accessed via the mach2 crate
macro_rules! deprecated_mach {
    (pub const $id:ident: $ty:ty = $expr:expr;) => {
        #[deprecated(
            since = "0.2.55",
            note = "Use the `mach2` crate instead",
        )]
        #[allow(deprecated)]
        pub const $id: $ty = $expr;
    };
    ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => {
        $(
            deprecated_mach!(
                pub const $id: $ty = $expr;
            );
        )*
    };
    (pub type $id:ident = $ty:ty;) => {
        #[deprecated(
            since = "0.2.55",
            note = "Use the `mach2` crate instead",
        )]
        #[allow(deprecated)]
        pub type $id = $ty;
    };
    ($(pub type $id:ident = $ty:ty;)*) => {
        $(
            deprecated_mach!(
                pub type $id = $ty;
            );
        )*
    }
}

/// Polyfill for std's `offset_of`.
// FIXME(msrv): stabilized in std in 1.77
macro_rules! offset_of {
    ($Ty:path, $field:ident) => {{
        // Taken from bytemuck, avoids accidentally calling on deref
        #[allow(clippy::unneeded_wildcard_pattern)]
        let $Ty { $field: _, .. };
        let data = core::mem::MaybeUninit::<$Ty>::uninit();
        let ptr = data.as_ptr();
        // nested unsafe, see f!
        #[allow(unused_unsafe)]
        // SAFETY: computed address is inbounds since we have a stack alloc for
        // T
        let fptr = unsafe { core::ptr::addr_of!((*ptr).$field) };
        let off = (fptr as usize).checked_sub(ptr as usize).unwrap();
        core::assert!(off <= core::mem::size_of::<$Ty>());
        off
    }};
}

#[cfg(test)]
mod tests {
    use core::any::TypeId;
    use core::prelude::v1::*;

    use crate::types::CEnumRepr;

    #[test]
    fn c_enum_basic() {
        // By default, variants get sequential values.
        c_enum! {
            pub enum e {
                VAR0,
                VAR1,
                VAR2,
            }

            // Also check enums that don't create a type.
            pub enum #anon {
                ANON0,
                ANON1,
                ANON2,
            }

            // No visibility required.
            enum #anon {
                ANON3,
                ANON4,
                ANON5,
            }
        }

        assert_eq!(TypeId::of::<e>(), TypeId::of::<CEnumRepr>());
        assert_eq!(VAR0, 0 as CEnumRepr);
        assert_eq!(VAR1, 1 as CEnumRepr);
        assert_eq!(VAR2, 2 as CEnumRepr);

        assert_eq!(type_id_of_val(&ANON0), TypeId::of::<CEnumRepr>());
        assert_eq!(ANON0, 0 as CEnumRepr);
        assert_eq!(ANON1, 1 as CEnumRepr);
        assert_eq!(ANON2, 2 as CEnumRepr);

        assert_eq!(type_id_of_val(&ANON3), TypeId::of::<CEnumRepr>());
        assert_eq!(ANON3, 0 as CEnumRepr);
        assert_eq!(ANON4, 1 as CEnumRepr);
        assert_eq!(ANON5, 2 as CEnumRepr);
    }

    #[test]
    fn c_enum_repr() {
        // Check specifying the integer representation
        c_enum! {
            #[repr(u16)]
            pub enum e {
                VAR0,
            }

            #[repr(u16)]
            pub enum #anon {
                ANON0,
            }
        }

        assert_eq!(TypeId::of::<e>(), TypeId::of::<u16>());
        assert_eq!(VAR0, 0_u16);

        assert_eq!(type_id_of_val(&ANON0), TypeId::of::<u16>());
        assert_eq!(ANON0, 0_u16);
    }

    #[test]
    fn c_enum_set_value() {
        // Setting an explicit value resets the count.
        c_enum! {
            pub enum e {
                VAR2 = 2,
                VAR3,
                VAR4,
            }
        }

        assert_eq!(VAR2, 2 as CEnumRepr);
        assert_eq!(VAR3, 3 as CEnumRepr);
        assert_eq!(VAR4, 4 as CEnumRepr);
    }

    #[test]
    fn c_enum_multiple_set_value() {
        // C enums always take one more than the previous value, unless set to a
        // specific value. Duplicates are allowed.
        c_enum! {
            pub enum e {
                VAR0,
                VAR2_0 = 2,
                VAR3_0,
                VAR4_0,
                VAR2_1 = 2,
                VAR3_1,
                VAR4_1,
            }
        }

        assert_eq!(VAR0, 0 as CEnumRepr);
        assert_eq!(VAR2_0, 2 as CEnumRepr);
        assert_eq!(VAR3_0, 3 as CEnumRepr);
        assert_eq!(VAR4_0, 4 as CEnumRepr);
        assert_eq!(VAR2_1, 2 as CEnumRepr);
        assert_eq!(VAR3_1, 3 as CEnumRepr);
        assert_eq!(VAR4_1, 4 as CEnumRepr);
    }

    #[test]
    fn c_enum_vis() {
        mod priv1 {
            c_enum! {
                #[repr(u8)]
                pub enum e1 {
                    PRIV_ON_1 = 10,
                    // Variant should still be usable within its visibility
                    pub PUB1 = PRIV_ON_1 * 2,
                }
            }
        }
        mod priv2 {
            c_enum! {
                #[repr(u16)]
                pub enum e2 {
                    pub PRIV_ON_1 = 42,
                    pub PUB2 = PRIV_ON_1 * 2,
                }
            }
        }

        use priv1::*;
        use priv2::*;

        assert_eq!(TypeId::of::<e1>(), TypeId::of::<u8>());
        assert_eq!(TypeId::of::<e2>(), TypeId::of::<u16>());
        assert_eq!(PUB1, 10u8 * 2);
        assert_eq!(PUB2, 42u16 * 2);
        // Verify that the default is private. If `PRIV_ON_1` was actually
        // public in `priv1`, this would be an ambiguous import and/or type
        // mismatch error.
        assert_eq!(PRIV_ON_1, 42u16);
    }

    #[test]
    fn c_enum_attrs() {
        // Note this can't work with `#[cfg]` currently because our expansion uses `previous + 1`
        c_enum! {
            pub enum e {
                VAR0,
                WITH_CFG = if cfg!(target_arch = "x86_64") { 86 } else { 1234 },
                #[deprecated]
                DEPRECATED,
                NOT_DEPRECATED,
            }
        }

        if cfg!(target_arch = "x86_64") {
            assert_eq!(WITH_CFG, 86);
        } else {
            assert_eq!(WITH_CFG, 1234);
        }

        #[expect(deprecated)]
        let _ = DEPRECATED;
        #[deny(deprecated)]
        let _ = NOT_DEPRECATED;
    }

    #[test]
    #[deny(unused_unsafe)]
    fn f_safety() {
        // Enusure the created functions are safe / unsafe / const as expected
        f! {
            pub unsafe fn unsafe_foo() -> u32 { 100 }
            pub const unsafe fn const_unsafe_foo() -> u32 { 101 }
            pub safe fn safe_foo() -> u32 { 200 }
            pub const safe fn const_safe_foo() -> u32 { 201 }
        }

        assert_eq!(unsafe { unsafe_foo() }, 100u32);
        assert_eq!(const { unsafe { const_unsafe_foo() } }, 101u32);
        assert_eq!(safe_foo(), 200u32);
        assert_eq!(const { const_safe_foo() }, 201u32);

        // Check the ABI
        let _: unsafe extern "C" fn() -> u32 = unsafe_foo;
        let _: unsafe extern "C" fn() -> u32 = const_unsafe_foo;
        let _: extern "C" fn() -> u32 = safe_foo;
        let _: extern "C" fn() -> u32 = const_safe_foo;
    }

    fn type_id_of_val<T: 'static>(_: &T) -> TypeId {
        TypeId::of::<T>()
    }

    #[test]
    fn test_offset_of() {
        #[repr(C)]
        struct Off1 {
            a: u8,
            b: u32,
            c: Off2,
            d: u64,
        }

        #[repr(C)]
        #[repr(align(128))]
        struct Off2 {}

        assert_eq!(core::mem::offset_of!(Off1, a), offset_of!(Off1, a));
        assert_eq!(core::mem::offset_of!(Off1, b), offset_of!(Off1, b));
        assert_eq!(core::mem::offset_of!(Off1, c), offset_of!(Off1, c));
        assert_eq!(core::mem::offset_of!(Off1, d), offset_of!(Off1, d));
    }

    #[test]
    fn s2_is_non_exhaustive() {
        // Without `#[exhaustive]`, the record should have an additional field
        // added at the end. If this test compiles, it has it.
        s2! {
            struct Something {
                a: u32,
            }
        }

        let s = Something::default();
        assert_eq!(s.__non_exhaustive, ());
    }

    #[test]
    fn s2_uses_exhaustive() {
        // With `#[exhaustive]`, the record should be regurgitated as-is. If
        // this test compiles, then it works.
        s2! {
            #[exhaustive]
            struct Something {
                a: u32,
            }
        }

        #[allow(unused)]
        let s = Something {
            a: Default::default(),
        };
    }

    #[test]
    fn s2_uses_mixed_exhaustive() {
        // `#[exhaustive]` should work when sandwiched between attributes. If
        // the test compiles, then it works.
        s2! {
            #[repr(align(8))]
            #[exhaustive]
            #[repr(align(2))]
            struct Something {
                a: u32,
            }
        }

        #[allow(unused)]
        let s = Something {
            a: Default::default(),
        };
    }

    #[test]
    fn s2_uses_custom_default() {
        // A non-default value proves `custom_default` is used rather than a
        // derived default.
        s2! {
            struct CustomDefault {
                a: u32,
                #[custom_default([1; 64])]
                buf: [u8; 64],
            }
        }

        let s = CustomDefault::default();
        assert_eq!(s.a, 0);
        assert_eq!(s.buf, [1u8; 64]);
    }

    #[test]
    fn s2_keeps_field_attrs() {
        // If `custom_default` stripping ate the other field attributes, the two
        // `a` fields would collide.
        s2! {
            struct FieldAttrs {
                #[cfg(target_arch = "x86_64")]
                a: u8,
                #[cfg(not(target_arch = "x86_64"))]
                a: u64,
            }
        }

        let s = FieldAttrs::default();
        #[cfg(target_arch = "x86_64")]
        assert_eq!(s.a, 0u8);
        #[cfg(not(target_arch = "x86_64"))]
        assert_eq!(s.a, 0u64);
    }

    #[test]
    fn s2_single_cfg_field() {
        // This field only exists on x86_64, so its default init needs the same
        // `cfg` or `Default` won't build on other arches.
        s2! {
            struct SingleCfg {
                common: u32,
                #[cfg(target_arch = "x86_64")]
                x86_only: u64,
            }
        }

        let s = SingleCfg::default();
        assert_eq!(s.common, 0);
        #[cfg(target_arch = "x86_64")]
        assert_eq!(s.x86_only, 0);
    }

    #[test]
    fn s_no_extra_traits2_zeroes_union() {
        // A union field's default is supplied by
        // `custom_default(unsafe { mem::zeroed })`.
        s_no_extra_traits2! {
            union U {
                a: u32,
                b: f32,
            }

            struct HasUnion {
                x: u16,
                #[custom_default(unsafe { ::core::mem::zeroed::<U>() })]
                u: U,
            }
        }

        let s = HasUnion::default();
        assert_eq!(s.x, 0);
        assert_eq!(unsafe { s.u.a }, 0);
    }

    #[test]
    fn s2_keeps_struct_cfg() {
        // The opposite of the configured-out types in `macro_checks`. With the
        // `cfg` true the type and its `Default` both exist, and the other
        // attributes still apply.
        s2! {
            #[cfg(true)]
            #[repr(align(8))]
            /// a doc comment
            struct EnabledCfg {
                a: u32,
                #[cfg(target_arch = "x86_64")]
                x86_only: u8,
                #[custom_default([1; 40])]
                buf: [u8; 40],
            }
        }

        let s = EnabledCfg::default();
        assert_eq!(s.a, 0);
        assert_eq!(s.buf, [1u8; 40]);
        assert_eq!(align_of::<EnabledCfg>(), 8);
        #[cfg(target_arch = "x86_64")]
        assert_eq!(s.x86_only, 0);
    }

    #[test]
    fn s2_union_default() {
        s_no_extra_traits2! {
            #[unsafe(union_default_via_zeroed)]
            union Foo {
                a: u32,
                b: u64
            }

            #[expect(unused)]
            union Bar {
                a: u32,
                b: u64
            }
        }

        let f: Foo = Foo::default();
        assert_eq!(unsafe { f.b }, 0);

        // Ensure it isn't always implemented, otherwise we would get a conflict.
        impl Default for Bar {
            fn default() -> Self {
                unimplemented!()
            }
        }
    }
}

#[cfg(test)]
#[allow(unused)]
mod macro_checks {
    use core::prelude::v1::*;

    s! {
        pub struct S1 {
            pub a: u32,
            b: u32,
        }

        struct S1Priv {
            pub a: u32,
            b: u32,
        }
    }

    s_no_extra_traits! {
        pub struct S2 {
            pub a: u32,
            b: u32,
        }

        struct S2Priv {
            pub a: u32,
            b: u32,
        }

        pub union U2 {
            pub a: u32,
            b: f32,
        }

        union U2Priv {
            pub a: u32,
            b: f32,
        }
    }

    extern_ty! {
        type Foo;
        pub type Bar;
    }

    s2! {
        pub struct S3 {
            pub a: u32,
            #[custom_default([1; 64])]
            pub buf: [u8; 64],
        }

        struct S3Priv {
            pub a: u32,
            b: u32,
        }
    }

    s_no_extra_traits2! {
        pub union U3 {
            pub a: u32,
            b: f32,
        }

        pub struct S4 {
            pub a: u32,
            #[custom_default(unsafe { ::core::mem::zeroed::<U3>() })]
            pub u: U3,
        }
    }

    fn assert_impls_default<T: Default>() {}

    fn check_default() {
        assert_impls_default::<S3>();
        assert_impls_default::<S4>();
    }

    // Types configured out entirely, checking that the generated impls carry
    // the same `cfg` as the type. Without it they fail to compile with "cannot
    // find type".
    s2! {
        #[cfg(false)]
        pub struct S5 {
            pub a: u32,
            #[custom_default([1; 64])]
            pub buf: [u8; 64],
        }
    }

    s_no_extra_traits! {
        #[cfg(false)]
        pub union U4 {
            pub a: u32,
            b: f32,
        }
    }

    s_no_extra_traits2! {
        #[cfg(false)]
        pub union U5 {
            pub a: u32,
            b: f32,
        }

        #[cfg(false)]
        pub struct S6 {
            pub a: u32,
        }
    }

    // The generated impls name the type and its fields, so they need to allow
    // deprecation.  `deny` turns the warning into an error if that ever stops
    // being the case.
    mod deprecated_checks {
        #![deny(deprecated)]

        s2! {
            #[deprecated(since = "0.0.0", note = "check that generated impls don't warn")]
            pub struct S7 {
                pub a: u32,
            }
        }

        s_no_extra_traits! {
            #[deprecated(since = "0.0.0", note = "check that generated impls don't warn")]
            pub union U6 {
                pub a: u32,
                b: f32,
            }
        }

        s_no_extra_traits2! {
            #[deprecated(since = "0.0.0", note = "check that generated impls don't warn")]
            pub union U7 {
                pub a: u32,
                b: f32,
            }
        }
    }
}