asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Immutable, reference-counted byte slice.

use super::buf::Buf;
use std::ops::{Deref, RangeBounds};
use std::sync::Arc;

/// Immutable byte slice with cheap cloning.
///
/// Cloning a `Bytes` is O(1) - it just increments a reference count.
/// Slicing is also O(1) - no data is copied, just the view is adjusted.
///
/// # Implementation
///
/// This implementation uses `Arc<Vec<u8>>` for shared ownership rather than
/// raw pointers, ensuring memory safety without unsafe code.
///
/// # Examples
///
/// ```
/// use asupersync::bytes::Bytes;
///
/// // Create from static data (no allocation)
/// let b = Bytes::from_static(b"hello world");
/// assert_eq!(&b[..], b"hello world");
///
/// // Clone is cheap (reference counting)
/// let b2 = b.clone();
/// assert_eq!(&b2[..], b"hello world");
///
/// // Slicing is O(1)
/// let hello = b.slice(0..5);
/// assert_eq!(&hello[..], b"hello");
/// ```
#[derive(Clone)]
pub struct Bytes {
    /// The backing storage.
    data: BytesInner,
    /// Start offset within the backing storage.
    start: usize,
    /// Length of this view.
    len: usize,
}

#[derive(Clone)]
enum BytesInner {
    /// Static data (no allocation, 'static lifetime).
    Static(&'static [u8]),
    /// Heap-allocated, reference-counted data.
    Shared(Arc<Vec<u8>>),
    /// Empty bytes (no allocation).
    Empty,
}

impl Bytes {
    /// Create an empty `Bytes`.
    ///
    /// No allocation occurs.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            data: BytesInner::Empty,
            start: 0,
            len: 0,
        }
    }

    /// Create `Bytes` from a static byte slice.
    ///
    /// No allocation occurs - the bytes point directly to static memory.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::Bytes;
    ///
    /// let b = Bytes::from_static(b"hello");
    /// assert_eq!(&b[..], b"hello");
    /// ```
    #[inline]
    #[must_use]
    pub const fn from_static(bytes: &'static [u8]) -> Self {
        Self {
            data: BytesInner::Static(bytes),
            start: 0,
            len: bytes.len(),
        }
    }

    /// Copy data from a slice into a new `Bytes`.
    ///
    /// This allocates and copies the data.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::Bytes;
    ///
    /// let data = vec![1, 2, 3, 4, 5];
    /// let b = Bytes::copy_from_slice(&data);
    /// assert_eq!(&b[..], &[1, 2, 3, 4, 5]);
    /// ```
    #[inline]
    #[must_use]
    pub fn copy_from_slice(data: &[u8]) -> Self {
        if data.is_empty() {
            return Self::new();
        }
        let vec = data.to_vec();
        let len = vec.len();
        Self {
            data: BytesInner::Shared(Arc::new(vec)),
            start: 0,
            len,
        }
    }

    /// Returns the number of bytes.
    #[inline]
    #[must_use]
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Returns true if empty.
    #[inline]
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns a slice of self for the given range.
    ///
    /// # Panics
    ///
    /// Panics if the range is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::Bytes;
    ///
    /// let b = Bytes::from_static(b"hello world");
    /// let hello = b.slice(0..5);
    /// assert_eq!(&hello[..], b"hello");
    /// ```
    #[inline]
    #[must_use]
    pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
        use std::ops::Bound;

        let start = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n.checked_add(1).expect("range start overflow"),
            Bound::Unbounded => 0,
        };

        let end = match range.end_bound() {
            Bound::Included(&n) => n.checked_add(1).expect("range end overflow"),
            Bound::Excluded(&n) => n,
            Bound::Unbounded => self.len,
        };

        assert!(
            start <= end && end <= self.len,
            "slice bounds out of range: start={start}, end={end}, len={}",
            self.len
        );

        Self {
            data: self.data.clone(),
            start: self.start + start,
            len: end - start,
        }
    }

    /// Split off the bytes from `at` to the end.
    ///
    /// Self becomes `[0, at)`, returns `[at, len)`.
    ///
    /// # Panics
    ///
    /// Panics if `at > len`.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::Bytes;
    ///
    /// let mut b = Bytes::from_static(b"hello world");
    /// let world = b.split_off(6);
    /// assert_eq!(&b[..], b"hello ");
    /// assert_eq!(&world[..], b"world");
    /// ```
    #[inline]
    #[must_use]
    pub fn split_off(&mut self, at: usize) -> Self {
        assert!(
            at <= self.len,
            "split_off out of bounds: at={at}, len={}",
            self.len
        );

        let other = Self {
            data: self.data.clone(),
            start: self.start + at,
            len: self.len - at,
        };

        self.len = at;
        other
    }

    /// Split off bytes from the beginning.
    ///
    /// Self becomes `[at, len)`, returns `[0, at)`.
    ///
    /// # Panics
    ///
    /// Panics if `at > len`.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::Bytes;
    ///
    /// let mut b = Bytes::from_static(b"hello world");
    /// let hello = b.split_to(6);
    /// assert_eq!(&hello[..], b"hello ");
    /// assert_eq!(&b[..], b"world");
    /// ```
    #[inline]
    #[must_use]
    pub fn split_to(&mut self, at: usize) -> Self {
        assert!(
            at <= self.len,
            "split_to out of bounds: at={at}, len={}",
            self.len
        );

        let other = Self {
            data: self.data.clone(),
            start: self.start,
            len: at,
        };

        self.start += at;
        self.len -= at;
        other
    }

    /// Truncate the buffer to `len` bytes.
    ///
    /// If `len` is greater than the current length, this has no effect.
    #[inline]
    pub fn truncate(&mut self, len: usize) {
        if len < self.len {
            self.len = len;
        }
    }

    /// Clear the buffer, making it empty.
    #[inline]
    pub fn clear(&mut self) {
        self.len = 0;
    }

    /// Get the underlying byte slice.
    #[inline]
    fn as_slice(&self) -> &[u8] {
        match &self.data {
            BytesInner::Empty => &[],
            BytesInner::Static(s) => &s[self.start..self.start + self.len],
            BytesInner::Shared(arc) => &arc[self.start..self.start + self.len],
        }
    }
}

