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
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
// All the uses of usize as isize are for struct offsets,
// which as far as I am aware are all smaller than isize::MAX
#![allow(clippy::ptr_offset_with_cast)]

#[macro_use]
mod impl_fieldoffset_methods;

mod repr_offset_ext_impls;

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

use crate::{
    alignment::{Aligned, Alignment, CombineAlignment, CombineAlignmentOut, Unaligned},
    offset_calc::GetNextFieldOffset,
    utils::Mem,
};

use crate::get_field_offset::FieldOffsetWithVis;

use core::{
    fmt::{self, Debug},
    marker::PhantomData,
    ops::Add,
};

/// Represents the offset of a (potentially nested) field inside a type.
///
/// # Type parameters
///
/// The type parameters are:
///
/// - `S`(for `struct`): the struct that contains the field that this is an offset for.
///
/// - `F`(for field): the type of the field this is an offset for.
///
/// - `A`(for alignment):
/// Is [`Aligned`] if this offset is for [an aligned field](#alignment-guidelines)
/// within the `S` struct,
/// [`Unaligned`] if it is for [an unaligned field](#alignment-guidelines).
/// This changes which methods are available,and the implementation of many of them.
///
/// # Safety
///
/// ### Alignment
///
/// All the unsafe methods for `FieldOffset<_, _, Aligned>`
/// that move/copy the field require that
/// the passed in pointers are aligned,
/// while the ones for `FieldOffset<_, _, Unaligned>` do not.
///
/// Because passing unaligned pointers to `FieldOffset<_, _, Aligned>` methods
/// causes undefined behavior,
/// you must be careful when accessing a nested field in `#[repr(C, packed)]` structs.
///
/// For an example of how to correctly access nested fields inside of
/// `#[repr(C, packed)]` structs [look here](#nested-field-in-packed).
///
/// <span id="alignment-guidelines"></span>
/// # Field alignment guidelines
///
/// A non-nested field is:
///
/// - Aligned: if the type that contains the field is
/// `#[repr(C)]`/`#[repr(C, align(...))]`/`#[repr(transparent)]`.
///
/// - Unaligned: if the type that contains the field is `#[repr(C, packed(....))]`,
/// and the packing is smaller than the alignment of the field type.<br>
/// Note that type alignment can vary across platforms,
/// so `FieldOffset<S, F, Unaligned>`(as opposed to `FieldOffset<S, F, Aligned>`)
/// is safest when `S` is a `#[repr(C, packed)]` type.
///
/// A nested field is unaligned if any field in the chain of field accesses to the
/// nested field (ie: `foo` and `bar` and `baz` in `foo.bar.baz`)
/// is unaligned according to the rules for non-nested fields described in this section.
///
///
/// # Examples
///
/// ### No Macros
///
/// This example demonstrates how you can construct `FieldOffset` without macros.
///
/// You can use the [`ReprOffset`] derive macro or [`unsafe_struct_field_offsets`] macro
/// to construct the constants more conveniently (and in a less error-prone way).
///
/// ```rust
/// # #![deny(safe_packed_borrows)]
/// use repr_offset::{Aligned, FieldOffset};
///
/// use std::mem;
///
///
/// fn main(){
///     let mut foo = Foo{ first: 3u16, second: 5, third: None };
///
///     *Foo::OFFSET_FIRST.get_mut(&mut foo) = 13;
///     *Foo::OFFSET_SECOND.get_mut(&mut foo) = 21;
///     *Foo::OFFSET_THIRD.get_mut(&mut foo) = Some(34);
///
///     assert_eq!( foo, Foo{ first: 13, second: 21, third: Some(34) } );
/// }
///
///
/// #[repr(C)]
/// #[derive(Debug,PartialEq)]
/// struct Foo<T>{
///     first: T,
///     second: u32,
///     third: Option<T>,
/// }
///
/// impl<T> Foo<T>{
///     const OFFSET_FIRST: FieldOffset<Self, T, Aligned> = unsafe{ FieldOffset::new(0) };
///
///     const OFFSET_SECOND: FieldOffset<Self, u32, Aligned> = unsafe{
///         Self::OFFSET_FIRST.next_field_offset()
///     };
///
///     const OFFSET_THIRD: FieldOffset<Self, Option<T>, Aligned> = unsafe{
///         Self::OFFSET_SECOND.next_field_offset()
///     };
/// }
///
/// ```
///
/// <span id="nested-field-in-packed"></span>
/// ### Accessing Nested Fields
///
/// This example demonstrates how to access nested fields in a `#[repr(C, packed)]` struct,
/// using the [`GetFieldOffset`] trait implemented by the [`ReprOffset`] derive,
/// through the [`off`](./macro.off.html) macro.
///
/// ```rust
/// # #![deny(safe_packed_borrows)]
#[cfg_attr(feature = "derive", doc = "use repr_offset::ReprOffset;")]
#[cfg_attr(not(feature = "derive"), doc = "use repr_offset_derive::ReprOffset;")]
/// use repr_offset::{
///     alignment::{Aligned, Unaligned},
///     off,
///     FieldOffset,
/// };
///
/// #[repr(C, packed)]
/// #[derive(ReprOffset)]
/// struct Pack{
///     x: u8,
///     y: NestedC,
/// }
///
/// #[repr(C)]
/// #[derive(ReprOffset)]
/// struct NestedC{
///     name: &'static str,
///     years: usize,
/// }
///
/// let this = Pack{
///     x: 0,
///     y: NestedC{ name: "John", years: 13 },
/// };
///
/// let off_y: FieldOffset<Pack, NestedC, Unaligned> = off!(y);
///
/// let off_name: FieldOffset<Pack, &'static str, Unaligned> = off!(y.name);
///
/// // You can also get the FieldOffset for a nested field like this.
/// let off_years: FieldOffset<Pack, usize, Unaligned> = off_y.add(off!(years));
///
/// // The this argument is required to call FieldOffset methods,
/// // infering the S type parameter of FieldOffset from `this`.
/// let _ = off!(this; y.years);
///
/// assert_eq!(off_name.get_copy(&this), "John" );
/// assert_eq!(off_years.get_copy(&this), 13 );
///
/// unsafe{
///     let nested_ptr: *const NestedC = off_y.get_ptr(&this);
///
///     // This code is undefined behavior,
///     // because `NestedC`'s offsets require the passed in pointer to be aligned.
///     //
///     // assert_eq!(NestedC::OFFSET_NAME.read(nested_ptr), "John" );
///     // assert_eq!(NestedC::OFFSET_YEARS.read(nested_ptr), 13 );
///
///     // This is fine though,because the offsets were turned into
///     // `FieldOffset<_, _, Unaligned>` with `.to_unaligned()`.
///     assert_eq!( NestedC::OFFSET_NAME.to_unaligned().read(nested_ptr), "John" );
///     assert_eq!( NestedC::OFFSET_YEARS.to_unaligned().read(nested_ptr), 13 );
///
/// }
/// ```
///
/// [`Aligned`]: ./alignment/struct.Aligned.html
/// [`Unaligned`]: ./alignment/struct.Unaligned.html
///
/// [`ReprOffset`]: ./derive.ReprOffset.html
/// [`unsafe_struct_field_offsets`]: ./macro.unsafe_struct_field_offsets.html
/// [`GetFieldOffset`]: ./get_field_offset/trait.GetFieldOffset.html
///
#[repr(transparent)]
pub struct FieldOffset<S, F, A> {
    offset: usize,
    #[doc(hidden)]
    pub tys: FOGhosts<S, F, A>,
}

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

