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
// Copyright 2018-2021 Theodore Cipicchio
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Support traits.

use core::ptr;
use core::slice;
use core::str;

use super::{ArrayIter, CacheAligned};
use core::mem::size_of;
#[cfg(any(stable_maybe_uninit, feature = "unstable"))]
use core::mem::MaybeUninit;

#[cfg(feature = "std")]
use std::ffi::CStr;

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
use super::{Box, Vec};

/// Trait for types that can be safely used as the backing data type for
/// storage of arbitrary data.
///
/// `ByteData` is implemented by default for all basic integer types as well
/// as the [`CacheAligned`] struct provided by this crate.
///
/// # Safety
///
/// This trait is used to help constrain implementations of the [`Buffer`]
/// trait to known types that are considered "safe" to use as the backing
/// storage type of a buffer. To properly implement this trait, the type
/// should have the following characteristics:
///
/// - Allow arbitrary bytes within instances of the type to be left
///   uninitialized without any possible side effects (outside of attempts to
///   explicitly read those bytes). In particular, types should not have
///   [`Drop`] trait implementations that rely on the data to be in any
///   particular state.
/// - Allow arbitrary bytes within instances of the type to be written to with
///   arbitrary values without affecting other bytes.
/// - Allow previously written bytes to be read back regardless of whether
///   other bytes have been written to yet (only bytes that have been
///   explicitly written to are expected to be read back).
///
/// [`Buffer`]: trait.Buffer.html
/// [`CacheAligned`]: struct.CacheAligned.html
/// [`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html
pub unsafe trait ByteData: Sized {}

unsafe impl ByteData for u8 {}
unsafe impl ByteData for u16 {}
unsafe impl ByteData for u32 {}
unsafe impl ByteData for u64 {}
#[cfg(any(stable128, feature = "unstable"))]
unsafe impl ByteData for u128 {}
unsafe impl ByteData for usize {}
unsafe impl ByteData for i8 {}
unsafe impl ByteData for i16 {}
unsafe impl ByteData for i32 {}
unsafe impl ByteData for i64 {}
#[cfg(any(stable128, feature = "unstable"))]
unsafe impl ByteData for i128 {}
unsafe impl ByteData for isize {}
unsafe impl ByteData for CacheAligned {}

#[cfg(any(stable_maybe_uninit, feature = "unstable"))]
unsafe impl<T: ByteData> ByteData for MaybeUninit<T> {}

/// Trait for static arrays.
///
/// # Safety
///
/// This trait is declared as unsafe to signify that it should only be
/// implemented for static arrays (`[T; x]` for some size x). Implementing it
/// for other types can result in undefined behavior and instability.
pub unsafe trait Array {
    /// Array item type.
    type Item: Sized;

    /// Returns a slice of the entire array.
    fn as_slice(&self) -> &[Self::Item];

    /// Returns a mutable slice of the entire array.
    fn as_mut_slice(&mut self) -> &mut [Self::Item];
}

/// Trait for [`Scratchpad`] buffer types.
///
/// `Buffer` objects contain the memory from which [`Scratchpad`] allocations
/// are made. [`Scratchpad`] handles all bookkeeping, so a buffer only needs
/// to provide methods for raw access of the buffer memory.
///
/// [`Scratchpad`]: struct.Scratchpad.html
pub trait Buffer {
    /// Returns a byte slice of the buffer contents.
    fn as_bytes(&self) -> &[u8];
    /// Returns a mutable byte slice of the buffer contents.
    fn as_bytes_mut(&mut self) -> &mut [u8];
}

impl<T> Buffer for T
where
    T: Array,
    <T as Array>::Item: ByteData,
{
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        let data = self.as_slice();
        unsafe {
            slice::from_raw_parts(
                data.as_ptr() as *const u8,
                data.len() * size_of::<<T as Array>::Item>(),
            )
        }
    }

    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        let data = self.as_mut_slice();
        unsafe {
            slice::from_raw_parts_mut(
                data.as_mut_ptr() as *mut u8,
                data.len() * size_of::<<T as Array>::Item>(),
            )
        }
    }
}

impl<'a, T> Buffer for &'a mut [T]
where
    T: ByteData,
{
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        unsafe {
            slice::from_raw_parts(
                self.as_ptr() as *const u8,
                self.len() * size_of::<T>(),
            )
        }
    }

    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        unsafe {
            slice::from_raw_parts_mut(
                self.as_mut_ptr() as *mut u8,
                self.len() * size_of::<T>(),
            )
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T> Buffer for Box<[T]>
where
    T: ByteData,
{
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        let data = self.as_ref();
        unsafe {
            slice::from_raw_parts(
                data.as_ptr() as *const u8,
                data.len() * size_of::<T>(),
            )
        }
    }

    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        let data = self.as_mut();
        unsafe {
            slice::from_raw_parts_mut(
                data.as_mut_ptr() as *mut u8,
                data.len() * size_of::<T>(),
            )
        }
    }
}