impl Default for Bytes {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for Bytes {
    type Target = [u8];

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

impl AsRef<[u8]> for Bytes {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl From<Vec<u8>> for Bytes {
    #[inline]
    fn from(vec: Vec<u8>) -> Self {
        if vec.is_empty() {
            return Self::new();
        }
        let len = vec.len();
        Self {
            data: BytesInner::Shared(Arc::new(vec)),
            start: 0,
            len,
        }
    }
}

impl From<&'static [u8]> for Bytes {
    #[inline]
    fn from(slice: &'static [u8]) -> Self {
        Self::from_static(slice)
    }
}

impl From<&'static str> for Bytes {
    #[inline]
    fn from(s: &'static str) -> Self {
        Self::from_static(s.as_bytes())
    }
}

impl From<String> for Bytes {
    #[inline]
    fn from(s: String) -> Self {
        Self::from(s.into_bytes())
    }
}

impl std::fmt::Debug for Bytes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Bytes")
            .field("len", &self.len)
            .field("start", &self.start)
            .field("data", &self.as_slice())
            .finish()
    }
}

impl PartialEq for Bytes {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl Eq for Bytes {}

impl PartialEq<[u8]> for Bytes {
    #[inline]
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

impl PartialEq<Bytes> for [u8] {
    #[inline]
    fn eq(&self, other: &Bytes) -> bool {
        self == other.as_slice()
    }
}

impl PartialEq<Vec<u8>> for Bytes {
    #[inline]
    fn eq(&self, other: &Vec<u8>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl std::hash::Hash for Bytes {
    #[inline]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state);
    }
}

// === Buf trait implementation ===

/// A cursor for reading from Bytes.
///
/// This wrapper tracks the read position, allowing Bytes to implement Buf.
#[derive(Clone, Debug)]
pub struct BytesCursor {
    inner: Bytes,
    pos: usize,
}

impl BytesCursor {
    /// Create a new cursor at position 0.
    #[inline]
    #[must_use]
    pub fn new(bytes: Bytes) -> Self {
        Self {
            inner: bytes,
            pos: 0,
        }
    }