#[doc(hidden)]
pub struct FOGhosts<S, F, A> {
    pub struct_: PhantomData<fn() -> S>,
    pub field: PhantomData<fn() -> F>,
    pub alignment: PhantomData<fn() -> A>,
}

impl<S, F, A> Copy for FOGhosts<S, F, A> {}

impl<S, F, A> Clone for FOGhosts<S, F, A> {
    #[inline(always)]
    fn clone(&self) -> Self {
        *self
    }
}

impl<S, F, A> FOGhosts<S, F, A> {
    const NEW: Self = Self {
        struct_: PhantomData,
        field: PhantomData,
        alignment: PhantomData,
    };
}

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

#[doc(hidden)]
#[repr(transparent)]
pub struct FOAssertStruct<S, F, A> {
    pub offset: FieldOffset<S, F, A>,
    pub struct_: PhantomData<fn() -> S>,
}

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

impl_cmp_traits_for_offset! {
    impl[S, F, A] FieldOffset<S, F, A>
}

impl<S, F, A> Debug for FieldOffset<S, F, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FieldOffset")
            .field("offset", &self.offset)
            .finish()
    }
}

impl<S, F, A> Copy for FieldOffset<S, F, A> {}

impl<S, F, A> Clone for FieldOffset<S, F, A> {
    #[inline(always)]
    fn clone(&self) -> Self {
        *self
    }
}

impl<S, F, A> FieldOffset<S, F, A> {
    /// Constructs this `FieldOffset` from the offset of the field.
    ///
    /// # Safety
    ///
    /// Callers must ensure all of these:
    ///
    /// - `S` must be a `#[repr(C)]` or `#[repr(transparent)]` struct
    /// (optionally with `align` or `packed` attributes).
    ///
    /// - `offset` must be the byte offset of a field of type `F` inside the struct `S`.
    ///
    /// - The `A` type parameter must be [`Unaligned`]
    /// if the field [is unaligned](#alignment-guidelines),
    /// or [`Aligned`] if [it is aligned](#alignment-guidelines).
    ///
    /// # Example
    ///
    /// Constructing the `FieldOffset`s of a packed struct.
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::{Aligned, FieldOffset, Unaligned};
    ///
    /// let this = Packed{ x: 3, y: 5, z: "huh" };
    ///
    /// assert_eq!( OFFSET_X.get_copy(&this), 3 );
    /// assert_eq!( OFFSET_Y.get_copy(&this), 5 );
    /// assert_eq!( OFFSET_Z.get_copy(&this), "huh" );
    ///
    /// #[repr(C, packed)]
    /// struct Packed{
    ///     x: u8,
    ///     y: u32,
    ///     z: &'static str,
    /// }
    ///
    /// // `u8` is always aligned.
    /// const OFFSET_X: FieldOffset<Packed, u8, Aligned> = unsafe{
    ///     FieldOffset::new(0)
    /// };
    /// const OFFSET_Y: FieldOffset<Packed, u32, Unaligned> = unsafe{
    ///     OFFSET_X.next_field_offset()
    /// };
    /// const OFFSET_Z: FieldOffset<Packed, &'static str, Unaligned> = unsafe{
    ///     OFFSET_Y.next_field_offset()
    /// };
    ///
    /// ```
    /// [`Aligned`]: ./alignment/struct.Aligned.html
    /// [`Unaligned`]: ./alignment/struct.Unaligned.html
    #[inline(always)]
    pub const unsafe fn new(offset: usize) -> Self {
        Self {
            offset,
            tys: FOGhosts::NEW,
        }
    }

    // This must be kept private
    #[inline(always)]
    const fn priv_new(offset: usize) -> Self {
        Self {
            offset,
            tys: FOGhosts::NEW,
        }
    }

    /// Constructs a `FieldOffset` by calculating the offset of the next field.
    ///
    /// # Safety
    ///
    /// Callers must ensure that:
    ///
    /// - `Next` is the type of the field after the one that this is an offset for.
    ///
    /// - `NextA` must be [`Unaligned`] if the field [is unaligned](#alignment-guidelines),
    /// or [`Aligned`] if [it is aligned](#alignment-guidelines).
    ///
    /// # Example
    ///
    /// Constructing the `FieldOffset`s of a `#[repr(C, align(16))]` struct.
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::{Aligned, FieldOffset};
    ///
    /// let this = ReprAligned{ foo: true, bar: Some('8'), baz: 55 };
    ///
    /// assert_eq!( OFFSET_FOO.get_copy(&this), true );
    /// assert_eq!( OFFSET_BAR.get_copy(&this), Some('8') );
    /// assert_eq!( OFFSET_BAZ.get_copy(&this), 55 );
    ///
    ///
    /// #[repr(C, align(16))]
    /// struct ReprAligned{
    ///     foo: bool,
    ///     bar: Option<char>,
    ///     baz: u64,
    /// }
    ///
    /// const OFFSET_FOO: FieldOffset<ReprAligned, bool, Aligned> = unsafe{
    ///     FieldOffset::new(0)
    /// };
    /// const OFFSET_BAR: FieldOffset<ReprAligned, Option<char>, Aligned> = unsafe{
    ///     OFFSET_FOO.next_field_offset()
    /// };
    /// const OFFSET_BAZ: FieldOffset<ReprAligned, u64, Aligned> = unsafe{
    ///     OFFSET_BAR.next_field_offset()
    /// };
    ///
    /// ```
    ///
    /// [`Aligned`]: ./alignment/struct.Aligned.html
    /// [`Unaligned`]: ./alignment/struct.Unaligned.html
    pub const unsafe fn next_field_offset<Next, NextA>(self) -> FieldOffset<S, Next, NextA> {
        let offset = GetNextFieldOffset {
            previous_offset: self.offset,
            previous_size: Mem::<F>::SIZE,
            container_alignment: Mem::<S>::ALIGN,
            next_alignment: Mem::<Next>::ALIGN,
        }
        .call();

        FieldOffset {
            offset,
            tys: FOGhosts::NEW,
        }
    }
}

impl FieldOffset<(), (), Aligned> {
    /// Constructs a `FieldOffset` where `T` is the struct and the field type.
    pub const fn identity<T>() -> FieldOffset<T, T, Aligned> {
        FieldOffset {
            offset: 0,
            tys: FOGhosts::NEW,
        }
    }
}

