hyperlight-common 0.19.0

Hyperlight's components common to host and guest.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 The Hyperlight Authors.

//! Owned and segmented virtqueue buffer representations.

use alloc::vec::Vec;

use bytes::{Buf, Bytes};
use smallvec::{SmallVec, smallvec};

use super::{Allocation, SlotPool};

/// Ordered byte segments that make up one virtqueue payload.
///
/// This is the high-level counterpart to the descriptor-oriented
/// [`BufferChain`](super::BufferChain).
#[derive(Debug, Default)]
pub struct Segments {
    chunks: SmallVec<[Bytes; 4]>,
    /// Earlier slots contain empty `Bytes` and retain no owners.
    head: usize,
}

impl Segments {
    /// Build a segmented payload from ordered byte segments.
    pub fn new(segments: impl IntoIterator<Item = Bytes>) -> Self {
        Self::from_smallvec(segments.into_iter().collect())
    }

    /// Build a single-segment payload.
    pub fn single(segment: Bytes) -> Self {
        Self::from_smallvec(smallvec![segment])
    }

    /// Reuse an existing segment list.
    pub(crate) fn from_smallvec(segments: SmallVec<[Bytes; 4]>) -> Self {
        Self {
            chunks: segments,
            head: 0,
        }
    }

    /// Total payload length across all segments.
    pub fn len(&self) -> usize {
        self.iter().map(Bytes::len).sum()
    }

    /// Whether the payload contains zero bytes.
    pub fn is_empty(&self) -> bool {
        self.iter().all(Bytes::is_empty)
    }

    /// Number of byte segments.
    pub fn segment_count(&self) -> usize {
        self.chunks.len() - self.head
    }

    /// Borrow all segments.
    pub fn as_slice(&self) -> &[Bytes] {
        &self.chunks[self.head..]
    }

    /// Iterate over segments.
    pub fn iter(&self) -> impl Iterator<Item = &Bytes> {
        self.as_slice().iter()
    }

    /// Append another payload without copying its bytes.
    ///
    /// Compacts consumed slots before extending the segment list.
    pub fn append(&mut self, other: Self) {
        if self.head != 0 {
            drop(self.chunks.drain(..self.head));
            self.head = 0;
        }

        self.chunks
            .extend(other.chunks.into_iter().skip(other.head));
    }

    /// Split off an owned byte prefix without copying payload data.
    ///
    /// Scans only the prefix, then transfers its segments without shifting the
    /// remainder. A boundary segment shares its [`Bytes`] owner.
    ///
    /// Returns `None` and leaves `self` unchanged when `len` exceeds the
    /// remaining payload length.
    pub fn split_to(&mut self, len: usize) -> Option<Self> {
        if len == 0 {
            return Some(Self::default());
        }

        let mut end = self.head;
        let mut remaining = len;

        while remaining != 0 {
            let segment = self.chunks.get(end)?;

            if segment.len() > remaining {
                break;
            }

            remaining -= segment.len();
            end += 1;
        }

        // Small suffixes must not inherit storage for consumed segments.
        if self.head == 0 && end == self.chunks.len() {
            return Some(core::mem::take(self));
        }

        let mut prefix =
            SmallVec::<[Bytes; 4]>::with_capacity(end - self.head + usize::from(remaining != 0));

        for segment in &mut self.chunks[self.head..end] {
            prefix.push(core::mem::take(segment));
        }

        self.head = end;

        if remaining != 0 {
            prefix.push(self.chunks[end].split_to(remaining));
        }

        Some(Self::from_smallvec(prefix))
    }