/// [`Buffer`] sub-trait for static arrays.
///
/// This trait is used specifically to restrict the implementation of
/// [`Scratchpad::static_new()`] to static array buffers.
///
/// # Safety
///
/// [`Scratchpad::static_new()`] leaves instances of this type **entirely**
/// uninitialized, so implementing it is fundamentally unsafe. It should only
/// be implemented by static arrays of [`ByteData`] types.
///
/// [`Buffer`]: trait.Buffer.html
/// [`ByteData`]: trait.ByteData.html
/// [`Scratchpad::static_new()`]: struct.Scratchpad.html#method.static_new
pub unsafe trait StaticBuffer: Buffer + Array {}

unsafe impl<T> StaticBuffer for T
where
    T: Array,
    <T as Array>::Item: ByteData,
{
}

/// Trait for [`Scratchpad`] allocation tracking containers.
///
/// Each [`Marker`] is tracked within a [`Scratchpad`] using only a single
/// `usize` value per allocation. Actual storage of such values can be
/// implemented in any manner (memory does not need to be contiguous, for
/// instance).
///
/// [`Scratchpad`] and [`Marker`] will never call [`get()`] for a given index
/// if [`set()`] has not been previously called for the same index, so values
/// can be left uninitialized prior to [`set()`] calls.
///
/// [`get()`]: #method.get.html
/// [`Marker`]: trait.Marker.html
/// [`Scratchpad`]: struct.Scratchpad.html
/// [`set()`]: #method.set.html
pub trait Tracking: Sized {
    /// Returns the total number of allocations that can be stored in this
    /// container.
    fn capacity(&self) -> usize;
    /// Stores a value at the specified index.
    fn set(&mut self, index: usize, value: usize);
    /// Retrieves the value from the specified index.
    fn get(&self, index: usize) -> usize;
}

impl<T> Tracking for T
where
    T: Buffer,
{
    #[inline]
    fn capacity(&self) -> usize {
        self.as_bytes().len() / size_of::<usize>()
    }

    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::cast_ptr_alignment, clippy::size_of_in_element_count)
    )]
    #[inline]
    fn set(&mut self, index: usize, value: usize) {
        let bytes = self.as_bytes_mut();
        unsafe {
            let contents = slice::from_raw_parts_mut(
                bytes.as_mut_ptr() as *mut usize,
                bytes.len() / size_of::<usize>(),
            );
            ptr::write_unaligned(&mut contents[index], value);
        }
    }

    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::cast_ptr_alignment, clippy::size_of_in_element_count)
    )]
    #[inline]
    fn get(&self, index: usize) -> usize {
        let bytes = self.as_bytes();
        unsafe {
            let contents = slice::from_raw_parts(
                bytes.as_ptr() as *const usize,
                bytes.len() / size_of::<usize>(),
            );
            ptr::read_unaligned(&contents[index])
        }
    }
}

/// Trait for dynamically sized types that wrap some slice type.
///
/// Some DSTs, such as [`str`], are mostly a wrapper for a basic slice type,
/// often providing some abstraction to ensure the data isn't used in an
/// unsafe manner. Implementing this trait for such DSTs exposes conversions
/// to and from the slice type, allowing us to use these types with allocation
/// operations.
///
/// [`str`]: https://doc.rust-lang.org/std/primitive.str.html
pub trait SliceLike {
    /// Slice element type.
    type Element: Sized;

    /// Returns a slice of `Self::Element` elements containing this slice's
    /// data.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    ///
    /// let message = "foo";
    /// let bytes = message.as_element_slice();
    /// assert_eq!(bytes, &[b'f', b'o', b'o']);
    /// ```
    fn as_element_slice(&self) -> &[Self::Element];

    /// Returns a mutable slice of `Self::Element` elements containing this
    /// slice's data.
    ///
    /// # Safety
    ///
    /// Slices of this type may perform validity checks against the internal
    /// data (e.g. `str` slices must contain valid UTF-8 data). This function
    /// allows for modification of the slice contents outside such checks.
    /// Improper use of this function can result in undefined behavior.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    ///
    /// let mut message = String::from("foo");
    ///
    /// unsafe {
    ///     let bytes = message.as_mut_str().as_element_slice_mut();
    ///     bytes[0] = b'b';
    ///     bytes[1] = b'a';
    ///     bytes[2] = b'r';
    /// }
    ///
    /// assert_eq!(message, "bar");
    /// ```
    unsafe fn as_element_slice_mut(&mut self) -> &mut [Self::Element];