impl<S, F> FieldOffset<S, F, Aligned> {
    /// Combines this `FieldOffset` with another one, to access a nested field.
    ///
    /// Note that the resulting `FieldOffset` has the
    /// alignment type parameter (the third one) of `other`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::{Aligned, FieldOffset, Unaligned};
    /// use repr_offset::for_examples::{ReprC, ReprPacked};
    ///
    /// type This = ReprC<char, ReprC<u8, u16>, ReprPacked<u32, u64>>;
    ///
    /// let this: This = ReprC {
    ///     a: '3',
    ///     b: ReprC{ a: 5u8, b: 8u16, c: (), d: () },
    ///     c: ReprPacked{ a: 13u32, b: 21u64, c: (), d: () },
    ///     d: (),
    /// };
    ///
    /// assert_eq!( OFFSET_B_A.get_copy(&this), 5 );
    /// assert_eq!( OFFSET_C_A.get_copy(&this), 13 );
    ///
    /// // This is the FieldOffset of the `.b.a` nested field.
    /// const OFFSET_B_A: FieldOffset<This, u8, Aligned> =
    ///     ReprC::OFFSET_B.add(ReprC::OFFSET_A);
    ///
    /// // This is the FieldOffset of the `.c.a` nested field.
    /// //
    /// // The alignment type parameter of the combined FieldOffset is`Unaligned` if
    /// // either FieldOffset has `Unaligned` as the `A` type parameter.
    /// const OFFSET_C_A: FieldOffset<This, u32, Unaligned> =
    ///     ReprC::OFFSET_C.add(ReprPacked::OFFSET_A);
    ///
    /// ```
    ///
    #[inline(always)]
    pub const fn add<F2, A2>(self, other: FieldOffset<F, F2, A2>) -> FieldOffset<S, F2, A2> {
        FieldOffset::priv_new(self.offset + other.offset)
    }
}

impl<S, F> FieldOffset<S, F, Unaligned> {
    /// Combines this `FieldOffset` with another one, to access a nested field.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::{FieldOffset, Unaligned};
    /// use repr_offset::for_examples::{ReprC, ReprPacked};
    ///
    /// type This = ReprPacked<char, ReprC<u8, u16>, ReprPacked<u32, u64>>;
    ///
    /// let this: This = ReprPacked {
    ///     a: '3',
    ///     b: ReprC{ a: 34u8, b: 55u16, c: (), d: () },
    ///     c: ReprPacked{ a: 89u32, b: 144u64, c: (), d: () },
    ///     d: (),
    /// };
    ///
    /// assert_eq!( OFFSET_B_A.get_copy(&this), 34 );
    /// assert_eq!( OFFSET_C_A.get_copy(&this), 89 );
    ///
    /// // This is the FieldOffset of the `.b.a` nested field.
    /// const OFFSET_B_A: FieldOffset<This, u8, Unaligned> =
    ///     ReprPacked::OFFSET_B.add(ReprC::OFFSET_A);
    ///
    /// // This is the FieldOffset of the `.c.a` nested field.
    /// const OFFSET_C_A: FieldOffset<This, u32, Unaligned> =
    ///     ReprPacked::OFFSET_C.add(ReprPacked::OFFSET_A);
    ///
    /// ```
    ///
    #[inline(always)]
    pub const fn add<F2, A2>(self, other: FieldOffset<F, F2, A2>) -> FieldOffset<S, F2, Unaligned> {
        FieldOffset::priv_new(self.offset + other.offset)
    }
}

/// Equivalent to the inherent `FieldOffset::add` method,
/// that one can be ran at compile-time(this one can't).
///
/// # Example
///
/// ```rust
/// # #![deny(safe_packed_borrows)]
/// use repr_offset::{Aligned, FieldOffset, Unaligned};
/// use repr_offset::for_examples::{ReprC, ReprPacked};
///
/// type This = ReprC<char, ReprC<u8, u16>, ReprPacked<u32, u64>>;
///
/// let this: This = ReprC {
///     a: '3',
///     b: ReprC{ a: 5u8, b: 8u16, c: (), d: () },
///     c: ReprPacked{ a: 13u32, b: 21u64, c: (), d: () },
///     d: (),
/// };
///
/// // This is the FieldOffset of the `.b.a` nested field.
/// let offset_b_b = ReprC::OFFSET_B + ReprC::OFFSET_B;
///
/// // This is the FieldOffset of the `.c.a` nested field.
/// let offset_c_b = ReprC::OFFSET_C + ReprPacked::OFFSET_B;
///
/// assert_eq!( offset_b_b.get_copy(&this), 8 );
/// assert_eq!( offset_c_b.get_copy(&this), 21 );
///
/// ```
///
impl<S, F, A, F2, A2> Add<FieldOffset<F, F2, A2>> for FieldOffset<S, F, A>
where
    A: CombineAlignment<A2>,
    A2: Alignment,
{
    type Output = FieldOffset<S, F2, CombineAlignmentOut<A, A2>>;

    #[inline(always)]
    fn add(self, other: FieldOffset<F, F2, A2>) -> Self::Output {
        FieldOffset::priv_new(self.offset + other.offset)
    }
}

impl<S, F, A> FieldOffset<S, F, A> {
    /// The offset (in bytes) of the `F` field in the `S` struct.
    ///
    /// # Example
    ///
    /// This example demonstrates this method with a `#[repr(C, packed)]` struct.
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// type Normal = ReprPacked<u8, u16, u32, u64>;
    /// type Reversed = ReprPacked<u64, u32, u16, u8>;
    ///
    /// assert_eq!( Normal::OFFSET_A.offset(), 0 );
    /// assert_eq!( Normal::OFFSET_B.offset(), 1 );
    /// assert_eq!( Normal::OFFSET_C.offset(), 3 );
    /// assert_eq!( Normal::OFFSET_D.offset(), 7 );
    ///
    /// assert_eq!( Reversed::OFFSET_A.offset(), 0 );
    /// assert_eq!( Reversed::OFFSET_B.offset(), 8 );
    /// assert_eq!( Reversed::OFFSET_C.offset(), 12 );
    /// assert_eq!( Reversed::OFFSET_D.offset(), 14 );
    ///
    ///
    /// ```
    #[inline(always)]
    pub const fn offset(self) -> usize {
        self.offset
    }
}

impl<S, F, A> FieldOffset<S, F, A> {
    /// Converts this FieldOffset into a [`FieldOffsetWithVis`].
    ///
    /// # Safety
    ///
    /// The `V` type parameter must be:
    /// - `[`IsPublic`]`: When the field is `pub`.
    ///
    /// - [`IsPrivate`]: When the field has the default (private) visibility,
    /// or has a visibility smaller or equal to `pub(crate)`.
    ///
    /// The `FN` type parameter must be the name of the field using the
    /// `repr_offset::tstr::TS` macro,
    /// eg: `TS!(foo)` for the `foo` field.
    ///
    /// [`IsPublic`]: ./privacy/struct.IsPublic.html
    /// [`IsPrivate`]: ./privacy/struct.IsPrivate.html
    ///
    /// [`FieldOffsetWithVis`] ./get_field_offset/struct.FieldOffsetWithVis.html
    ///
    #[inline(always)]
    pub const unsafe fn with_vis<V, FN>(self) -> FieldOffsetWithVis<S, V, FN, F, A> {
        FieldOffsetWithVis::from_fieldoffset(self)
    }
}