    /// Get a reference to the underlying Bytes.
    #[inline]
    #[must_use]
    pub fn get_ref(&self) -> &Bytes {
        &self.inner
    }

    /// Consume the cursor, returning the underlying Bytes.
    #[inline]
    #[must_use]
    pub fn into_inner(self) -> Bytes {
        self.inner
    }

    /// Get the current position.
    #[inline]
    #[must_use]
    pub fn position(&self) -> usize {
        self.pos
    }

    /// Set the position.
    #[inline]
    pub fn set_position(&mut self, pos: usize) {
        self.pos = pos;
    }
}

impl Buf for BytesCursor {
    #[inline]
    fn remaining(&self) -> usize {
        self.inner.len().saturating_sub(self.pos)
    }

    #[inline]
    fn chunk(&self) -> &[u8] {
        let slice = self.inner.as_slice();
        if self.pos >= slice.len() {
            &[]
        } else {
            &slice[self.pos..]
        }
    }

    #[inline]
    fn advance(&mut self, cnt: usize) {
        assert!(
            cnt <= self.remaining(),
            "advance out of bounds: cnt={cnt}, remaining={}",
            self.remaining()
        );
        self.pos += cnt;
    }
}

impl Bytes {
    /// Create a cursor for reading from this Bytes.
    ///
    /// The cursor implements `Buf` and tracks the read position.
    ///
    /// # Examples
    ///
    /// ```
    /// use asupersync::bytes::{Bytes, Buf};
    ///
    /// let b = Bytes::from_static(b"\x00\x01\x02\x03");
    /// let mut cursor = b.reader();
    /// assert_eq!(cursor.get_u8(), 0);
    /// assert_eq!(cursor.get_u8(), 1);
    /// assert_eq!(cursor.remaining(), 2);
    /// ```
    #[inline]
    #[must_use]
    pub fn reader(self) -> BytesCursor {
        BytesCursor::new(self)
    }
}

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

    fn init_test(name: &str) {
        crate::test_utils::init_test_logging();
        crate::test_phase!(name);
    }

    #[test]
    fn test_bytes_new() {
        init_test("test_bytes_new");
        let b = Bytes::new();
        let empty = b.is_empty();
        crate::assert_with_log!(empty, "empty", true, empty);
        let len = b.len();
        crate::assert_with_log!(len == 0, "len", 0, len);
        crate::test_complete!("test_bytes_new");
    }

    #[test]
    fn test_bytes_from_static() {
        init_test("test_bytes_from_static");
        let b = Bytes::from_static(b"hello world");
        let len = b.len();
        crate::assert_with_log!(len == 11, "len", 11, len);
        let ok = &b[..] == b"hello world";
        crate::assert_with_log!(ok, "contents", b"hello world", &b[..]);
        crate::test_complete!("test_bytes_from_static");
    }

    #[test]
    fn test_bytes_copy_from_slice() {
        init_test("test_bytes_copy_from_slice");
        let data = vec![1u8, 2, 3, 4, 5];
        let b = Bytes::copy_from_slice(&data);
        let len = b.len();
        crate::assert_with_log!(len == 5, "len", 5, len);
        let ok = b[..] == data[..];
        crate::assert_with_log!(ok, "contents", &data[..], &b[..]);
        crate::test_complete!("test_bytes_copy_from_slice");
    }

    #[test]
    fn test_bytes_clone_is_cheap() {
        init_test("test_bytes_clone_is_cheap");
        let b1 = Bytes::copy_from_slice(&vec![0u8; 1_000_000]);
        let start = std::time::Instant::now();
        for _ in 0..1000 {
            let _b2 = b1.clone();
        }
        let elapsed = start.elapsed();
        // Should be very fast (reference counting)
        let fast = elapsed.as_millis() < 50;
        crate::assert_with_log!(fast, "clone fast", true, fast);
        crate::test_complete!("test_bytes_clone_is_cheap");
    }