    /// Reinterprets a slice of `Self::Inner` elements as a slice of this
    /// type.
    ///
    /// # Safety
    ///
    /// Slices of this type may perform validity checks against the internal
    /// data (e.g. `str` slices must contain valid UTF-8 data). This function
    /// bypasses any such checks, potentially returning data that is invalid.
    /// Improper use of this function can result in undefined behavior.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    ///
    /// let bytes = [b'f', b'o', b'o'];
    /// let message = unsafe {
    ///     <str as SliceLike>::from_element_slice(&bytes[..])
    /// };
    /// assert_eq!(message, "foo");
    /// ```
    unsafe fn from_element_slice(slice: &[Self::Element]) -> &Self;

    /// Reinterprets a mutable slice of `Self::Inner` elements as a mutable
    /// slice of this type.
    ///
    /// # Safety
    ///
    /// Slices of this type may perform validity checks against the internal
    /// data (e.g. `str` slices must contain valid UTF-8 data). This function
    /// bypasses any such checks, potentially returning data that is invalid.
    /// Improper use of this function can result in undefined behavior.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    ///
    /// let mut bytes = [b'f', b'o', b'o'];
    ///
    /// unsafe {
    ///     let message = <str as SliceLike>::from_element_slice_mut(
    ///         &mut bytes[..],
    ///     );
    ///     message.as_bytes_mut()[0] = b'b';
    ///     message.as_bytes_mut()[1] = b'a';
    ///     message.as_bytes_mut()[2] = b'r';
    /// }
    ///
    /// assert_eq!(bytes, [b'b', b'a', b'r']);
    /// ```
    unsafe fn from_element_slice_mut(
        slice: &mut [Self::Element],
    ) -> &mut Self;
}

impl<T> SliceLike for [T] {
    type Element = T;

    #[inline]
    fn as_element_slice(&self) -> &[Self::Element] {
        self
    }

    #[inline]
    unsafe fn as_element_slice_mut(&mut self) -> &mut [Self::Element] {
        self
    }

    #[inline]
    unsafe fn from_element_slice(slice: &[Self::Element]) -> &Self {
        slice
    }

    #[inline]
    unsafe fn from_element_slice_mut(
        slice: &mut [Self::Element],
    ) -> &mut Self {
        slice
    }
}

impl SliceLike for str {
    type Element = u8;

    #[inline]
    fn as_element_slice(&self) -> &[Self::Element] {
        self.as_bytes()
    }

    #[inline]
    unsafe fn as_element_slice_mut(&mut self) -> &mut [Self::Element] {
        self.as_bytes_mut()
    }

    #[inline]
    unsafe fn from_element_slice(slice: &[Self::Element]) -> &Self {
        str::from_utf8_unchecked(slice)
    }

    #[inline]
    unsafe fn from_element_slice_mut(
        slice: &mut [Self::Element],
    ) -> &mut Self {
        str::from_utf8_unchecked_mut(slice)
    }
}

#[cfg(feature = "std")]
impl SliceLike for CStr {
    /// Slice element type.
    ///
    /// `u8` is used as the element type instead of [`c_char`], as the
    /// [`CStr`] methods that handle conversions to and from slices work with
    /// `u8` slices (`c_char` is only used when working with raw pointers).
    ///
    /// [`c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html
    /// [`CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
    type Element = u8;

    /// Returns a slice of `Self::Element` elements containing this slice's
    /// data.
    ///
    /// This uses [`CStr::to_bytes_with_nul()`] to return the entire [`CStr`]
    /// contents, including the nul terminator.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    /// use std::ffi::CString;
    ///
    /// let message = CString::new("foo").unwrap();
    /// let message_c_str = message.as_c_str();
    /// let bytes = message_c_str.as_element_slice();
    /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
    /// ```
    ///
    /// [`CStr::to_bytes_with_nul()`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.to_bytes_with_nul
    /// [`CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
    #[inline]
    fn as_element_slice(&self) -> &[Self::Element] {
        self.to_bytes_with_nul()
    }