impl<S, F, A> FieldOffset<S, F, A> {
    /// Changes the `S` type parameter, most useful for `#[repr(transparent)]` wrappers.
    ///
    /// # Safety
    ///
    /// Callers must ensure that there is a field of type `F` at the same offset
    /// inside the `S2` type,
    /// and is at least as public as this `FieldOffset`.
    ///
    /// If the `A` type parameter is [`Aligned`],
    /// then the field [must be aligned](#alignment-guidelines)
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = Wrapper(ReprC{
    ///     a: false,
    ///     b: 3u8,
    ///     c: Some('5'),
    ///     d: [8u32, 13u32],
    /// });
    ///
    /// assert_eq!( cast_offset(ReprC::OFFSET_A).get(&this), &false );
    /// assert_eq!( cast_offset(ReprC::OFFSET_B).get(&this), &3u8 );
    /// assert_eq!( cast_offset(ReprC::OFFSET_C).get(&this), &Some('5') );
    /// assert_eq!( cast_offset(ReprC::OFFSET_D).get(&this), &[8u32, 13u32] );
    ///
    ///
    /// #[repr(transparent)]
    /// pub struct Wrapper<T>(pub T);
    ///
    /// pub const fn cast_offset<T,F,A>(offset: FieldOffset<T,F,A>) -> FieldOffset<Wrapper<T>,F,A>{
    ///     // safety: This case is safe because this is a
    ///     // `#[repr(transparent)]` wrapper around `T`
    ///     // where `T` is a public field in the wrapper
    ///     unsafe{ offset.cast_struct() }
    /// }
    ///
    ///
    ///
    /// ```
    ///
    /// [`Aligned`]: ./alignment/struct.Aligned.html
    /// [`Unaligned`]: ./alignment/struct.Unaligned.html
    #[inline(always)]
    pub const unsafe fn cast_struct<S2>(self) -> FieldOffset<S2, F, A> {
        FieldOffset::new(self.offset)
    }

    /// Changes the `F` type parameter.
    ///
    /// # Safety
    ///
    /// Callers must ensure that the `F2` type is compatible with the `F` type,
    /// including size,alignment, and internal layout.
    ///
    /// If the `F` type encodes an invariant,
    /// then callers must ensure that if the field is used as the `F` type
    /// (including the destructor for the type)
    /// that the invariants for that type must be upheld.
    ///
    /// The same applies if the field is used as the `F2` type
    /// (if the returned FieldOffset isn't used,then it would not be used as the `F2` type)
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    ///
    /// use repr_offset::{Aligned, FieldOffset};
    /// use repr_offset::for_examples::ReprC;
    ///
    /// type This = ReprC<u8, u64, (), ()>;
    ///
    /// let this: This = ReprC{ a: 3, b: 5, c: (), d: () };
    ///
    /// unsafe{
    ///     assert_eq!( This::OFFSET_A.cast_field::<i8>().get(&this), &3i8 );
    ///     assert_eq!( This::OFFSET_B.cast_field::<i64>().get(&this), &5i64 );
    /// }
    ///
    /// ```
    /// [safe and valid]:
    /// https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#validity-and-safety-invariant
    #[inline(always)]
    pub const unsafe fn cast_field<F2>(self) -> FieldOffset<S, F2, A> {
        FieldOffset::new(self.offset)
    }

    /// Changes this `FieldOffset` to be for a (potentially) unaligned field.
    ///
    /// This is useful if you want to get a nested field from an unaligned pointer to a
    /// `#[repr(C)]`/`#[repr(C,align())]` struct.
    ///
    /// # Example
    ///
    /// This example demonstrates how you can copy a field
    /// from an unaligned pointer to a `#[repr(C)]` struct.
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::{ReprC, ReprPacked};
    ///
    /// type Inner = ReprC<usize, &'static str>;
    /// type Outer = ReprPacked<u8, Inner>;
    ///
    /// let inner = ReprC { a: 3, b: "5", c: (), d: () };
    /// let outer: Outer = ReprPacked{ a: 21, b: inner, c: (), d: () };
    ///
    /// let inner_ptr: *const Inner = Outer::OFFSET_B.get_ptr(&outer);
    /// unsafe{
    ///     assert_eq!( Inner::OFFSET_A.to_unaligned().read_copy(inner_ptr), 3 );
    ///     assert_eq!( Inner::OFFSET_B.to_unaligned().read_copy(inner_ptr), "5" );
    ///
    ///     // This is undefined behavior,
    ///     // because ReprC's FieldOFfsets require the pointer to be aligned.
    ///     //
    ///     // assert_eq!( Inner::OFFSET_A.read_copy(inner_ptr), 3 );
    ///     // assert_eq!( Inner::OFFSET_B.read_copy(inner_ptr), "5" );
    /// }
    ///
    /// ```
    ///
    #[inline(always)]
    pub const fn to_unaligned(self) -> FieldOffset<S, F, Unaligned> {
        FieldOffset {
            offset: self.offset,
            tys: FOGhosts::NEW,
        }
    }

    /// Changes this `FieldOffset` to be for an aligned field.
    ///
    /// # Safety
    ///
    /// Callers must ensure that [the field is aligned](#alignment-guidelines)
    /// within the `S` type.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::{Aligned, FieldOffset, Unaligned};
    ///
    /// // ReprPacked2 is aligned to 2 bytes.
    /// use repr_offset::for_examples::ReprPacked2;
    ///
    /// type This = ReprPacked2<u8, u16, (), ()>;
    ///
    /// let _: FieldOffset<This, u8, Unaligned> = This::OFFSET_A;
    /// let _: FieldOffset<This, u16, Unaligned> = This::OFFSET_B;
    ///
    /// let this: This = ReprPacked2{ a: 89, b: 144, c: (), d: () };
    ///
    /// unsafe{
    ///     assert_eq!( This::OFFSET_A.to_aligned().get(&this), &89 );
    ///     assert_eq!( This::OFFSET_B.to_aligned().get(&this), &144 );
    /// }
    /// ```
    #[inline(always)]
    pub const unsafe fn to_aligned(self) -> FieldOffset<S, F, Aligned> {
        FieldOffset::new(self.offset)
    }
}

impl<S, F> FieldOffset<S, F, Aligned> {
    /// Gets a reference to the field that this is an offset for.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: '@', b: 21u8, c: (), d: () };
    ///
    /// assert_eq!( ReprC::OFFSET_A.get(&this), &'@' );
    /// assert_eq!( ReprC::OFFSET_B.get(&this), &21u8 );
    ///
    /// ```
    #[inline(always)]
    pub fn get(self, base: &S) -> &F {
        unsafe { impl_fo!(fn get<S, F, Aligned>(self, base)) }
    }

    /// Gets a mutable reference to the field that this is an offset for.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: "what", b: '?', c: (), d: () };
    ///
    /// assert_eq!( ReprC::OFFSET_A.get_mut(&mut this), &mut "what" );
    /// assert_eq!( ReprC::OFFSET_B.get_mut(&mut this), &mut '?' );
    ///
    /// ```
    #[inline(always)]
    pub fn get_mut(self, base: &mut S) -> &mut F {
        unsafe { impl_fo!(fn get_mut<S, F, Aligned>(self, base)) }
    }

    /// Copies the aligned field that this is an offset for.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: Some(false), b: [8i32, 13, 21], c: (), d: () };
    ///
    /// assert_eq!( ReprC::OFFSET_A.get_copy(&this), Some(false) );
    /// assert_eq!( ReprC::OFFSET_B.get_copy(&this), [8i32, 13, 21] );
    ///
    /// ```
    ///
    /// This method can't be called for non-Copy fields.
    /// ```compile_fail
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: vec![0, 1, 2, 3], b: (), c: (), d: () };
    ///
    /// let _ = ReprC::OFFSET_A.get_copy(&this);
    /// ```
    #[inline(always)]
    pub fn get_copy(self, base: &S) -> F
    where
        F: Copy,
    {
        unsafe { impl_fo!(fn get_copy<S, F, Aligned>(self, base)) }
    }
}

impl<S, F, A> FieldOffset<S, F, A> {
    /// Gets a raw pointer to a field from a reference to the `S` struct.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr_a = ReprPacked::OFFSET_A.get_ptr(&this);
    /// // A `u8` is always aligned,so a `.read()` is fine.
    /// assert_eq!( unsafe{ ptr_a.read() }, 3u8 );
    ///
    /// let ptr_b = ReprPacked::OFFSET_B.get_ptr(&this);
    /// // ReprPacked has an alignment of 1,
    /// // so this u16 field has to be copied with `.read_unaligned()`.
    /// assert_eq!( unsafe{ ptr_b.read_unaligned() }, 5u16 );
    ///
    /// ```
    #[inline(always)]
    pub fn get_ptr(self, base: &S) -> *const F {
        unsafe { impl_fo!(fn get_ptr<S, F, A>(self, base)) }
    }

