sentinel 0.5.4

A sentinel-terminated slice library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! `sentinel` is a sentinel-terminated slice library.

#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "nightly", feature(extern_types))]
#![cfg_attr(feature = "nightly", feature(allocator_api))]
#![cfg_attr(feature = "nightly", feature(ptr_metadata))]
#![cfg_attr(feature = "nightly", feature(concat_bytes))]
#![forbid(unsafe_op_in_unsafe_fn)]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::cmp::Ordering;
use core::fmt;
use core::fmt::Write;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::panic::{RefUnwindSafe, UnwindSafe};

mod sentinel;
pub use self::sentinel::*;

mod iter;
pub use self::iter::*;

#[cfg(any(feature = "nightly", feature = "alloc"))]
mod sbox;
#[cfg(any(feature = "nightly", feature = "alloc"))]
pub use self::sbox::*;

mod index;

mod inline;
pub use self::inline::*;

/// A type that wraps a "C-like" string.
///
/// When you hold a reference to a `CStr`, you are guarenteed that it is null-terminated. This type
/// knows this and allows safe manipulation of those bytes.
pub type CStr = SSlice<u8>;

#[cfg(feature = "nightly")]
extern "C" {
    type SliceContent;
}

/// A sentinel-terminated slice.
#[repr(transparent)]
pub struct SSlice<T>
where
    T: Sentinel,
{
    /// Educate the drop-checker about the values owned by a value of this type.
    _content: PhantomData<[T]>,
    /// Makes that `SSlice<T>` is `!Sized` and cannot be created on the stack.
    #[cfg(feature = "nightly")]
    _size: SliceContent,
}

impl<T: Sentinel> SSlice<T> {
    /// Creates a new [`SSlice<T>`] instance from the provided pointer.
    ///
    /// # Safety
    ///
    /// The elements referenced by the provided pointer, until the first sentinel value, must be
    /// part of the same allocated object. They must be properly initialized and safe for reads.
    ///
    /// This invariant must remain until the end of the lifetime `'a` (at least).
    #[inline(always)]
    pub const unsafe fn from_ptr<'a>(ptr: *const T) -> &'a Self {
        // SAFETY:
        //  The caller must ensure that the invariants of `SSlice` are upheld.
        unsafe { &*(ptr as *const Self) }
    }

    /// Creates a new [`SSlice<T>`] instance from the provided pointer.
    ///
    /// # Safety
    ///
    /// The elements referenced by the provided pointer, until the first sentinel value, must be
    /// part of the same allocated object. They must be properly initialized and safe for reads
    /// and writes.
    ///
    /// This invariant must remain until the end of the lifetime `'a` (at least).
    #[inline(always)]
    pub unsafe fn from_mut_ptr<'a>(ptr: *mut T) -> &'a mut Self {
        // SAFETY:
        //  The caller must ensure that the invariants of `SSlice` are upheld.
        unsafe { &mut *(ptr as *mut Self) }
    }

    /// Creates a [`SSlice<T>`] instance from the provided slice.
    ///
    /// If the slice contains a sentinel character, the function retuns a [`SSlice<T>`]
    /// referencing the elements stored in `T` up to (and including) the first sentinel
    /// character. The remaining of the slice is returned as well.
    ///
    /// Otherwise, the function returns [`None`]
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let input = b"abc\0def";
    /// let (a, b) = SSlice::<u8>::from_slice_split(input).unwrap();
    /// assert_eq!(a, b"abc");
    /// assert_eq!(b, b"def");
    /// ```
    #[inline]
    pub fn from_slice_split(slice: &[T]) -> Option<(&Self, &[T])> {
        let idx = T::find_sentinel(slice)?;

        Some(unsafe {
            (
                Self::from_ptr(slice.as_ptr()),
                core::slice::from_raw_parts(
                    slice.as_ptr().add(idx).add(1),
                    slice.len().wrapping_sub(idx).wrapping_sub(1),
                ),
            )
        })
    }