    /// Returns a mutable slice of `Self::Element` elements containing this
    /// slice's data.
    ///
    /// This is roughly equivalent to [`CStr::to_bytes_with_nul()`], returning
    /// a mutable slice instead of an immutable slice.
    ///
    /// # Safety
    ///
    /// This function potentially allows for modification of the [`CStr`]
    /// contents outside of any validity checks, specifically checks for a nul
    /// terminator and no internal nul bytes. Improper use of this function
    /// can result in undefined behavior.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    /// use std::ffi::{CStr, CString};
    ///
    /// let mut message = CString::new("foo").unwrap().into_boxed_c_str();
    /// let message_c_str = &mut *message;
    ///
    /// unsafe {
    ///     let bytes = message_c_str.as_element_slice_mut();
    ///     bytes[0] = b'b';
    ///     bytes[1] = b'a';
    ///     bytes[2] = b'r';
    /// }
    ///
    /// assert_eq!(
    ///     message_c_str,
    ///     CStr::from_bytes_with_nul(b"bar\0").unwrap(),
    /// );
    /// ```
    ///
    /// [`CStr::to_bytes_with_nul()`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.to_bytes_with_nul
    /// [`CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
    #[inline]
    unsafe fn as_element_slice_mut(&mut self) -> &mut [Self::Element] {
        // TODO: Converting from a constant reference to a mutable reference
        //       is technically undefined behavior, but aliasing of the
        //       constant reference is prevented by the fact that we take a
        //       mutable reference to the `CStr` slice as an argument.
        //       Regardless, a better alternative should be found if possible.
        // LINT: Allowing mutable cast for the time being (see above TODO).
        #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ref_to_mut))]
        &mut *(self.to_bytes_with_nul() as *const [u8] as *mut [u8])
    }

    /// Reinterprets a slice of `Self::Inner` elements as a slice of this
    /// type.
    ///
    /// This uses [`CStr::from_bytes_with_nul_unchecked()`] to create a
    /// [`CStr`] from a nul-terminated byte slice.
    ///
    /// # Safety
    ///
    /// No safety checking is performed on the provided byte slice. It
    /// **must** be nul-terminated and not contain any interior nul bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    /// use std::ffi::CStr;
    ///
    /// let bytes = [b'f', b'o', b'o', b'\0'];
    /// let message = unsafe {
    ///     <CStr as SliceLike>::from_element_slice(&bytes[..])
    /// };
    /// assert_eq!(message, CStr::from_bytes_with_nul(b"foo\0").unwrap());
    /// ```
    ///
    /// [`CStr::from_bytes_with_nul_unchecked()`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.from_bytes_with_nul_unchecked
    /// [`CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
    #[inline]
    unsafe fn from_element_slice(slice: &[Self::Element]) -> &Self {
        CStr::from_bytes_with_nul_unchecked(slice)
    }

    /// Reinterprets a mutable slice of `Self::Inner` elements as a mutable
    /// slice of this type.
    ///
    /// This is roughly equivalent to
    /// [`CStr::from_bytes_with_nul_unchecked()`], returning a mutable
    /// [`CStr`] reference instead of an immutable reference.
    ///
    /// # Safety
    ///
    /// No safety checking is performed on the provided byte slice. It
    /// **must** be nul-terminated and not contain any interior nul bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceLike;
    /// use std::ffi::CStr;
    ///
    /// let mut bytes = [b'f', b'o', b'o', b'\0'];
    /// let message = unsafe {
    ///     <CStr as SliceLike>::from_element_slice_mut(&mut bytes[..])
    /// };
    /// assert_eq!(message, CStr::from_bytes_with_nul(b"foo\0").unwrap());
    /// ```
    ///
    /// [`CStr::from_bytes_with_nul_unchecked()`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.from_bytes_with_nul_unchecked
    /// [`CStr`]: https://doc.rust-lang.org/std/ffi/struct.CStr.html
    #[inline]
    unsafe fn from_element_slice_mut(
        slice: &mut [Self::Element],
    ) -> &mut Self {
        // TODO: This makes the same assumption as `as_element_slice_mut()`
        //       above with regards to casting from a constant reference to a
        //       mutable reference. Seeing as it still falls under the blanket
        //       of undefined behavior, an alternative should be used if
        //       possible.
        // LINT: Allowing mutable cast for the time being (see above TODO).
        #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ref_to_mut))]
        &mut *(CStr::from_bytes_with_nul_unchecked(slice) as *const CStr
            as *mut CStr)
    }
}

/// Extension of the [`SliceLike`] trait used to mark DSTs for which
/// concatenation can safely be performed by concatenating the underlying
/// slice type.
///
/// This is used as a constraint for functions such as
/// [`Allocation::concat()`] and [`MarkerFront::append()`].
///
/// [`Allocation::concat()`]: struct.Allocation.html#method.concat
/// [`MarkerFront::append()`]: struct.MarkerFront.html#method.append
/// [`SliceLike`]: trait.SliceLike.html
pub trait ConcatenateSlice: SliceLike {}

impl<T> ConcatenateSlice for [T] {}
impl ConcatenateSlice for str {}