    /// Gets a mutable raw pointer to a field from a mutable reference to the `S` struct.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let mut this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr_a = ReprPacked::OFFSET_A.get_mut_ptr(&mut this);
    /// unsafe{
    ///     // A `u8` is always aligned,so a `.read()` is fine.
    ///     assert_eq!( ptr_a.read(), 3u8 );
    ///     ptr_a.write(103);
    ///     assert_eq!( ptr_a.read(), 103 );
    /// }
    ///
    /// let ptr_b = ReprPacked::OFFSET_B.get_mut_ptr(&mut this);
    /// unsafe{
    ///     // ReprPacked has an alignment of 1,
    ///     // so this u16 field has to be read with `.read_unaligned()`.
    ///     assert_eq!( ptr_b.read_unaligned(), 5u16 );
    ///     ptr_b.write_unaligned(105);
    ///     assert_eq!( ptr_b.read_unaligned(), 105 );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub fn get_mut_ptr(self, base: &mut S) -> *mut F {
        unsafe { impl_fo!(fn get_mut_ptr<S, F, A>(self, base)) }
    }

    /// Gets a raw pointer to a field from a pointer to the `S` struct.
    ///
    /// # Safety
    ///
    /// This has the same safety requirements as the [`<*const T>::offset`] method.
    ///
    /// [`<*const T>::offset`]:
    /// https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr: *const _ = &this;
    ///
    /// unsafe{
    ///     // A `u8` is always aligned,so a `.read()` is fine.
    ///     assert_eq!( ReprPacked::OFFSET_A.raw_get(ptr).read(), 3u8 );
    ///     
    ///     // ReprPacked has an alignment of 1,
    ///     // so this u16 field has to be copied with `.read_unaligned()`.
    ///     assert_eq!( ReprPacked::OFFSET_B.raw_get(ptr).read_unaligned(), 5u16 );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn raw_get(self, base: *const S) -> *const F {
        impl_fo!(fn raw_get<S, F, A>(self, base))
    }

    /// Gets a mutable raw pointer to a field from a pointer to the `S` struct.
    ///
    /// # Safety
    ///
    /// This has the same safety requirements as the [`<*mut T>::offset`] method.
    ///
    /// [`<*mut T>::offset`]:
    /// https://doc.rust-lang.org/std/primitive.pointer.html#method.offset-1
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let mut this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    ///
    /// unsafe{
    ///     let ptr_a = ReprPacked::OFFSET_A.raw_get_mut(ptr);
    ///
    ///     // A `u8` is always aligned,so a `.read()` is fine.
    ///     assert_eq!( ptr_a.read(), 3u8 );
    ///     ptr_a.write(103);
    ///     assert_eq!( ptr_a.read(), 103 );
    ///
    ///
    ///     let ptr_b = ReprPacked::OFFSET_B.raw_get_mut(ptr);
    ///
    ///     // ReprPacked has an alignment of 1,
    ///     // so this u16 field has to be read with `.read_unaligned()`.
    ///     assert_eq!( ptr_b.read_unaligned(), 5u16 );
    ///     ptr_b.write_unaligned(105);
    ///     assert_eq!( ptr_b.read_unaligned(), 105 );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn raw_get_mut(self, base: *mut S) -> *mut F {
        impl_fo!(fn raw_get_mut<S, F, A>(self, base))
    }

    /// Gets a raw pointer to a field from a pointer to the `S` struct.
    ///
    /// # Safety
    ///
    /// While calling this method is not by itself unsafe,
    /// using the pointer returned by this method has the same safety requirements
    /// as the [`<*const T>::wrapping_offset`] method.
    ///
    /// [`<*const T>::wrapping_offset`]:
    /// https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_offset
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr_a = ReprPacked::OFFSET_A.wrapping_raw_get(&this);
    /// // A `u8` is always aligned,so a `.read()` is fine.
    /// assert_eq!( unsafe{ ptr_a.read() }, 3u8 );
    ///
    /// let ptr_b = ReprPacked::OFFSET_B.wrapping_raw_get(&this);
    /// // ReprPacked has an alignment of 1,
    /// // so this u16 field has to be copied with `.read_unaligned()`.
    /// assert_eq!( unsafe{ ptr_b.read_unaligned() }, 5u16 );
    ///
    /// ```
    #[inline(always)]
    pub fn wrapping_raw_get(self, base: *const S) -> *const F {
        (base as *const u8).wrapping_offset(self.offset as isize) as *const F
    }

    /// Gets a mutable raw pointer to a field from a pointer to the `S` struct.
    ///
    /// # Safety
    ///
    /// While calling this method is not by itself unsafe,
    /// using the pointer returned by this method has the same safety requirements
    /// as the [`<*mut T>::wrapping_offset`] method.
    ///
    /// [`<*mut T>::wrapping_offset`]:
    /// https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_offset-1
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::FieldOffset;
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let mut this = ReprPacked{ a: 3u8, b: 5u16, c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    ///
    /// let ptr_a = ReprPacked::OFFSET_A.wrapping_raw_get_mut(ptr);
    /// unsafe{
    ///
    ///     // A `u8` is always aligned,so a `.read()` is fine.
    ///     assert_eq!( ptr_a.read(), 3u8 );
    ///     ptr_a.write(103);
    ///     assert_eq!( ptr_a.read(), 103 );
    /// }
    ///
    /// let ptr_b = ReprPacked::OFFSET_B.wrapping_raw_get_mut(ptr);
    /// unsafe{
    ///
    ///     // ReprPacked has an alignment of 1,
    ///     // so this u16 field has to be read with `.read_unaligned()`.
    ///     assert_eq!( ptr_b.read_unaligned(), 5u16 );
    ///     ptr_b.write_unaligned(105);
    ///     assert_eq!( ptr_b.read_unaligned(), 105 );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub fn wrapping_raw_get_mut(self, base: *mut S) -> *mut F {
        (base as *mut u8).wrapping_offset(self.offset as isize) as *mut F
    }
}