    /// Creates a [`SSlice<T>`] instance from the provided slice.
    ///
    /// If the slice contains a sentinel character, the function returns [`SSlice<T>`]
    /// referencing the elements stored in `T` up to (and including) the first sentinel
    /// character. Otherwise, the function returns [`None`].
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let sslice = SSlice::<u8>::from_slice(b"abc\0def").unwrap();
    /// assert_eq!(sslice, b"abc");
    /// ```
    #[inline]
    pub fn from_slice(slice: &[T]) -> Option<&Self> {
        if T::find_sentinel(slice).is_some() {
            Some(unsafe { Self::from_ptr(slice.as_ptr()) })
        } else {
            None
        }
    }

    /// Creates a [`SSlice<T>`] instance from the provided slice.
    ///
    /// If the slice contains a sentinel character, the function retuns a [`SSlice<T>`]
    /// referencing the elements stored in `T` up to (and including) the first sentinel
    /// character. The remaining of the slice is returned as well.
    ///
    /// Otherwise, the function returns [`None`]
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let mut slice = [1, 2, 3, 0, 4, 5, 6];
    /// let (sslice, remainder) = SSlice::<u8>::from_slice_split_mut(&mut slice).unwrap();
    ///
    /// assert_eq!(sslice, &[1, 2, 3]);
    /// assert_eq!(remainder, [4, 5, 6]);
    /// ```
    #[inline]
    pub fn from_slice_split_mut(slice: &mut [T]) -> Option<(&mut Self, &mut [T])> {
        let idx = T::find_sentinel(slice)?;

        Some(unsafe {
            (
                Self::from_mut_ptr(slice.as_mut_ptr()),
                core::slice::from_raw_parts_mut(
                    slice.as_mut_ptr().add(idx).add(1),
                    slice.len().wrapping_sub(idx).wrapping_sub(1),
                ),
            )
        })
    }

    /// Creates a [`SSlice<T>`] instance from the provided slice.
    ///
    /// If the slice contains a sentinel character, the function returns [`SSlice<T>`]
    /// referencing the elements stored in `T` up to (and including) the first sentinel
    /// character. Otherwise, the function returns [`None`].
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let mut slice = [1, 2, 3, 0, 4, 5, 6];
    /// let sslice = SSlice::<u8>::from_slice_mut(&mut slice).unwrap();
    ///
    /// assert_eq!(sslice, &[1, 2, 3]);
    /// ```
    #[inline]
    pub fn from_slice_mut(slice: &mut [T]) -> Option<&mut Self> {
        if T::find_sentinel(slice).is_some() {
            Some(unsafe { Self::from_mut_ptr(slice.as_mut_ptr()) })
        } else {
            None
        }
    }

    /// Returns a pointer to the first element that is part of the slice.
    ///
    /// # Notes
    ///
    /// This pointer is always valid and *will* reference an initialized instance of `T`. Note,
    /// however, that this value cannot be modified (even if it supports interior mutability). Or,
    /// rather, if it is a sentinel, it must remain a sentinel.
    #[inline(always)]
    pub const fn as_ptr(&self) -> *const T {
        self as *const Self as *const T
    }

    /// Returns a pointer to the first element that is part of the slice.
    ///
    /// # Notes
    ///
    /// This pointer is always valid and *will* reference an initialized instance of `T`. Note,
    /// however, that this value cannot be modified. Or, rather, if it is a sentinel, it must
    /// remain a sentinel.
    #[inline(always)]
    pub fn as_mut_ptr(&mut self) -> *mut T {
        self as *mut Self as *mut T
    }