    /// Borrow this payload as a [`Buf`] cursor.
    pub fn as_buf(&self) -> SegmentsBuf<'_> {
        SegmentsBuf::new(self.as_slice(), self.len())
    }

    /// Return this payload as contiguous bytes.
    ///
    /// This is O(1) for zero or one segment, and allocates/copies for multiple
    /// segments.
    pub fn to_bytes(&self) -> Bytes {
        match self.as_slice() {
            [] => Bytes::new(),
            [segment] => segment.clone(),
            _ => self.collect(self.as_slice(), self.len()),
        }
    }

    /// Consume this payload and return contiguous bytes.
    ///
    /// Reuses a single segment's storage, and allocates/copies for multiple segments.
    pub fn into_bytes(mut self) -> Bytes {
        match self.segment_count() {
            0 => Bytes::new(),
            1 => self.chunks.pop().unwrap_or_default(),
            _ => self.collect(self.as_slice(), self.len()),
        }
    }

    /// Consume this payload without flattening its segments.
    pub fn into_chunks(mut self) -> Vec<Bytes> {
        if self.head != 0 {
            drop(self.chunks.drain(..self.head));
        }

        self.chunks.into_vec()
    }

    fn collect(&self, sgs: &[Bytes], len: usize) -> Bytes {
        let mut out = Vec::with_capacity(len);
        for seg in sgs {
            out.extend_from_slice(seg);
        }
        Bytes::from(out)
    }
}

impl Clone for Segments {
    /// Clone only unconsumed segment handles.
    fn clone(&self) -> Self {
        Self::new(self.iter().cloned())
    }
}

/// Borrowed [`Buf`] cursor over [`Segments`].
///
/// Advancing the cursor does not mutate the underlying [`Segments`].
#[derive(Debug, Clone)]
pub struct SegmentsBuf<'a> {
    segments: &'a [Bytes],
    index: usize,
    offset: usize,
    remaining: usize,
}

impl<'a> SegmentsBuf<'a> {
    fn new(segments: &'a [Bytes], len: usize) -> Self {
        let mut this = Self {
            segments,
            index: 0,
            offset: 0,
            remaining: len,
        };

        this.skip_empty_segments();
        this
    }

    fn skip_empty_segments(&mut self) {
        while self.index < self.segments.len() && self.offset >= self.segments[self.index].len() {
            self.index += 1;
            self.offset = 0;
        }
    }
}

impl Buf for SegmentsBuf<'_> {
    fn remaining(&self) -> usize {
        self.remaining
    }

    fn chunk(&self) -> &[u8] {
        if self.remaining == 0 {
            return &[];
        }

        let segment = self.segments[self.index].as_ref();
        &segment[self.offset..]
    }

    fn advance(&mut self, cnt: usize) {
        assert!(cnt <= self.remaining, "cannot advance past remaining bytes");

        self.remaining -= cnt;
        let mut cnt = cnt;

        while cnt > 0 {
            let seg_rem = self.segments[self.index].len() - self.offset;
            let n = seg_rem.min(cnt);
            self.offset += n;
            cnt -= n;
            self.skip_empty_segments();
        }

        if self.remaining == 0 {
            self.index = self.segments.len();
            self.offset = 0;
        }
    }
}

/// A buffer allocation returned to its original pool on drop.
pub struct BufferLease {
    /// The pool that allocated the buffer.
    pool: SlotPool,
    /// The buffer's start address and full allocation capacity.
    allocation: Allocation,
}

impl BufferLease {
    /// Create a new buffer lease from a pool and allocation.
    pub fn new(pool: SlotPool, allocation: Allocation) -> Self {
        Self { pool, allocation }
    }

    /// The buffer's start address and full allocation capacity.
    pub fn allocation(&self) -> Allocation {
        self.allocation
    }
}