impl<S, F> FieldOffset<S, F, Aligned> {
    /// Copies the aligned field that this is an offset for.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::read`](https://doc.rust-lang.org/std/ptr/fn.read.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: 10u8, b: "20", c: (), d: () };
    ///
    /// let ptr: *const _ = &this;
    /// unsafe{
    ///     assert_eq!( ReprC::OFFSET_A.read_copy(ptr), 10u8 );
    ///     assert_eq!( ReprC::OFFSET_B.read_copy(ptr), "20" );
    /// }
    /// ```
    ///
    /// This method can't be called for non-Copy fields.
    /// ```compile_fail
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: vec![0, 1, 2, 3], b: (), c: (), d: () };
    /// unsafe{
    ///     let _ = ReprC::OFFSET_A.read_copy(&this);
    /// }
    /// ```
    ///
    #[inline(always)]
    pub unsafe fn read_copy(self, base: *const S) -> F
    where
        F: Copy,
    {
        impl_fo!(fn read_copy<S, F, Aligned>(self, base))
    }

    /// Reads the value from the field in `source` without moving it.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::read`](https://doc.rust-lang.org/std/ptr/fn.read.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// use std::mem::ManuallyDrop;
    ///
    /// let this = ManuallyDrop::new(ReprC{
    ///     a: vec![0, 1, 2],
    ///     b: "20".to_string(),
    ///     c: (),
    ///     d: (),
    /// });
    ///
    /// let ptr: *const _ = &*this;
    /// unsafe{
    ///     assert_eq!( ReprC::OFFSET_A.read(ptr), vec![0, 1, 2] );
    ///     assert_eq!( ReprC::OFFSET_B.read(ptr), "20".to_string() );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn read(self, source: *const S) -> F {
        impl_fo!(fn read<S, F, Aligned>(self, source))
    }

    /// Writes `value` ìnto the field in `destination` without dropping the old value of the field.
    ///
    /// This allows uninitialized fields to be initialized,since doing
    /// `*OFFSET_FOO.raw_get_mut(ptr) = value;` would drop uninitialized memory.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::write`](https://doc.rust-lang.org/std/ptr/fn.write.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: 10u8, b: "20", c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    /// unsafe{
    ///     ReprC::OFFSET_A.write(ptr, 13u8);
    ///     ReprC::OFFSET_B.write(ptr, "21");
    /// }
    /// assert_eq!( this.a, 13u8 );
    /// assert_eq!( this.b, "21" );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn write(self, destination: *mut S, value: F) {
        impl_fo!(fn write<S, F, Aligned>(self, destination, value))
    }

    /// Copies the field in `source` into `destination`.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::copy`](https://doc.rust-lang.org/std/ptr/fn.copy.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: 10u8, b: "20", c: (), d: () };
    /// let mut other = ReprC{ a: 0u8, b: "", c: (), d: () };
    ///
    /// let this_ptr: *const _ = &this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprC::OFFSET_A.copy(this_ptr, other_ptr);
    ///     ReprC::OFFSET_B.copy(this_ptr, other_ptr);
    /// }
    /// assert_eq!( this.a, 10u8 );
    /// assert_eq!( this.b, "20" );
    ///
    /// assert_eq!( other.a, 10u8 );
    /// assert_eq!( other.b, "20" );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn copy(self, source: *const S, destination: *mut S) {
        impl_fo!(fn copy<S, F, Aligned>(self, source, destination))
    }

    /// Copies the field in `source` into `destination`,
    /// `source` and `destination` must not overlap.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::copy_nonoverlapping`
    /// ](https://doc.rust-lang.org/std/ptr/fn.copy_nonoverlapping.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let this = ReprC{ a: '#', b: 81, c: (), d: () };
    /// let mut other = ReprC{ a: '_', b: 0, c: (), d: () };
    ///
    /// let this_ptr: *const _ = &this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprC::OFFSET_A.copy_nonoverlapping(this_ptr, other_ptr);
    ///     ReprC::OFFSET_B.copy_nonoverlapping(this_ptr, other_ptr);
    /// }
    /// assert_eq!( this.a, '#' );
    /// assert_eq!( this.b, 81 );
    ///
    /// assert_eq!( other.a, '#' );
    /// assert_eq!( other.b, 81 );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn copy_nonoverlapping(self, source: *const S, destination: *mut S) {
        impl_fo!(fn copy_nonoverlapping<S, F, Aligned>(self, source, destination))
    }

    /// Replaces the value of a field in `destination` with `value`,
    /// returning the old value of the field.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::replace`](https://doc.rust-lang.org/std/ptr/fn.replace.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: [0u8, 1], b: false, c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    /// unsafe{
    ///     assert_eq!( ReprC::OFFSET_A.replace(ptr, [2, 3]), [0u8, 1] );
    ///     assert_eq!( ReprC::OFFSET_B.replace(ptr, true), false );
    /// }
    ///
    /// assert_eq!( this.a, [2u8, 3] );
    /// assert_eq!( this.b, true );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn replace(self, destination: *mut S, value: F) -> F {
        impl_fo!(fn replace<S, F, Aligned>(self, destination, value))
    }

    /// Replaces the value of a field in `destination` with `value`,
    /// returning the old value of the field.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: [0u8, 1], b: false, c: (), d: () };
    ///
    /// assert_eq!( ReprC::OFFSET_A.replace_mut(&mut this, [2, 3]), [0u8, 1] );
    /// assert_eq!( ReprC::OFFSET_B.replace_mut(&mut this, true), false );
    ///
    /// assert_eq!( this.a, [2u8, 3] );
    /// assert_eq!( this.b, true );
    ///
    /// ```
    #[inline(always)]
    pub fn replace_mut(self, destination: &mut S, value: F) -> F {
        unsafe { impl_fo!(fn replace_mut<S, F, Aligned>(self, destination, value)) }
    }

    /// Swaps the values of a field between the `left` and `right` pointers.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::swap`](https://doc.rust-lang.org/std/ptr/fn.swap.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: '=', b: 64u16, c: (), d: () };
    /// let mut other = ReprC{ a: '!', b: 255u16, c: (), d: () };
    ///
    /// let this_ptr: *mut _ = &mut this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprC::OFFSET_A.swap(this_ptr, other_ptr);
    ///     ReprC::OFFSET_B.swap(this_ptr, other_ptr);
    /// }
    /// assert_eq!( this.a, '!' );
    /// assert_eq!( this.b, 255 );
    ///
    /// assert_eq!( other.a, '=' );
    /// assert_eq!( other.b, 64 );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn swap(self, left: *mut S, right: *mut S) {
        impl_fo!(fn swap<S, F, Aligned>(self, left, right))
    }

    /// Swaps the values of a field between the `left` and `right` non-overlapping pointers.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::swap_nonoverlapping`
    /// ](https://doc.rust-lang.org/std/ptr/fn.swap_nonoverlapping.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: [false, true], b: &27u32, c: (), d: () };
    /// let mut other = ReprC{ a: [true, false], b: &81u32, c: (), d: () };
    ///
    /// let this_ptr: *mut _ = &mut this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprC::OFFSET_A.swap_nonoverlapping(this_ptr, other_ptr);
    ///     ReprC::OFFSET_B.swap_nonoverlapping(this_ptr, other_ptr);
    /// }
    /// assert_eq!( this.a, [true, false] );
    /// assert_eq!( this.b, &81 );
    ///
    /// assert_eq!( other.a, [false, true] );
    /// assert_eq!( other.b, &27 );
    ///
    /// ```
    ///
    #[inline(always)]
    pub unsafe fn swap_nonoverlapping(self, left: *mut S, right: *mut S) {
        impl_fo!(fn swap_nonoverlapping<S, F, Aligned>(self, left, right))
    }

    /// Swaps the values of a field between `left` and `right`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprC;
    ///
    /// let mut this = ReprC{ a: [true, true], b: 0x0Fu8, c: (), d: () };
    /// let mut other = ReprC{ a: [false, false], b: 0xF0u8, c: (), d: () };
    ///
    /// ReprC::OFFSET_A.swap_mut(&mut this, &mut other);
    /// ReprC::OFFSET_B.swap_mut(&mut this, &mut other);
    ///
    /// assert_eq!( this.a, [false, false] );
    /// assert_eq!( this.b, 0xF0u8 );
    ///
    /// assert_eq!( other.a, [true, true] );
    /// assert_eq!( other.b, 0x0Fu8 );
    ///
    /// ```
    ///
    #[inline(always)]
    pub fn swap_mut(self, left: &mut S, right: &mut S) {
        unsafe { impl_fo!(fn swap_mut<S, F, Aligned>(self, left, right)) }
    }
}