    #[test]
    fn test_bytes_slice() {
        init_test("test_bytes_slice");
        let b = Bytes::from_static(b"hello world");

        let hello = b.slice(0..5);
        let ok = &hello[..] == b"hello";
        crate::assert_with_log!(ok, "hello", b"hello", &hello[..]);

        let world = b.slice(6..);
        let ok = &world[..] == b"world";
        crate::assert_with_log!(ok, "world", b"world", &world[..]);

        let middle = b.slice(3..8);
        let ok = &middle[..] == b"lo wo";
        crate::assert_with_log!(ok, "middle", b"lo wo", &middle[..]);
        crate::test_complete!("test_bytes_slice");
    }

    #[test]
    fn test_bytes_split_off() {
        init_test("test_bytes_split_off");
        let mut b = Bytes::from_static(b"hello world");
        let world = b.split_off(6);

        let ok = &b[..] == b"hello ";
        crate::assert_with_log!(ok, "left", b"hello ", &b[..]);
        let ok = &world[..] == b"world";
        crate::assert_with_log!(ok, "world", b"world", &world[..]);
        crate::test_complete!("test_bytes_split_off");
    }

    #[test]
    fn test_bytes_split_to() {
        init_test("test_bytes_split_to");
        let mut b = Bytes::from_static(b"hello world");
        let hello = b.split_to(6);

        let ok = &hello[..] == b"hello ";
        crate::assert_with_log!(ok, "left", b"hello ", &hello[..]);
        let ok = &b[..] == b"world";
        crate::assert_with_log!(ok, "world", b"world", &b[..]);
        crate::test_complete!("test_bytes_split_to");
    }

    #[test]
    fn test_bytes_truncate() {
        init_test("test_bytes_truncate");
        let mut b = Bytes::from_static(b"hello world");
        b.truncate(5);
        let ok = &b[..] == b"hello";
        crate::assert_with_log!(ok, "truncate", b"hello", &b[..]);

        // Truncate to larger has no effect
        b.truncate(100);
        let ok = &b[..] == b"hello";
        crate::assert_with_log!(ok, "truncate unchanged", b"hello", &b[..]);
        crate::test_complete!("test_bytes_truncate");
    }

    #[test]
    fn test_bytes_clear() {
        init_test("test_bytes_clear");
        let mut b = Bytes::from_static(b"hello world");
        b.clear();
        let empty = b.is_empty();
        crate::assert_with_log!(empty, "empty", true, empty);
        crate::test_complete!("test_bytes_clear");
    }

    #[test]
    fn test_bytes_from_vec() {
        init_test("test_bytes_from_vec");
        let v = vec![1u8, 2, 3];
        let b: Bytes = v.into();
        let ok = b[..] == [1, 2, 3];
        crate::assert_with_log!(ok, "from vec", &[1, 2, 3], &b[..]);
        crate::test_complete!("test_bytes_from_vec");
    }

    #[test]
    fn test_bytes_from_string() {
        init_test("test_bytes_from_string");
        let s = String::from("hello");
        let b: Bytes = s.into();
        let ok = &b[..] == b"hello";
        crate::assert_with_log!(ok, "from string", b"hello", &b[..]);
        crate::test_complete!("test_bytes_from_string");
    }

    #[test]
    #[should_panic(expected = "slice bounds out of range")]
    fn test_bytes_slice_panic() {
        init_test("test_bytes_slice_panic");
        let b = Bytes::from_static(b"hello");
        let _bad = b.slice(0..100);
    }

    #[test]
    #[should_panic(expected = "out of bounds")]
    fn test_bytes_split_off_panic() {
        init_test("test_bytes_split_off_panic");
        let mut b = Bytes::from_static(b"hello");
        let _bad = b.split_off(100);
    }

    // =========================================================================
    // Wave 32: Data-type trait coverage
    // =========================================================================

    #[test]
    fn bytes_debug() {
        let b = Bytes::from_static(b"hi");
        let dbg = format!("{b:?}");
        assert!(dbg.contains("Bytes"));
        assert!(dbg.contains("len"));
    }