    /// Returns an iterator over the elements of the slice.
    ///
    /// # Examples
    ///
    /// ```
    /// let s = sentinel::cstr!("Hello!");
    /// let mut iter = s.iter();
    ///
    /// assert_eq!(iter.next(), Some(&b'H'));
    /// assert_eq!(iter.next(), Some(&b'e'));
    /// assert_eq!(iter.next(), Some(&b'l'));
    /// assert_eq!(iter.next(), Some(&b'l'));
    /// assert_eq!(iter.next(), Some(&b'o'));
    /// assert_eq!(iter.next(), Some(&b'!'));
    /// assert_eq!(iter.next(), None);
    /// ```
    #[inline(always)]
    pub fn iter(&self) -> &Iter<T> {
        Iter::new_ref(self)
    }

    /// Returns an iterator over the elements of the slice.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut array = *b"abc\0";
    /// let mut sslice = sentinel::SSlice::<u8>::from_slice_mut(&mut array).unwrap();
    /// let mut iter = sslice.iter_mut();
    ///
    /// *iter.next().unwrap() = b'1';
    /// *iter.next().unwrap() = b'2';
    /// *iter.next().unwrap() = b'3';
    ///
    /// assert_eq!(sslice, b"123");
    /// ```
    #[inline(always)]
    pub fn iter_mut(&mut self) -> &mut Iter<T> {
        Iter::new_mut(self)
    }

    /// Indexes into this [`SSlice<T>`] instance without checking the bounds.
    ///
    /// # Safety
    ///
    /// `index` must be in bounds.
    #[inline(always)]
    pub unsafe fn get_unchecked<Idx>(&self, index: Idx) -> &Idx::Output
    where
        Idx: self::index::SliceIndex<T>,
    {
        unsafe { index.index_unchecked(self) }
    }

    /// Indexes into this [`SSlice<T>`] instance without checking the bounds.
    ///
    /// # Safety
    ///
    /// `index` must be in bounds.
    #[inline(always)]
    pub unsafe fn get_unchecked_mut<Idx>(&mut self, index: Idx) -> &mut Idx::Output
    where
        Idx: self::index::SliceIndex<T>,
    {
        unsafe { index.index_unchecked_mut(self) }
    }

    /// Returns the length of the [`SSlice<T>`]. This is the number of elements referenced by
    /// that instance, not including the terminating sentinel character.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let sslice = SSlice::<u8>::from_slice(b"Hello\0World").unwrap();
    /// assert_eq!(sslice.len(), 5);
    /// ```
    #[inline(always)]
    pub fn len(&self) -> usize {
        // SAFETY:
        //  This is safe by invariant of `SSlice<T>`.
        unsafe { T::find_sentinel_infinite(self.as_ptr()) }
    }