impl<S, F> FieldOffset<S, F, Unaligned> {
    /// Copies the unaligned field that this is an offset for.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: Some(false), b: [8i32, 13, 21], c: (), d: () };
    ///
    /// assert_eq!( ReprPacked::OFFSET_A.get_copy(&this), Some(false) );
    /// assert_eq!( ReprPacked::OFFSET_B.get_copy(&this), [8i32, 13, 21] );
    ///
    /// ```
    ///
    /// This method can't be called for non-Copy fields.
    /// ```compile_fail
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: vec![0, 1, 2], b: (), c: (), d: () };
    ///
    /// let _ = ReprPacked::OFFSET_A.get_copy(&this);
    ///
    /// ```
    #[inline(always)]
    pub fn get_copy(self, base: &S) -> F
    where
        F: Copy,
    {
        unsafe { impl_fo!(fn get_copy<S, F, Unaligned>(self, base)) }
    }

    /// Copies the unaligned field that this is an offset for.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::read_unaligned`](https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: 10u8, b: "20", c: (), d: () };
    ///
    /// let ptr: *const _ = &this;
    /// unsafe{
    ///     assert_eq!( ReprPacked::OFFSET_A.read_copy(ptr), 10u8 );
    ///     assert_eq!( ReprPacked::OFFSET_B.read_copy(ptr), "20" );
    /// }
    /// ```
    ///
    /// This method can't be called for non-Copy fields.
    /// ```compile_fail
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// let this = ReprPacked{ a: vec![0, 1, 2], b: "20", c: (), d: () };
    ///
    /// unsafe{
    ///     let _ = ReprPacked::OFFSET_A.read_copy(&this);
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn read_copy(self, base: *const S) -> F
    where
        F: Copy,
    {
        impl_fo!(fn read_copy<S, F, Unaligned>(self, base))
    }

    /// Reads the value from the field in `source` without moving it.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::read_unaligned`](https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    ///
    /// use std::mem::ManuallyDrop;
    ///
    /// let this = ManuallyDrop::new(ReprPacked{
    ///     a: vec![0, 1, 2],
    ///     b: "20".to_string(),
    ///     c: (),
    ///     d: (),
    /// });
    ///
    /// let ptr: *const _ = &*this;
    /// unsafe{
    ///     assert_eq!( ReprPacked::OFFSET_A.read(ptr), vec![0, 1, 2] );
    ///     assert_eq!( ReprPacked::OFFSET_B.read(ptr), "20".to_string() );
    /// }
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn read(self, source: *const S) -> F {
        impl_fo!(fn read<S, F, Unaligned>(self, source))
    }

    /// Writes `value` ìnto the field in `source` without dropping the old value of the field.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::write_unaligned`](https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html).
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: 10u8, b: "20", c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    /// unsafe{
    ///     ReprPacked::OFFSET_A.write(ptr, 13u8);
    ///     ReprPacked::OFFSET_B.write(ptr, "21");
    /// }
    /// assert_eq!( moved(this.a), 13u8 );
    /// assert_eq!( moved(this.b), "21" );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn write(self, source: *mut S, value: F) {
        impl_fo!(fn write<S, F, Unaligned>(self, source, value))
    }

    /// Copies the field in `source` into `destination`.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::copy`](https://doc.rust-lang.org/std/ptr/fn.copy.html),
    /// except that `source` and `destination` do not need to be properly aligned.
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    ///
    /// let this = ReprPacked{ a: 10u8, b: "20", c: (), d: () };
    /// let mut other = ReprPacked{ a: 0u8, b: "", c: (), d: () };
    ///
    /// let this_ptr: *const _ = &this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprPacked::OFFSET_A.copy(this_ptr, other_ptr);
    ///     ReprPacked::OFFSET_B.copy(this_ptr, other_ptr);
    /// }
    /// assert_eq!( moved(this.a), 10u8 );
    /// assert_eq!( moved(this.b), "20" );
    ///
    /// assert_eq!( moved(other.a), 10u8 );
    /// assert_eq!( moved(other.b), "20" );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn copy(self, source: *const S, destination: *mut S) {
        impl_fo!(fn copy<S, F, Unaligned>(self, source, destination))
    }

    /// Copies the field in `source` into `destination`,
    /// `source` and `destination` must not overlap.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::copy_nonoverlapping`
    /// ](https://doc.rust-lang.org/std/ptr/fn.copy_nonoverlapping.html),
    /// except that `source` and `destination` do not need to be properly aligned.
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let this = ReprPacked{ a: '#', b: 81, c: (), d: () };
    /// let mut other = ReprPacked{ a: '_', b: 0, c: (), d: () };
    ///
    /// let this_ptr: *const _ = &this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprPacked::OFFSET_A.copy_nonoverlapping(this_ptr, other_ptr);
    ///     ReprPacked::OFFSET_B.copy_nonoverlapping(this_ptr, other_ptr);
    /// }
    /// assert_eq!( moved(this.a), '#' );
    /// assert_eq!( moved(this.b), 81 );
    ///
    /// assert_eq!( moved(other.a), '#' );
    /// assert_eq!( moved(other.b), 81 );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn copy_nonoverlapping(self, source: *const S, destination: *mut S) {
        impl_fo!(fn copy_nonoverlapping<S, F, Unaligned>(self, source, destination))
    }
}