/// Trait for reinterpreting a pointer to a given type as a compatible
/// [`SliceLike`] pointer that uses the exact same backing memory.
///
/// # Safety
///
/// This trait is marked as unsafe due to the potential for data loss if
/// implemented incorrectly. It is used within this crate to determine which
/// slice conversions are valid for a given [`Allocation`] without requiring
/// the allocated data to be modified in any way. The source and destination
/// types must use the same exact storage memory, and dropping the data stored
/// in the destination type must also perform any cleanup required by the
/// original source type.
///
/// [`Allocation`]: struct.Allocation.html
/// [`SliceLike`]: trait.SliceLike.html
pub unsafe trait IntoMutSliceLikePtr<T>
where
    T: SliceLike + ?Sized,
{
    /// Reinterprets a mutable pointer of this type as a [`SliceLike`]
    /// pointer.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::IntoMutSliceLikePtr;
    ///
    /// let mut value = 3.14159;
    /// let value_ptr: *mut f64 = &mut value;
    ///
    /// let slice_ptr = IntoMutSliceLikePtr::into_mut_slice_like_ptr(
    ///     value_ptr,
    /// );
    /// assert_eq!(unsafe { &*slice_ptr }, [3.14159]);
    /// ```
    ///
    /// [`SliceLike`]: trait.SliceLike.html
    // LINT: `wrong_self_convention` lint wasn't present when this method was
    //       added. Altering it to satisfy the lint warning would be a
    //       SemVer-breaking change, so it will have to be deferred to a later
    //       major version.
    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::wrong_self_convention)
    )]
    fn into_mut_slice_like_ptr(ptr: *mut Self) -> *mut T;
}

unsafe impl<T> IntoMutSliceLikePtr<[T]> for T {
    // LINT: `ptr` doesn't get dereferenced by `slice::from_raw_parts_mut()`,
    //       so the lint error can be ignored.
    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::not_unsafe_ptr_arg_deref)
    )]
    #[inline]
    fn into_mut_slice_like_ptr(ptr: *mut T) -> *mut [T] {
        unsafe { slice::from_raw_parts_mut(ptr, 1) }
    }
}

unsafe impl<T> IntoMutSliceLikePtr<T> for T
where
    T: SliceLike + ?Sized,
{
    #[inline]
    fn into_mut_slice_like_ptr(ptr: *mut T) -> *mut T {
        ptr
    }
}

unsafe impl IntoMutSliceLikePtr<[u8]> for str {
    // LINT: Despite the notation, `ptr` doesn't actually get dereferenced by
    //       this function (only the pointer value itself is ever read), so
    //       the lint error can be ignored.
    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::not_unsafe_ptr_arg_deref)
    )]
    #[inline]
    fn into_mut_slice_like_ptr(ptr: *mut str) -> *mut [u8] {
        unsafe { (*ptr).as_bytes_mut() }
    }
}

#[cfg(feature = "std")]
unsafe impl IntoMutSliceLikePtr<[u8]> for CStr {
    // TODO: While the current implementation of `CStr` doesn't require
    //       dereferencing `ptr` in order to convert to a `*mut [u8]`, it may
    //       in the future, so the `not_unsafe_ptr_arg_deref` lint warning is
    //       valid in this context. Since `IntoMutSliceLikePtr` is a public
    //       trait, adding `unsafe` to this function would be a breaking
    //       change, so we can't do so until the 2.0 release.
    #[cfg_attr(
        feature = "cargo-clippy",
        allow(clippy::not_unsafe_ptr_arg_deref)
    )]
    #[inline]
    fn into_mut_slice_like_ptr(ptr: *mut CStr) -> *mut [u8] {
        unsafe { (*ptr).as_element_slice_mut() }
    }
}

/// Trait for sources of slice data provided to [`Marker`] trait methods.
///
/// `SliceSource` is implemented for static arrays and slice references. If
/// either the `std` or `unstable` features are enabled, [boxed slice] and
/// [`Vec`] instances can be used as slice sources as well.
///
/// `SliceSource` on its own is only usable for `Clone` and `Copy` data
/// sources. For move operations, the [`SliceMoveSource`] subtrait provides
/// additional functionality for moving slices out of supported types.
///
/// [`Marker`]: trait.Marker.html
/// [`SliceMoveSource`]: trait.SliceMoveSource.html
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
/// [boxed slice]: https://doc.rust-lang.org/std/boxed/struct.Box.html
pub trait SliceSource<T>
where
    T: SliceLike + ?Sized,
{
    /// Returns a [`SliceLike`] reference to this object's data.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceSource;
    ///
    /// // `value` is an `[f64; 1]`...
    /// let value = [3.14159];
    ///
    /// // ...but `value_slice` is a `&[f64]`...
    /// let value_slice = value.as_slice_like();
    /// assert_eq!(value_slice.len(), 1);
    /// assert_eq!(value_slice[0], 3.14159);
    ///
    /// // ...that references the same memory as `value`.
    /// assert!(std::ptr::eq(&value[0], &value_slice[0]));
    /// ```
    ///
    /// [`SliceLike`]: trait.SliceLike.html
    fn as_slice_like(&self) -> &T;

    /// Returns a [`SliceLike`] pointer to this object's data.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceSource;
    ///
    /// // `value` is an `[f64; 1]`...
    /// let value = [3.14159];
    ///
    /// unsafe {
    ///     // ...but `value_slice_ptr` is a `*const [f64]`...
    ///     let value_slice_ptr = value.as_slice_like_ptr();
    ///     assert_eq!((*value_slice_ptr).len(), 1);
    ///     assert_eq!((*value_slice_ptr)[0], 3.14159);
    ///
    ///     // ...that references the same memory as `value`.
    ///     assert!(std::ptr::eq(&value[0], &(*value_slice_ptr)[0]));
    /// }
    /// ```
    ///
    /// [`SliceLike`]: trait.SliceLike.html
    #[inline(always)]
    fn as_slice_like_ptr(&self) -> *const T {
        self.as_slice_like()
    }
}