    /// Returns whether the slice is currently empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// assert!(!SSlice::<u8>::from_slice(b"123\0").unwrap().is_empty());
    /// assert!(SSlice::<u8>::from_slice(b"\0").unwrap().is_empty());
    /// ```
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        // SAFETY:
        //  We're not modifying the underlying value.
        T::is_sentinel(unsafe { self.raw_first() })
    }

    /// Returns the first element of the slice, or [`None`] if it is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// assert_eq!(SSlice::<u8>::from_slice(b"123\0").unwrap().first(), Some(&b'1'));
    /// assert_eq!(SSlice::<u8>::from_slice(b"\0").unwrap().first(), None);
    /// ```
    #[inline]
    pub fn first(&self) -> Option<&T> {
        if self.is_empty() {
            None
        } else {
            // SAFETY:
            //  This value is not a sentinel, we can safely mutate it.
            Some(unsafe { self.raw_first() })
        }
    }

    /// Returns the first element of the slice, or [`None`] if it is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let mut array = [1, 2, 3, 0];
    /// let mut sslice = SSlice::<u8>::from_slice_mut(&mut array).unwrap();
    ///
    /// *sslice.first_mut().unwrap() = 0;
    ///
    /// assert_eq!(sslice.first_mut(), None);
    /// ```
    #[inline]
    pub fn first_mut(&mut self) -> Option<&mut T> {
        if self.is_empty() {
            None
        } else {
            // SAFETY:
            //  We know that this value is not a sentinel. We can safely mutate it.
            Some(unsafe { self.raw_first_mut() })
        }
    }

    /// Returns a pointer to the first element of the slice, and a slice to the remaining elements.
    /// [`None`] is returned if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let sslice = SSlice::<u8>::from_slice(b"1234\0").unwrap();
    /// let (first, remainder) = sslice.split_first().unwrap();
    /// assert_eq!(*first, '1' as u8);
    /// assert_eq!(remainder, b"234");
    /// ```
    pub fn split_first(&self) -> Option<(&T, &Self)> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { (&*self.as_ptr(), SSlice::from_ptr(self.as_ptr().add(1))) })
        }
    }

    /// Returns a pointer to the first element of the slice, and a slice to the remaining elements.
    /// [`None`] is returned if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use sentinel::SSlice;
    ///
    /// let mut array = *b"1234\0";
    /// let mut sslice = SSlice::<u8>::from_slice_mut(&mut array).unwrap();
    /// let (first, remainder) = sslice.split_first_mut().unwrap();
    /// assert_eq!(*first, '1' as u8);
    /// assert_eq!(remainder, b"234");
    ///
    /// *first = 0;
    ///
    /// assert!(sslice.is_empty());
    /// ```
    pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut Self)> {
        unsafe {
            if T::is_sentinel(&*self.as_ptr()) {
                None
            } else {
                Some((
                    &mut *self.as_mut_ptr(),
                    SSlice::from_mut_ptr(self.as_mut_ptr().add(1)),
                ))
            }
        }
    }

    /// Returns a shared reference to the first element of the slice, or a sentinel
    /// value if the slice is empty.
    ///
    /// # Safety
    ///
    /// If the returned value is a sentinel, it must not be modified (or rather, it must remain
    /// a sentinel).
    #[inline(always)]
    pub const unsafe fn raw_first(&self) -> &T {
        // SAFETY:
        //  The first element of an `SSlice<T>` is always exists.
        unsafe { &*self.as_ptr() }
    }

    /// Returns an exclusive reference to the first element of the slice, or a sentinel value if
    /// the slice is empty.
    ///
    /// # Safety
    ///
    /// If the returned value is a sentinel, it must not be modified (or rather, it must remain
    /// a sentinel).
    #[inline(always)]
    pub unsafe fn raw_first_mut(&mut self) -> &mut T {
        // SAFETY:
        //  The first element of an `SSlice<T>` is always exists.
        unsafe { &mut *self.as_mut_ptr() }
    }

    /// Returns a slice referencing every element of this [`SSlice<T>`], not including the
    /// terminating sentinel character.
    ///
    ///
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        let len = self.len();
        unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
    }

    /// Returns a slice referencing every element of this [`SSlice<T>`], not including the
    /// terminating sentinel character.
    #[inline]
    pub fn as_slice_mut(&mut self) -> &mut [T] {
        let len = self.len();
        unsafe { core::slice::from_raw_parts_mut(self.as_mut_ptr(), len) }
    }

    /// Returns a slice referencing every element of this [`SSlice<T>`], including the
    /// terminating sentinel character.
    #[inline]
    pub fn as_slice_with_sentinel(&self) -> &[T] {
        let len = self.len().wrapping_add(1);
        unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
    }

    /// Returns a slice referencing every element of this [`SSlice<T>`], including the
    /// terminating sentinel character.
    #[inline]
    pub fn as_slice_with_sentinel_mut(&mut self) -> &mut [T] {
        let len = self.len().wrapping_add(1);
        unsafe { core::slice::from_raw_parts_mut(self.as_mut_ptr(), len) }
    }

    /// Casts a slice of `SSlice<T>` values into a slice of `*const T`s.
    #[inline(always)]
    pub const fn cast_to_slice_of_ptrs<'a>(slice: &'a [&Self]) -> &'a [*const T] {
        unsafe { &*(slice as *const [&Self] as *const [*const T]) }
    }

    /// Casts a slice of `*const T`s into a slice of `SSlice<T>` values.
    ///
    /// # Safety
    ///
    /// It must be safe to call `SSlice::from_ptr` for every pointer in the slice (for the lifetime
    /// `'a`).
    #[inline(always)]
    pub const unsafe fn cast_to_slice_of_sslices<'a>(slice: &[*const T]) -> &[&'a Self] {
        unsafe { &*(slice as *const [*const T] as *const [&Self]) }
    }
}