impl<S, F> FieldOffset<S, F, Unaligned> {
    /// Replaces the value of a field in `dest` with `value`,
    /// returning the old value of the field.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::replace`](https://doc.rust-lang.org/std/ptr/fn.replace.html),
    /// except that `dest` does not need to be properly aligned.
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: [0u8, 1], b: false, c: (), d: () };
    ///
    /// let ptr: *mut _ = &mut this;
    /// unsafe{
    ///     assert_eq!( ReprPacked::OFFSET_A.replace(ptr, [2, 3]), [0u8, 1] );
    ///     assert_eq!( ReprPacked::OFFSET_B.replace(ptr, true), false );
    /// }
    ///
    /// assert_eq!( moved(this.a), [2u8, 3] );
    /// assert_eq!( moved(this.b), true );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn replace(self, dest: *mut S, value: F) -> F {
        impl_fo!(fn replace<S, F, Unaligned>(self, dest, value))
    }

    /// Replaces the value of a field in `dest` with `value`,
    /// returning the old value of the field.
    ///
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: [0u8, 1], b: false, c: (), d: () };
    ///
    /// assert_eq!( ReprPacked::OFFSET_A.replace_mut(&mut this, [2, 3]), [0u8, 1] );
    /// assert_eq!( ReprPacked::OFFSET_B.replace_mut(&mut this, true), false );
    ///
    /// assert_eq!( moved(this.a), [2u8, 3] );
    /// assert_eq!( moved(this.b), true );
    ///
    /// ```
    pub fn replace_mut(self, dest: &mut S, value: F) -> F {
        unsafe { impl_fo!(fn replace_mut<S, F, Unaligned>(self, dest, value)) }
    }
}

impl<S, F> FieldOffset<S, F, Unaligned> {
    /// Swaps the values of a field between the `left` and `right` pointers.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::swap`](https://doc.rust-lang.org/std/ptr/fn.swap.html),
    /// except that it does not require aligned pointers.
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: '=', b: 64u16, c: (), d: () };
    /// let mut other = ReprPacked{ a: '!', b: 255u16, c: (), d: () };
    ///
    /// let this_ptr: *mut _ = &mut this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprPacked::OFFSET_A.swap(this_ptr, other_ptr);
    ///     ReprPacked::OFFSET_B.swap(this_ptr, other_ptr);
    /// }
    /// assert_eq!( moved(this.a), '!' );
    /// assert_eq!( moved(this.b), 255 );
    ///
    /// assert_eq!( moved(other.a), '=' );
    /// assert_eq!( moved(other.b), 64 );
    ///
    /// ```
    #[inline(always)]
    pub unsafe fn swap(self, left: *mut S, right: *mut S) {
        impl_fo!(fn swap<S, F, Unaligned>(self, left, right))
    }

    /// Swaps the values of a field between the non-overlapping `left` and `right` pointers.
    ///
    /// # Safety
    ///
    /// This function has the same safety requirements as
    /// [`std::ptr::swap_nonoverlapping`
    /// ](https://doc.rust-lang.org/std/ptr/fn.swap_nonoverlapping.html)
    /// except that it does not require aligned pointers.
    ///
    /// Those safety requirements only apply to the field that this is an offset for,
    /// fields after it or before it don't need to be valid to call this method.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: [false, true], b: &27u32, c: (), d: () };
    /// let mut other = ReprPacked{ a: [true, false], b: &81u32, c: (), d: () };
    ///
    /// let this_ptr: *mut _ = &mut this;
    /// let other_ptr: *mut _ = &mut other;
    /// unsafe{
    ///     ReprPacked::OFFSET_A.swap_nonoverlapping(this_ptr, other_ptr);
    ///     ReprPacked::OFFSET_B.swap_nonoverlapping(this_ptr, other_ptr);
    /// }
    /// assert_eq!( moved(this.a), [true, false] );
    /// assert_eq!( moved(this.b), &81 );
    ///
    /// assert_eq!( moved(other.a), [false, true] );
    /// assert_eq!( moved(other.b), &27 );
    ///
    /// ```
    ///
    #[inline(always)]
    pub unsafe fn swap_nonoverlapping(self, left: *mut S, right: *mut S) {
        impl_fo!(fn swap_nonoverlapping<S, F, Unaligned>(self, left, right))
    }

    /// Swaps the values of a field between `left` and `right`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #![deny(safe_packed_borrows)]
    /// use repr_offset::for_examples::ReprPacked;
    /// use repr_offset::utils::moved;
    ///
    /// let mut this = ReprPacked{ a: [true, true], b: 0x0Fu8, c: (), d: () };
    /// let mut other = ReprPacked{ a: [false, false], b: 0xF0u8, c: (), d: () };
    ///
    /// ReprPacked::OFFSET_A.swap_mut(&mut this, &mut other);
    /// ReprPacked::OFFSET_B.swap_mut(&mut this, &mut other);
    ///
    /// assert_eq!( moved(this.a), [false, false] );
    /// assert_eq!( moved(this.b), 0xF0u8 );
    ///
    /// assert_eq!( moved(other.a), [true, true] );
    /// assert_eq!( moved(other.b), 0x0Fu8 );
    ///
    /// ```
    ///
    #[inline(always)]
    pub fn swap_mut(self, left: &mut S, right: &mut S) {
        unsafe { impl_fo!(fn swap_mut<S, F, Unaligned>(self, left, right)) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::types_for_tests::StructPacked;

    use core::mem;

    #[test]
    fn test_constructor_offset() {
        unsafe {
            let field_0 = FieldOffset::<(u128,), u8, Aligned>::new(0);
            let field_1 = field_0.next_field_offset::<u32, Aligned>();
            assert_eq!(field_0.offset(), 0);
            assert_eq!(field_1.offset(), mem::align_of::<u32>());
        }
        unsafe {
            let field_0 = FieldOffset::<StructPacked<u128, (), (), ()>, u8, Unaligned>::new(0);
            let field_1 = field_0.next_field_offset::<u32, Unaligned>();
            let field_2 = field_1.next_field_offset::<&'static str, Unaligned>();
            assert_eq!(field_0.offset(), 0);
            assert_eq!(field_1.offset(), 1);
            assert_eq!(field_2.offset(), 5);
        }
    }
}