    #[test]
    fn bytes_default() {
        let b = Bytes::default();
        assert!(b.is_empty());
        assert_eq!(b.len(), 0);
    }

    #[test]
    fn bytes_hash() {
        use std::collections::HashSet;
        let a = Bytes::from_static(b"hello");
        let b = Bytes::copy_from_slice(b"hello");
        let c = Bytes::from_static(b"world");
        let mut set = HashSet::new();
        set.insert(a);
        set.insert(b);
        set.insert(c);
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn bytes_partial_eq_vec() {
        let b = Bytes::from_static(b"hello");
        let v = b"hello".to_vec();
        assert!(b == v);
    }

    #[test]
    fn bytes_as_ref() {
        let b = Bytes::from_static(b"hello");
        let r: &[u8] = b.as_ref();
        assert_eq!(r, b"hello");
    }

    #[test]
    fn bytes_from_static_slice() {
        let b: Bytes = (b"test" as &'static [u8]).into();
        assert_eq!(&b[..], b"test");
    }

    #[test]
    fn bytes_from_str() {
        let b: Bytes = "hello".into();
        assert_eq!(&b[..], b"hello");
    }

    #[test]
    fn bytes_cursor_debug_clone() {
        let b = Bytes::from_static(b"hello");
        let cursor = BytesCursor::new(b);
        let dbg = format!("{cursor:?}");
        assert!(dbg.contains("BytesCursor"));
        let cloned = cursor;
        assert_eq!(cloned.position(), 0);
    }

    #[test]
    fn bytes_cursor_position() {
        let b = Bytes::from_static(b"hello");
        let mut cursor = BytesCursor::new(b);
        assert_eq!(cursor.position(), 0);
        cursor.set_position(3);
        assert_eq!(cursor.position(), 3);
    }

    #[test]
    fn bytes_cursor_get_ref_into_inner() {
        let b = Bytes::from_static(b"hello");
        let cursor = BytesCursor::new(b.clone());
        assert_eq!(cursor.get_ref(), &b);
        let inner = cursor.into_inner();
        assert_eq!(&inner[..], b"hello");
    }

    #[test]
    fn bytes_cursor_buf_trait() {
        let b = Bytes::from_static(b"hello");
        let mut cursor = b.reader();
        assert_eq!(cursor.remaining(), 5);
        assert_eq!(cursor.chunk(), b"hello");
        cursor.advance(3);
        assert_eq!(cursor.remaining(), 2);
        assert_eq!(cursor.chunk(), b"lo");
    }

    #[test]
    fn test_bytes_equality() {
        init_test("test_bytes_equality");
        let b1 = Bytes::from_static(b"hello");
        let b2 = Bytes::copy_from_slice(b"hello");
        let ok = b1 == b2;
        crate::assert_with_log!(ok, "b1 == b2", b2, b1);
        let ok = b1 == b"hello"[..];
        crate::assert_with_log!(ok, "b1 == slice", b"hello".as_slice(), b1);
        let ok = b"hello"[..] == b1;
        crate::assert_with_log!(ok, "slice == b1", b1, b"hello".as_slice());
        crate::test_complete!("test_bytes_equality");
    }

    proptest! {
        #[test]
        fn metamorphic_slice_matches_split_extraction(
            data in prop::collection::vec(any::<u8>(), 0..128),
            start in 0usize..128,
            end in 0usize..128,
        ) {
            let bytes = Bytes::copy_from_slice(&data);
            let len = bytes.len();
            let start = start.min(len);
            let end = end.min(len);
            let (start, end) = if start <= end {
                (start, end)
            } else {
                (end, start)
            };

            let direct = bytes.slice(start..end);

            let mut split_view = bytes.clone();
            let _prefix = split_view.split_to(start);
            let middle = split_view.split_to(end - start);

            prop_assert_eq!(
                &direct[..],
                &middle[..],
                "direct slicing and split-based extraction must expose identical contiguous bytes",
            );
            prop_assert_eq!(
                &direct[..],
                &data[start..end],
                "both extraction paths must match the original contiguous source slice",
            );
        }
    }
}