impl Drop for BufferLease {
    fn drop(&mut self) {
        if let Err(error) = self.pool.dealloc(self.allocation.addr) {
            log::error!("Failed to release a virtqueue buffer: {error}");
            debug_assert!(false, "BufferLease deallocation failed: {error}");
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::sync::Arc;

    use bytes::Buf;

    use super::*;
    use crate::virtq::{SlotLayout, SlotPool};

    #[test]
    fn lease_returns_slot_on_drop() {
        let layout = SlotLayout::new(0, 4, 1).unwrap();
        let pool = SlotPool::new(layout).unwrap();
        let allocation = pool.alloc(4).unwrap();
        let lease = BufferLease::new(pool.clone(), allocation);

        assert_eq!(lease.allocation().addr, allocation.addr);
        assert_eq!(lease.allocation().len, 4);
        assert_eq!(pool.num_free(), 0);

        drop(lease);
        assert_eq!(pool.num_free(), 1);

        let reused = pool.alloc(4).unwrap();
        assert_eq!(reused.addr, allocation.addr);
        pool.dealloc(reused.addr).unwrap();
    }

    #[test]
    fn old_leases_do_not_release_a_reused_slot() {
        let mut pool = SlotPool::new(SlotLayout::new(0x1000, 4096, 1).unwrap()).unwrap();
        let mut allocation = pool.alloc(4096).unwrap();
        let mut retained = Vec::new();

        for _ in 0..3 {
            retained.push(BufferLease::new(pool.clone(), allocation));
            pool = pool.swap(core::iter::empty()).unwrap();
            let reused = pool.alloc(4096).unwrap();
            assert_eq!(reused.addr, allocation.addr);
            allocation = reused;
        }

        drop(retained);
        assert_eq!(pool.num_free(), 0);
        drop(BufferLease::new(pool.clone(), allocation));
        assert_eq!(pool.num_free(), 1);
    }

    #[test]
    fn swap_preserves_queue_reservations() {
        let pool = SlotPool::new(SlotLayout::new(0x1000, 4096, 2).unwrap()).unwrap();
        let reserved = pool.alloc(4096).unwrap();
        let captured = pool.alloc(4096).unwrap();
        let retained = BufferLease::new(pool.clone(), captured);

        let swapped = pool.swap(core::iter::once(reserved.addr)).unwrap();
        assert_eq!(swapped.num_free(), 1);
        assert_eq!(swapped.alloc(4096).unwrap().addr, captured.addr);

        drop(retained);
        assert_eq!(swapped.num_free(), 0);
        swapped.dealloc(reserved.addr).unwrap();
        swapped.dealloc(captured.addr).unwrap();
        assert_eq!(swapped.num_free(), 2);
        pool.dealloc(reserved.addr).unwrap();
        assert_eq!(pool.num_free(), 2);
    }

    #[test]
    fn segments_cursor_advances_across_segments() {
        let segments = Segments::new([
            Bytes::from_static(b"abc"),
            Bytes::from_static(b"def"),
            Bytes::from_static(b"ghi"),
        ]);
        let mut cursor = segments.as_buf();

        assert_eq!(cursor.remaining(), 9);
        assert_eq!(cursor.chunk(), b"abc");

        cursor.advance(2);
        assert_eq!(cursor.remaining(), 7);
        assert_eq!(cursor.chunk(), b"c");

        cursor.advance(1);
        assert_eq!(cursor.chunk(), b"def");

        cursor.advance(4);
        assert_eq!(cursor.chunk(), b"hi");

        cursor.advance(2);
        assert_eq!(cursor.remaining(), 0);
        assert_eq!(cursor.chunk(), b"");
    }

    #[test]
    fn segments_cursor_skips_empty_segments() {
        let segments = Segments::new([
            Bytes::new(),
            Bytes::from_static(b"ab"),
            Bytes::new(),
            Bytes::from_static(b"cd"),
            Bytes::new(),
        ]);
        let mut cursor = segments.as_buf();

        assert_eq!(cursor.remaining(), 4);
        assert_eq!(cursor.chunk(), b"ab");

        cursor.advance(2);
        assert_eq!(cursor.remaining(), 2);
        assert_eq!(cursor.chunk(), b"cd");

        cursor.advance(2);
        assert!(!cursor.has_remaining());
        assert_eq!(cursor.chunk(), b"");
    }

    #[test]
    fn segments_cursor_reads_split_header_without_collecting_all_segments() {
        let segments = Segments::new([
            Bytes::from_static(&[0x01, 0x02, 0x03]),
            Bytes::from_static(&[0x04, 0x05]),
            Bytes::from_static(&[0x06, 0x07, 0x08, 0xff]),
        ]);
        let mut cursor = segments.as_buf();
        let mut header = [0u8; 8];

        cursor.try_copy_to_slice(&mut header).unwrap();

        assert_eq!(header, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
        assert_eq!(cursor.remaining(), 1);
        assert_eq!(cursor.chunk(), &[0xff]);
    }

    #[test]
    fn segments_cursor_copy_to_bytes_collects_only_requested_prefix() {
        let segments = Segments::new([
            Bytes::from_static(b"hello"),
            Bytes::from_static(b" "),
            Bytes::from_static(b"world"),
        ]);
        let mut cursor = segments.as_buf();

        let prefix = cursor.copy_to_bytes(6);

        assert_eq!(prefix.as_ref(), b"hello ");
        assert_eq!(cursor.remaining(), 5);
        assert_eq!(cursor.chunk(), b"world");
    }

    #[test]
    fn segments_split_to_shares_boundary_segment() {
        let boundary = Bytes::from(vec![b'd', b'e', b'f']);
        let boundary_ptr = boundary.as_ptr();
        let mut segments = Segments::new([
            Bytes::from_static(b"abc"),
            boundary,
            Bytes::from_static(b"ghi"),
        ]);

        let prefix = segments.split_to(5).unwrap();

        assert_eq!(prefix.segment_count(), 2);
        assert_eq!(prefix.to_bytes().as_ref(), b"abcde");
        assert_eq!(prefix.as_slice()[1].as_ptr(), boundary_ptr);
        assert_eq!(segments.segment_count(), 2);
        assert_eq!(segments.to_bytes().as_ref(), b"fghi");
        assert_eq!(
            segments.as_slice()[0].as_ptr(),
            boundary_ptr.wrapping_add(2)
        );

        assert!(segments.split_to(5).is_none());
        assert_eq!(segments.to_bytes().as_ref(), b"fghi");
    }

    #[test]
    fn segments_split_to_preserves_empty_segments() {
        let mut segments = Segments::new([
            Bytes::new(),
            Bytes::from_static(b"ab"),
            Bytes::new(),
            Bytes::from_static(b"cd"),
            Bytes::new(),
        ]);
        let original = segments.as_slice().to_vec();

        assert!(segments.split_to(5).is_none());
        assert_eq!(segments.as_slice(), original);
        assert_eq!(segments.split_to(0).unwrap().segment_count(), 0);
        assert_eq!(segments.as_slice(), original);

        let first = segments.split_to(2).unwrap();
        assert_eq!(first.as_slice(), &[Bytes::new(), Bytes::from_static(b"ab")]);
        assert_eq!(segments.segment_count(), 3);

        let second = segments.split_to(2).unwrap();
        assert_eq!(
            second.as_slice(),
            &[Bytes::new(), Bytes::from_static(b"cd")]
        );
        assert!(segments.is_empty());
        assert_eq!(segments.segment_count(), 1);
        assert_eq!(segments.split_to(0).unwrap().segment_count(), 0);
        assert!(segments.split_to(1).is_none());
        assert_eq!(segments.as_slice(), &[Bytes::new()]);
    }

    #[test]
    fn segments_split_to_reuses_full_segment_list() {
        let mut segments = Segments::new((0..8).map(|byte| Bytes::from(vec![byte])));
        let storage = segments.chunks.as_ptr();

        let prefix = segments.split_to(8).unwrap();

        assert_eq!(prefix.chunks.as_ptr(), storage);
        assert_eq!(prefix.segment_count(), 8);
        assert_eq!(prefix.into_bytes().as_ref(), &[0, 1, 2, 3, 4, 5, 6, 7]);
        assert!(segments.is_empty());
        assert_eq!(segments.segment_count(), 0);
    }

    #[test]
    fn segments_split_to_keeps_small_suffix_inline() {
        #[cfg(miri)]
        let count = 64;
        #[cfg(not(miri))]
        let count = 8192;

        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"x")));
        drop(segments.split_to(count - 1).unwrap());

        let suffix = segments.split_to(1).unwrap();

        assert!(!suffix.chunks.spilled());
        assert_eq!(suffix.head, 0);
        assert_eq!(suffix.into_bytes().as_ref(), b"x");
        assert!(segments.is_empty());
        assert_eq!(segments.segment_count(), 0);
    }