impl CStr {
    /// Creates a new [`SSlice<T>`] from the provided standard [`core::ffi::CStr`].
    #[inline]
    pub const fn from_std_cstr(cstr: &core::ffi::CStr) -> &Self {
        // Safety:
        //  We know by invariant that the standard CStr is null-terminated.
        unsafe { Self::from_ptr(cstr.as_ptr() as *const u8) }
    }

    /// Turns this [`SSlice<T>`] into a standard [`core::ffi::CStr`].
    #[inline]
    pub fn as_std_cstr(&self) -> &core::ffi::CStr {
        unsafe { core::ffi::CStr::from_ptr(self.as_ptr() as *const core::ffi::c_char) }
    }

    /// Casts a slice of `CStr` values into a slice of `*const c_char`s.
    #[inline(always)]
    pub const fn cast_to_slice_of_cstrs<'a>(slice: &'a [&Self]) -> &'a [*const core::ffi::c_char] {
        unsafe { &*(slice as *const [&Self] as *const [*const core::ffi::c_char]) }
    }

    /// An implementation of [`fmt::Display`] and [`fmt::Debug`] for the [`CStr`] type.
    ///
    /// When an invalid character is found, the [`REPLACEMENT_CHARACTER`] is displayed instead.
    ///
    /// [`REPLACEMENT_CHARACTER`]: core::char::REPLACEMENT_CHARACTER
    #[inline(always)]
    pub const fn display(&self) -> &Display {
        unsafe { &*(self as *const Self as *const Display) }
    }
}

/// Implements [`fmt::Display`] [`fmt::Debug`] for a [`CStr`].
#[repr(transparent)]
pub struct Display(CStr);

impl fmt::Display for Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut slice = self.0.as_slice();
        loop {
            let s = next_str_part(&mut slice);
            f.write_str(s)?;

            if slice.is_empty() {
                break;
            }

            f.write_char(char::REPLACEMENT_CHARACTER)?;
        }
        Ok(())
    }
}

impl fmt::Debug for Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut slice = self.0.as_slice();

        f.write_str("\"")?;
        loop {
            let s = next_str_part(&mut slice);
            fmt::Display::fmt(&s.escape_debug(), f)?;

            if slice.is_empty() {
                break;
            }

            f.write_char(char::REPLACEMENT_CHARACTER)?;
        }
        f.write_str("\"")?;
        Ok(())
    }
}

/// Returns the largest prefix of `slice` that's valid UTF-8 and strips.
fn next_str_part<'a>(slice: &mut &'a [u8]) -> &'a str {
    match core::str::from_utf8(slice) {
        Ok(ok) => {
            *slice = &[];
            ok
        }
        Err(err) => unsafe {
            // SAFETY:
            //  We know that the slice is valid up to that point. We can construct a valid `str`
            //  from those bytes that have been validated.
            let ret = core::str::from_utf8_unchecked(slice.get_unchecked(..err.valid_up_to()));

            // SAFETY:
            //  If the slice is valid up to that point, then its length must be at least that many
            //  bytes.
            *slice = slice.get_unchecked(err.valid_up_to()..);

            ret
        },
    }
}

impl<T: Sentinel + Hash> Hash for SSlice<T> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.iter().for_each(|x| x.hash(state));
    }
}

impl<T: Sentinel> Drop for SSlice<T> {
    fn drop(&mut self) {
        struct Guard<'a, T: Sentinel>(&'a mut Iter<T>);

        impl<'a, T: Sentinel> Guard<'a, T> {
            pub fn drop_content(&mut self) {
                for elem in &mut self.0 {
                    unsafe { core::ptr::drop_in_place(elem) };
                }
            }
        }