impl<T> SliceSource<[<T as Array>::Item]> for T
where
    T: Array,
{
    #[inline]
    fn as_slice_like(&self) -> &[<T as Array>::Item] {
        self.as_slice()
    }
}

impl<'a, T> SliceSource<T> for &'a T
where
    T: SliceLike + ?Sized,
{
    #[inline]
    fn as_slice_like(&self) -> &T {
        *self
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T> SliceSource<T> for Box<T>
where
    T: SliceLike + ?Sized,
{
    #[inline]
    fn as_slice_like(&self) -> &T {
        &**self
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T> SliceSource<[T]> for Vec<T> {
    #[inline]
    fn as_slice_like(&self) -> &[T] {
        self.as_slice()
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'a, T> SliceSource<T> for &'a Box<T>
where
    T: SliceLike + ?Sized,
{
    #[inline]
    fn as_slice_like(&self) -> &T {
        &***self
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'a, T> SliceSource<[T]> for &'a Vec<T> {
    #[inline]
    fn as_slice_like(&self) -> &[T] {
        self.as_slice()
    }
}

/// Subtrait of [`SliceSource`] for taking ownership of the contents of a
/// slice.
///
/// [`SliceSource`]: trait.SliceSource.html
pub trait SliceMoveSource<T>: SliceSource<T>
where
    T: SliceLike + ?Sized,
{
    /// Calls a closure for each item in this source, consuming the source.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceMoveSource;
    ///
    /// fn move_to_vec<T: SliceMoveSource<[i32]>>(source: T) -> Vec<i32> {
    ///     let mut out = Vec::new();
    ///     source.move_elements(|x| out.push(x));
    ///     out
    /// }
    ///
    /// let values = [5, 6, 7, 8];
    /// let out = move_to_vec([5, 6, 7, 8]);
    /// assert_eq!(*out, [5, 6, 7, 8]);
    /// ```
    fn move_elements<F>(self, f: F)
    where
        Self: Sized,
        F: FnMut(<T as SliceLike>::Element);
}

impl<T> SliceMoveSource<[<T as Array>::Item]> for T
where
    T: Array,
{
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as Array>::Item),
    {
        for item in ArrayIter::new(self) {
            f(item);
        }
    }
}

impl<'a, T> SliceMoveSource<T> for &'a T
where
    T: SliceLike + ?Sized,
    <T as SliceLike>::Element: Copy,
{
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for item in self.as_element_slice() {
            f(*item);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T> SliceMoveSource<T> for Box<T>
where
    T: SliceLike + ?Sized,
{
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        let boxed_slice = unsafe {
            Box::from_raw((*Box::into_raw(self)).as_element_slice_mut()
                as *mut [T::Element])
        };

        for item in boxed_slice.into_vec() {
            f(item);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T> SliceMoveSource<[T]> for Vec<T> {
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(T),
    {
        for item in self {
            f(item);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'a, T> SliceMoveSource<T> for &'a Box<T>
where
    T: SliceLike + ?Sized,
    <T as SliceLike>::Element: Copy,
{
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for item in self.as_element_slice() {
            f(*item);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'a, T> SliceMoveSource<[T]> for &'a Vec<T>
where
    T: Copy,
{
    fn move_elements<F>(self, mut f: F)
    where
        F: FnMut(T),
    {
        for item in self {
            f(*item);
        }
    }
}

/// Trait for generic access to collections of [`SliceSource`] objects.
///
/// Collections can be either arrays, tuples, or slices of [`SliceSource`]
/// objects. Tuples can contain contain objects of different [`SliceSource`]
/// implementation types. If either the `std` or `unstable` features are
/// enabled, [`boxed slice`] and [`Vec`] instances can be used as slice
/// source collections as well.
///
/// `SliceSourceCollection` on its own is only usable for `Clone` and `Copy`
/// data sources. For move operations, the [`SliceMoveSourceCollection`]
/// subtrait provides additional functionality for moving slices out of
/// supported types.
///
/// [`boxed slice`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
/// [`Marker`]: trait.Marker.html
/// [`SliceMoveSourceCollection`]: trait.SliceMoveSourceCollection.html
/// [`SliceSource`]: trait.SliceSource.html
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
pub trait SliceSourceCollection<T>
where
    T: SliceLike + ?Sized,
{
    /// Calls a closure for each [`SliceSource`] in this collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::{SliceLike, SliceSourceCollection};
    ///
    /// let collection = ([1, 2, 3], [4], [5, 6]);
    ///
    /// let mut out = Vec::new();
    /// collection.for_each(|source| {
    ///     for x in source.as_slice_like().as_element_slice() {
    ///         out.push(*x * 2);
    ///     }
    /// });
    /// assert_eq!(*out, [2, 4, 6, 8, 10, 12]);
    /// ```
    ///
    /// [`SliceSource`]: trait.SliceSource.html
    fn for_each<F>(&self, f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>);
}

impl<T, U> SliceSourceCollection<T> for U
where
    T: SliceLike + ?Sized,
    U: Array,
    <U as Array>::Item: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in self.as_slice() {
            f(source);
        }
    }
}

impl<'b, T, U> SliceSourceCollection<T> for &'b [U]
where
    T: SliceLike + ?Sized,
    U: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in *self {
            f(source);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T, U> SliceSourceCollection<T> for Box<[U]>
where
    T: SliceLike + ?Sized,
    U: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in &**self {
            f(source);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T, U> SliceSourceCollection<T> for Vec<U>
where
    T: SliceLike + ?Sized,
    U: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in self {
            f(source);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'b, T, U> SliceSourceCollection<T> for &'b Box<[U]>
where
    T: SliceLike + ?Sized,
    U: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in &***self {
            f(source);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'b, T, U> SliceSourceCollection<T> for &'b Vec<U>
where
    T: SliceLike + ?Sized,
    U: SliceSource<T>,
{
    fn for_each<F>(&self, mut f: F)
    where
        F: for<'a> FnMut(&'a SliceSource<T>),
    {
        for source in *self {
            f(source);
        }
    }
}

/// Subtrait of [`SliceSourceCollection`] for taking ownership of the contents
/// of a collection of slice sources.
///
/// [`SliceSourceCollection`]: trait.SliceSourceCollection.html
pub trait SliceMoveSourceCollection<T>: SliceSourceCollection<T>
where
    T: SliceLike + ?Sized,
{
    /// Calls a closure for each item in all [`SliceSource`] objects of this
    /// collection in sequence, consuming the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use scratchpad::SliceMoveSourceCollection;
    ///
    /// let collection = ([1, 2, 3], [4], [5, 6]);
    ///
    /// let mut out = Vec::new();
    /// collection.move_all_elements(|x| out.push(x * 2));
    /// assert_eq!(*out, [2, 4, 6, 8, 10, 12]);
    /// ```
    ///
    /// [`SliceSource`]: trait.SliceSource.html
    fn move_all_elements<F>(self, f: F)
    where
        Self: Sized,
        F: FnMut(<T as SliceLike>::Element);
}

impl<T, U> SliceMoveSourceCollection<T> for U
where
    T: SliceLike + ?Sized,
    U: Array,
    <U as Array>::Item: SliceMoveSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in ArrayIter::new(self) {
            source.move_elements(&mut f);
        }
    }
}

impl<'b, T, U> SliceMoveSourceCollection<T> for &'b [U]
where
    T: SliceLike + ?Sized,
    <T as SliceLike>::Element: Copy,
    U: SliceSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in self {
            for item in source.as_slice_like().as_element_slice() {
                f(*item);
            }
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T, U> SliceMoveSourceCollection<T> for Box<[U]>
where
    T: SliceLike + ?Sized,
    U: SliceMoveSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in self.into_vec() {
            source.move_elements(&mut f);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<T, U> SliceMoveSourceCollection<T> for Vec<U>
where
    T: SliceLike + ?Sized,
    U: SliceMoveSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in self {
            source.move_elements(&mut f);
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'b, T, U> SliceMoveSourceCollection<T> for &'b Box<[U]>
where
    T: SliceLike + ?Sized,
    <T as SliceLike>::Element: Copy,
    U: SliceSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in &**self {
            for item in source.as_slice_like().as_element_slice() {
                f(*item);
            }
        }
    }
}

#[cfg(any(feature = "std", feature = "alloc", feature = "unstable"))]
impl<'b, T, U> SliceMoveSourceCollection<T> for &'b Vec<U>
where
    T: SliceLike + ?Sized,
    <T as SliceLike>::Element: Copy,
    U: SliceSource<T>,
{
    fn move_all_elements<F>(self, mut f: F)
    where
        F: FnMut(<T as SliceLike>::Element),
    {
        for source in self {
            for item in source.as_slice_like().as_element_slice() {
                f(*item);
            }
        }
    }
}

/// Macro for generating trait implementations for tuples.
macro_rules! generate_tuple_trait_impls {
    ($($name:ident,)+) => {
        impl<T, $($name,)*> SliceSourceCollection<T> for ($($name,)*)
        where
            T: SliceLike + ?Sized,
            $($name: SliceSource<T>,)*
        {
            #[allow(non_snake_case)]
            fn for_each<F>(&self, mut f: F)
            where
                F: for<'a> FnMut(&'a SliceSource<T>)
            {
                let ($(ref $name,)*) = *self;
                $(
                    f($name);
                )*
            }
        }

        #[allow(non_snake_case)]
        impl<'b, T, $($name,)*> SliceSourceCollection<T> for &'b ($($name,)*)
        where
            T: SliceLike + ?Sized,
            $($name: SliceSource<T>,)*
        {
            #[allow(non_snake_case)]
            fn for_each<F>(&self, mut f: F)
            where
                F: for<'a> FnMut(&'a SliceSource<T>)
            {
                let ($(ref $name,)*) = **self;
                $(
                    f($name);
                )*
            }
        }

        impl<T, $($name,)*> SliceMoveSourceCollection<T> for ($($name,)*)
        where
            T: SliceLike + ?Sized,
            $($name: SliceMoveSource<T>,)*
        {
            #[allow(non_snake_case)]
            fn move_all_elements<F>(self, mut f: F)
            where
                F: FnMut(<T as SliceLike>::Element),
            {
                let ($($name,)*) = self;
                $(
                    $name.move_elements(&mut f);
                )*
            }
        }

        #[allow(non_snake_case)]
        impl<'b, T, $($name,)*> SliceMoveSourceCollection<T>
            for &'b ($($name,)*)
        where
            T: SliceLike + ?Sized,
            <T as SliceLike>::Element: Copy,
            $($name: SliceSource<T>,)*
        {
            #[allow(non_snake_case)]
            fn move_all_elements<F>(self, mut f: F)
            where
                F: FnMut(<T as SliceLike>::Element),
            {
                let ($(ref $name,)*) = *self;
                $(
                    for item in $name.as_slice_like().as_element_slice() {
                        f(*item);
                    }
                )*
            }
        }

        generate_tuple_trait_impls!([REDUCE] $($name,)*);
    };

    () => {};

    ([REDUCE] $name:ident, $($remaining:ident,)*) => {
        generate_tuple_trait_impls!($($remaining,)*);
    }
}

generate_tuple_trait_impls!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,);

/// Macro for generating trait implementations for static arrays.
macro_rules! generate_array_trait_impls {
    ($size:expr) => {
        unsafe impl<T> Array for [T; $size]
        where
            T: Sized,
        {
            type Item = T;

            #[inline]
            fn as_slice(&self) -> &[T] {
                &self[..]
            }

            #[inline]
            fn as_mut_slice(&mut self) -> &mut [T] {
                &mut self[..]
            }
        }

        unsafe impl<T> IntoMutSliceLikePtr<[T]> for [T; $size] {
            // LINT: Despite the notation, `ptr` doesn't actually get
            //       dereferenced by this function (only the pointer value
            //       itself is ever read), so the lint error can be ignored.
            #[cfg_attr(feature = "cargo-clippy", allow(clippy::not_unsafe_ptr_arg_deref))]
            #[inline]
            fn into_mut_slice_like_ptr(ptr: *mut [T; $size]) -> *mut [T] {
                unsafe { &mut (*ptr)[..] }
            }
        }
    };

    ($size:expr, $($other:tt)*) => {
        generate_array_trait_impls!($size);
        generate_array_trait_impls!($($other)*);
    };

    () => {};
}

generate_array_trait_impls!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
generate_array_trait_impls!(11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
generate_array_trait_impls!(21, 22, 23, 24, 25, 26, 27, 28, 29, 30);
generate_array_trait_impls!(31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
generate_array_trait_impls!(41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
generate_array_trait_impls!(51, 52, 53, 54, 55, 56, 57, 58, 59, 60);
generate_array_trait_impls!(61, 62, 63, 64);
generate_array_trait_impls!(0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000);
generate_array_trait_impls!(
    0x4000, 0x8000, 0x10000, 0x20000, 0x40000, 0x80000
);
generate_array_trait_impls!(
    0x10_0000, 0x20_0000, 0x40_0000, 0x80_0000, 0x100_0000
);
generate_array_trait_impls!(0x200_0000, 0x400_0000, 0x800_0000, 0x1000_0000);
generate_array_trait_impls!(0x2000_0000, 0x4000_0000);