    #[test]
    fn segments_split_to_keeps_large_remainder_in_place() {
        #[cfg(miri)]
        let count = 64;
        #[cfg(not(miri))]
        let count = 8192;
        let half = count / 2;

        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"x")));
        let storage = segments.chunks.as_ptr();

        let prefix = segments.split_to(half).unwrap();

        assert_eq!(prefix.segment_count(), half);
        assert!(prefix.iter().all(|segment| segment.as_ref() == b"x"));
        assert_eq!(segments.segment_count(), half);
        assert_eq!(segments.as_slice().as_ptr(), storage.wrapping_add(half));
        assert!(segments.chunks[..segments.head].iter().all(Bytes::is_empty));
    }

    #[test]
    fn segments_split_to_repeatedly_consumes_fragments() {
        #[cfg(miri)]
        let count = 64;
        #[cfg(not(miri))]
        let count = 4096;

        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"ab")));

        for _ in 0..count {
            assert_eq!(segments.split_to(1).unwrap().into_bytes().as_ref(), b"a");
            assert_eq!(segments.split_to(1).unwrap().into_bytes().as_ref(), b"b");
        }

        assert!(segments.is_empty());
        assert_eq!(segments.segment_count(), 0);
        assert!(segments.split_to(1).is_none());
    }

    #[test]
    fn segments_split_to_releases_owners_independently() {
        let first: Arc<[u8]> = Arc::from(&b"abc"[..]);
        let boundary: Arc<[u8]> = Arc::from(&b"def"[..]);
        let last: Arc<[u8]> = Arc::from(&b"ghi"[..]);
        let first_owner = Arc::downgrade(&first);
        let boundary_owner = Arc::downgrade(&boundary);
        let last_owner = Arc::downgrade(&last);
        let mut segments = Segments::new([
            Bytes::from_owner(first),
            Bytes::from_owner(boundary),
            Bytes::from_owner(last),
        ]);

        let prefix = segments.split_to(4).unwrap();
        assert_eq!(prefix.to_bytes().as_ref(), b"abcd");
        assert!(first_owner.upgrade().is_some());

        drop(prefix);

        assert!(first_owner.upgrade().is_none());
        assert!(boundary_owner.upgrade().is_some());
        assert!(last_owner.upgrade().is_some());
        assert_eq!(segments.to_bytes().as_ref(), b"efghi");

        drop(segments);

        assert!(boundary_owner.upgrade().is_none());
        assert!(last_owner.upgrade().is_none());
    }

    #[test]
    fn segments_views_ignore_consumed_segments() {
        let mut segments = Segments::new([
            Bytes::from_static(b"ab"),
            Bytes::from_static(b"cd"),
            Bytes::from_static(b"ef"),
        ]);
        drop(segments.split_to(3).unwrap());
        let expected = [Bytes::from_static(b"d"), Bytes::from_static(b"ef")];

        assert_eq!(segments.len(), 3);
        assert!(!segments.is_empty());
        assert_eq!(segments.segment_count(), 2);
        assert_eq!(segments.as_slice(), &expected);
        assert!(segments.iter().eq(expected.iter()));
        assert_eq!(segments.to_bytes().as_ref(), b"def");

        let mut cursor = segments.as_buf();
        let mut bytes = [0; 3];
        cursor.copy_to_slice(&mut bytes);
        assert_eq!(&bytes, b"def");
        assert_eq!(cursor.remaining(), 0);

        let cloned = segments.clone();
        assert_eq!(cloned.head, 0);
        assert_eq!(cloned.chunks.len(), 2);
        assert_eq!(cloned.into_bytes().as_ref(), b"def");
        assert_eq!(segments.into_chunks(), expected);
    }

    #[test]
    fn segments_into_bytes_reuses_single_segment() {
        let segment = Bytes::from(vec![1, 2, 3, 4]);
        let ptr = segment.as_ptr();

        let collected = Segments::single(segment).into_bytes();

        assert_eq!(collected.as_ptr(), ptr);
        assert_eq!(collected.as_ref(), &[1, 2, 3, 4]);
    }

    #[test]
    fn segments_into_bytes_reuses_remaining_segment() {
        let segment = Bytes::from(vec![1, 2, 3, 4]);
        let ptr = segment.as_ptr();
        let mut segments = Segments::new([Bytes::from_static(b"prefix"), segment]);
        drop(segments.split_to(6).unwrap());

        assert_eq!(segments.to_bytes().as_ptr(), ptr);

        let collected = segments.into_bytes();

        assert_eq!(collected.as_ptr(), ptr);
        assert_eq!(collected.as_ref(), &[1, 2, 3, 4]);
    }

    #[test]
    fn segments_into_chunks_preserves_segment_storage() {
        let first = Bytes::from(vec![1, 2]);
        let second = Bytes::from(vec![3, 4]);
        let first_ptr = first.as_ptr();
        let second_ptr = second.as_ptr();

        let chunks = Segments::new([first, second]).into_chunks();

        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].as_ptr(), first_ptr);
        assert_eq!(chunks[1].as_ptr(), second_ptr);
    }

    #[test]
    fn segments_append_keeps_small_payloads_inline() {
        let chunks = [
            Bytes::from(vec![1]),
            Bytes::from(vec![2]),
            Bytes::from(vec![3]),
            Bytes::from(vec![4]),
        ];
        let ptrs = chunks.each_ref().map(|chunk| chunk.as_ptr());
        let [first, second, third, fourth] = chunks;
        let mut segments = Segments::new([first, second]);

        // Four segments fit in the inline storage.
        segments.append(Segments::new([third, fourth]));
        assert!(!segments.chunks.spilled());
        assert_eq!(segments.len(), 4);
        assert_eq!(segments.segment_count(), 4);
        assert_eq!(segments.to_bytes().as_ref(), &[1, 2, 3, 4]);
        for (segment, ptr) in segments.iter().zip(ptrs) {
            assert_eq!(segment.as_ptr(), ptr);
        }
    }

    #[test]
    fn segments_append_spills_in_order() {
        let mut segments = Segments::new((0..4).map(|byte| Bytes::from(vec![byte])));
        let last = Bytes::from(vec![4]);
        let ptr = last.as_ptr();
        assert!(!segments.chunks.spilled());

        // The fifth segment requires heap storage for the segment list.
        segments.append(Segments::single(last));
        assert!(segments.chunks.spilled());
        assert_eq!(segments.len(), 5);
        assert_eq!(segments.segment_count(), 5);
        assert_eq!(segments.to_bytes().as_ref(), &[0, 1, 2, 3, 4]);
        assert_eq!(segments.as_slice()[4].as_ptr(), ptr);
    }

    #[test]
    fn segments_append_to_empty() {
        let bytes = Bytes::from(vec![1, 2, 3]);
        let ptr = bytes.as_ptr();
        let mut segments = Segments::default();

        segments.append(Segments::single(bytes));

        assert_eq!(segments.len(), 3);
        assert_eq!(segments.segment_count(), 1);
        assert_eq!(segments.as_slice()[0].as_ptr(), ptr);
        assert_eq!(segments.as_slice()[0].as_ref(), &[1, 2, 3]);
    }

    #[test]
    fn segments_append_empty_payload() {
        let bytes = Bytes::from(vec![1, 2, 3]);
        let ptr = bytes.as_ptr();
        let mut segments = Segments::single(bytes);

        segments.append(Segments::default());

        assert_eq!(segments.len(), 3);
        assert_eq!(segments.segment_count(), 1);
        assert_eq!(segments.as_slice()[0].as_ptr(), ptr);
        assert_eq!(segments.as_slice()[0].as_ref(), &[1, 2, 3]);
    }

    #[test]
    fn segments_append_compacts_consumed_slots() {
        let mut segments = Segments::new([
            Bytes::from_static(b"a"),
            Bytes::from_static(b"b"),
            Bytes::from_static(b"c"),
            Bytes::from_static(b"d"),
        ]);

        let mut other = Segments::new([
            Bytes::from_static(b"x"),
            Bytes::from_static(b"y"),
            Bytes::from_static(b"z"),
        ]);

        drop(segments.split_to(2).unwrap());
        drop(other.split_to(1).unwrap());

        segments.append(other);

        assert!(!segments.chunks.spilled());
        assert_eq!(segments.head, 0);
        assert_eq!(segments.segment_count(), 4);
        assert_eq!(segments.to_bytes().as_ref(), b"cdyz");
    }

    #[test]
    fn segments_append_preserves_owner_lifetime() {
        let storage: Arc<[u8]> = Arc::from(&b"data"[..]);
        let owner = Arc::downgrade(&storage);
        let mut segments = Segments::single(Bytes::from_static(b"header"));

        // The appended segment becomes the only owner of this storage.
        segments.append(Segments::single(Bytes::from_owner(storage)));
        assert_eq!(owner.strong_count(), 1);
        assert_eq!(segments.to_bytes().as_ref(), b"headerdata");

        drop(segments);
        assert!(owner.upgrade().is_none());
    }
}