        impl<'a, T: Sentinel> Drop for Guard<'a, T> {
            fn drop(&mut self) {
                self.drop_content();
            }
        }

        let mut guard = Guard(self.iter_mut());
        guard.drop_content();
        core::mem::forget(guard);
    }
}

unsafe impl<T: Sync + Sentinel> Sync for SSlice<T> {}
unsafe impl<T: Send + Sentinel> Send for SSlice<T> {}

impl<T: UnwindSafe + Sentinel> UnwindSafe for SSlice<T> {}
impl<T: RefUnwindSafe + Sentinel> RefUnwindSafe for SSlice<T> {}

impl<T: Unpin + Sentinel> Unpin for SSlice<T> {}

impl<T1, T2> PartialEq<SSlice<T2>> for SSlice<T1>
where
    T1: PartialEq<T2>,
    T1: Sentinel,
    T2: Sentinel,
{
    #[inline]
    fn eq(&self, other: &SSlice<T2>) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<T: Eq + Sentinel> Eq for SSlice<T> {}

impl<T1, T2> PartialEq<[T2]> for SSlice<T1>
where
    T1: PartialEq<T2>,
    T1: Sentinel,
{
    #[inline]
    fn eq(&self, other: &[T2]) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<T1, T2> PartialEq<SSlice<T2>> for [T1]
where
    T1: PartialEq<T2>,
    T2: Sentinel,
{
    #[inline]
    fn eq(&self, other: &SSlice<T2>) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<T1, T2, const N: usize> PartialEq<[T2; N]> for SSlice<T1>
where
    T1: PartialEq<T2>,
    T1: Sentinel,
{
    #[inline]
    fn eq(&self, other: &[T2; N]) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<T1, T2, const N: usize> PartialEq<SSlice<T2>> for [T1; N]
where
    T1: PartialEq<T2>,
    T2: Sentinel,
{
    #[inline]
    fn eq(&self, other: &SSlice<T2>) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<T1, T2> PartialOrd<SSlice<T2>> for SSlice<T1>
where
    T1: PartialOrd<T2>,
    T1: Sentinel,
    T2: Sentinel,
{
    #[inline]
    fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<T: Ord + Sentinel> Ord for SSlice<T> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other.iter())
    }
}

impl<T1, T2> PartialOrd<[T2]> for SSlice<T1>
where
    T1: PartialOrd<T2> + Sentinel,
{
    #[inline]
    fn partial_cmp(&self, other: &[T2]) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<T1, T2> PartialOrd<SSlice<T2>> for [T1]
where
    T1: PartialOrd<T2>,
    T2: Sentinel,
{
    #[inline]
    fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<T1, T2, const N: usize> PartialOrd<[T2; N]> for SSlice<T1>
where
    T1: PartialOrd<T2>,
    T1: Sentinel,
{
    #[inline]
    fn partial_cmp(&self, other: &[T2; N]) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<T1, T2, const N: usize> PartialOrd<SSlice<T2>> for [T1; N]
where
    T1: PartialOrd<T2>,
    T2: Sentinel,
{
    #[inline]
    fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<'a, T: Sentinel> IntoIterator for &'a SSlice<T> {
    type IntoIter = &'a Iter<T>;
    type Item = &'a T;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T: Sentinel> IntoIterator for &'a mut SSlice<T> {
    type IntoIter = &'a mut Iter<T>;
    type Item = &'a mut T;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T: fmt::Debug + Sentinel> fmt::Debug for SSlice<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

/// Creates a new [`CStr`] using a string literal. A null byte is automatically appended at the end
/// of that literal, ensuring the safety of the operation.
///
/// # Examples
///
/// ```
/// let s = sentinel::cstr!("Hello, World!");
/// assert_eq!(s, b"Hello, World!");
/// ```
#[macro_export]
macro_rules! cstr {
    ($s:literal) => {
        unsafe { $crate::CStr::from_ptr(::core::concat!($s, "\0").as_ptr()) }
    };
}

#[cfg(test)]
#[test]
fn from_slice() {
    let mut slice = *b"hello\0test";

    let s = SSlice::<u8>::from_slice(&slice).unwrap();
    assert_eq!(s, b"hello");

    let s = SSlice::<u8>::from_slice_mut(&mut slice).unwrap();
    assert_eq!(s, b"hello");
}

#[cfg(test)]
#[test]
fn from_slice_none() {
    let mut slice = *b"hello";

    assert!(SSlice::<u8>::from_slice(&slice).is_none());
    assert!(SSlice::<u8>::from_slice_mut(&mut slice).is_none());
}

#[cfg(test)]
#[test]
fn from_slice_split_middle() {
    let mut slice = *b"abc\0def";

    let (s, r) = SSlice::<u8>::from_slice_split(&slice).unwrap();
    assert_eq!(s, b"abc");
    assert_eq!(r, b"def");

    let (s, r) = SSlice::<u8>::from_slice_split_mut(&mut slice).unwrap();
    assert_eq!(s, b"abc");
    assert_eq!(r, b"def");
}

#[cfg(test)]
#[test]
fn from_slice_split_start() {
    let mut slice = *b"\0abcdef";

    let (s, r) = SSlice::<u8>::from_slice_split(&slice).unwrap();
    assert_eq!(s, b"");
    assert_eq!(r, b"abcdef");

    let (s, r) = SSlice::<u8>::from_slice_split_mut(&mut slice).unwrap();
    assert_eq!(s, b"");
    assert_eq!(r, b"abcdef");
}

#[cfg(test)]
#[test]
fn from_slice_split_end() {
    let mut slice = *b"abcdef\0";

    let (s, r) = SSlice::<u8>::from_slice_split(&slice).unwrap();
    assert_eq!(s, b"abcdef");
    assert_eq!(r, b"");

    let (s, r) = SSlice::<u8>::from_slice_split_mut(&mut slice).unwrap();
    assert_eq!(s, b"abcdef");
    assert_eq!(r, b"");
}

#[cfg(test)]
#[test]
fn from_slice_split_none() {
    let mut slice = *b"abcdef";

    assert!(SSlice::<u8>::from_slice_split(&slice).is_none());
    assert!(SSlice::<u8>::from_slice_split_mut(&mut slice).is_none());
}

#[cfg(test)]
#[test]
fn len() {
    let s = SSlice::<u8>::from_slice(b"Hello\0").unwrap();
    assert_eq!(s.len(), 5);
    assert!(!s.is_empty());

    let s = SSlice::<u8>::from_slice(b"\0").unwrap();
    assert_eq!(s.len(), 0);
    assert!(s.is_empty());
}

#[cfg(test)]
#[test]
fn split_first() {
    let mut slice = *b"hello\0";

    let s = SSlice::<u8>::from_slice(&slice).unwrap();
    let (a, r) = s.split_first().unwrap();
    assert_eq!(a, &b'h');
    assert_eq!(r, b"ello");

    let s = SSlice::<u8>::from_slice_mut(&mut slice).unwrap();
    let (a, r) = s.split_first().unwrap();
    assert_eq!(a, &b'h');
    assert_eq!(r, b"ello");
}

#[cfg(test)]
#[test]
fn as_slice() {
    let mut slice = *b"hello\0";

    let s = SSlice::<u8>::from_slice(&slice).unwrap();
    let sl = s.as_slice();
    assert_eq!(sl, b"hello");

    let s = SSlice::<u8>::from_slice_mut(&mut slice).unwrap();
    let sl = s.as_slice_mut();
    assert_eq!(sl, b"hello");
}

#[cfg(test)]
#[test]
fn cstr_macro() {
    let s = cstr!("test");
    assert_eq!(s, b"test");
}

#[cfg(test)]
#[test]
fn cstr_macro_empty() {
    let s = cstr!("");
    assert_eq!(s, b"");
}