Skip to main content

hyperlight_common/virtq/
buffer.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 The Hyperlight Authors.
3
4//! Owned and segmented virtqueue buffer representations.
5
6use alloc::vec::Vec;
7
8use bytes::{Buf, Bytes};
9use smallvec::{SmallVec, smallvec};
10
11use super::{Allocation, SlotPool};
12
13/// Ordered byte segments that make up one virtqueue payload.
14///
15/// This is the high-level counterpart to the descriptor-oriented
16/// [`BufferChain`](super::BufferChain).
17#[derive(Debug, Default)]
18pub struct Segments {
19    chunks: SmallVec<[Bytes; 4]>,
20    /// Earlier slots contain empty `Bytes` and retain no owners.
21    head: usize,
22}
23
24impl Segments {
25    /// Build a segmented payload from ordered byte segments.
26    pub fn new(segments: impl IntoIterator<Item = Bytes>) -> Self {
27        Self::from_smallvec(segments.into_iter().collect())
28    }
29
30    /// Build a single-segment payload.
31    pub fn single(segment: Bytes) -> Self {
32        Self::from_smallvec(smallvec![segment])
33    }
34
35    /// Reuse an existing segment list.
36    pub(crate) fn from_smallvec(segments: SmallVec<[Bytes; 4]>) -> Self {
37        Self {
38            chunks: segments,
39            head: 0,
40        }
41    }
42
43    /// Total payload length across all segments.
44    pub fn len(&self) -> usize {
45        self.iter().map(Bytes::len).sum()
46    }
47
48    /// Whether the payload contains zero bytes.
49    pub fn is_empty(&self) -> bool {
50        self.iter().all(Bytes::is_empty)
51    }
52
53    /// Number of byte segments.
54    pub fn segment_count(&self) -> usize {
55        self.chunks.len() - self.head
56    }
57
58    /// Borrow all segments.
59    pub fn as_slice(&self) -> &[Bytes] {
60        &self.chunks[self.head..]
61    }
62
63    /// Iterate over segments.
64    pub fn iter(&self) -> impl Iterator<Item = &Bytes> {
65        self.as_slice().iter()
66    }
67
68    /// Append another payload without copying its bytes.
69    ///
70    /// Compacts consumed slots before extending the segment list.
71    pub fn append(&mut self, other: Self) {
72        if self.head != 0 {
73            drop(self.chunks.drain(..self.head));
74            self.head = 0;
75        }
76
77        self.chunks
78            .extend(other.chunks.into_iter().skip(other.head));
79    }
80
81    /// Split off an owned byte prefix without copying payload data.
82    ///
83    /// Scans only the prefix, then transfers its segments without shifting the
84    /// remainder. A boundary segment shares its [`Bytes`] owner.
85    ///
86    /// Returns `None` and leaves `self` unchanged when `len` exceeds the
87    /// remaining payload length.
88    pub fn split_to(&mut self, len: usize) -> Option<Self> {
89        if len == 0 {
90            return Some(Self::default());
91        }
92
93        let mut end = self.head;
94        let mut remaining = len;
95
96        while remaining != 0 {
97            let segment = self.chunks.get(end)?;
98
99            if segment.len() > remaining {
100                break;
101            }
102
103            remaining -= segment.len();
104            end += 1;
105        }
106
107        // Small suffixes must not inherit storage for consumed segments.
108        if self.head == 0 && end == self.chunks.len() {
109            return Some(core::mem::take(self));
110        }
111
112        let mut prefix =
113            SmallVec::<[Bytes; 4]>::with_capacity(end - self.head + usize::from(remaining != 0));
114
115        for segment in &mut self.chunks[self.head..end] {
116            prefix.push(core::mem::take(segment));
117        }
118
119        self.head = end;
120
121        if remaining != 0 {
122            prefix.push(self.chunks[end].split_to(remaining));
123        }
124
125        Some(Self::from_smallvec(prefix))
126    }
127
128    /// Borrow this payload as a [`Buf`] cursor.
129    pub fn as_buf(&self) -> SegmentsBuf<'_> {
130        SegmentsBuf::new(self.as_slice(), self.len())
131    }
132
133    /// Return this payload as contiguous bytes.
134    ///
135    /// This is O(1) for zero or one segment, and allocates/copies for multiple
136    /// segments.
137    pub fn to_bytes(&self) -> Bytes {
138        match self.as_slice() {
139            [] => Bytes::new(),
140            [segment] => segment.clone(),
141            _ => self.collect(self.as_slice(), self.len()),
142        }
143    }
144
145    /// Consume this payload and return contiguous bytes.
146    ///
147    /// Reuses a single segment's storage, and allocates/copies for multiple segments.
148    pub fn into_bytes(mut self) -> Bytes {
149        match self.segment_count() {
150            0 => Bytes::new(),
151            1 => self.chunks.pop().unwrap_or_default(),
152            _ => self.collect(self.as_slice(), self.len()),
153        }
154    }
155
156    /// Consume this payload without flattening its segments.
157    pub fn into_chunks(mut self) -> Vec<Bytes> {
158        if self.head != 0 {
159            drop(self.chunks.drain(..self.head));
160        }
161
162        self.chunks.into_vec()
163    }
164
165    fn collect(&self, sgs: &[Bytes], len: usize) -> Bytes {
166        let mut out = Vec::with_capacity(len);
167        for seg in sgs {
168            out.extend_from_slice(seg);
169        }
170        Bytes::from(out)
171    }
172}
173
174impl Clone for Segments {
175    /// Clone only unconsumed segment handles.
176    fn clone(&self) -> Self {
177        Self::new(self.iter().cloned())
178    }
179}
180
181/// Borrowed [`Buf`] cursor over [`Segments`].
182///
183/// Advancing the cursor does not mutate the underlying [`Segments`].
184#[derive(Debug, Clone)]
185pub struct SegmentsBuf<'a> {
186    segments: &'a [Bytes],
187    index: usize,
188    offset: usize,
189    remaining: usize,
190}
191
192impl<'a> SegmentsBuf<'a> {
193    fn new(segments: &'a [Bytes], len: usize) -> Self {
194        let mut this = Self {
195            segments,
196            index: 0,
197            offset: 0,
198            remaining: len,
199        };
200
201        this.skip_empty_segments();
202        this
203    }
204
205    fn skip_empty_segments(&mut self) {
206        while self.index < self.segments.len() && self.offset >= self.segments[self.index].len() {
207            self.index += 1;
208            self.offset = 0;
209        }
210    }
211}
212
213impl Buf for SegmentsBuf<'_> {
214    fn remaining(&self) -> usize {
215        self.remaining
216    }
217
218    fn chunk(&self) -> &[u8] {
219        if self.remaining == 0 {
220            return &[];
221        }
222
223        let segment = self.segments[self.index].as_ref();
224        &segment[self.offset..]
225    }
226
227    fn advance(&mut self, cnt: usize) {
228        assert!(cnt <= self.remaining, "cannot advance past remaining bytes");
229
230        self.remaining -= cnt;
231        let mut cnt = cnt;
232
233        while cnt > 0 {
234            let seg_rem = self.segments[self.index].len() - self.offset;
235            let n = seg_rem.min(cnt);
236            self.offset += n;
237            cnt -= n;
238            self.skip_empty_segments();
239        }
240
241        if self.remaining == 0 {
242            self.index = self.segments.len();
243            self.offset = 0;
244        }
245    }
246}
247
248/// A buffer allocation returned on drop while its pool generation is current.
249pub struct BufferLease {
250    /// The pool that allocated the buffer.
251    pool: SlotPool,
252    /// The pool generation at the time of allocation.
253    generation: u64,
254    /// The buffer's start address and full allocation capacity.
255    allocation: Allocation,
256}
257
258impl BufferLease {
259    /// Create a new buffer lease from a pool and allocation.
260    pub fn new(pool: SlotPool, allocation: Allocation) -> Self {
261        let generation = pool.generation();
262
263        Self {
264            pool,
265            allocation,
266            generation,
267        }
268    }
269
270    /// The buffer's start address and full allocation capacity.
271    pub fn allocation(&self) -> Allocation {
272        self.allocation
273    }
274}
275
276impl Drop for BufferLease {
277    fn drop(&mut self) {
278        if self.generation != self.pool.generation() {
279            return;
280        }
281
282        if let Err(error) = self.pool.dealloc(self.allocation.addr) {
283            log::error!("Failed to release a virtqueue buffer: {error}");
284            debug_assert!(false, "BufferLease deallocation failed: {error}");
285        }
286    }
287}
288
289#[cfg(test)]
290mod tests {
291    use alloc::sync::Arc;
292
293    use bytes::Buf;
294
295    use super::*;
296    use crate::virtq::{SlotLayout, SlotPool};
297
298    #[test]
299    fn lease_returns_slot_on_drop() {
300        let layout = SlotLayout::new(0, 4, 1).unwrap();
301        let pool = SlotPool::new(layout).unwrap();
302        let allocation = pool.alloc(4).unwrap();
303        let lease = BufferLease::new(pool.clone(), allocation);
304
305        assert_eq!(lease.allocation().addr, allocation.addr);
306        assert_eq!(lease.allocation().len, 4);
307        assert_eq!(pool.num_free(), 0);
308
309        drop(lease);
310        assert_eq!(pool.num_free(), 1);
311
312        let reused = pool.alloc(4).unwrap();
313        assert_eq!(reused.addr, allocation.addr);
314        pool.dealloc(reused.addr).unwrap();
315    }
316
317    #[test]
318    fn retired_lease_does_not_release_a_reused_slot() {
319        let pool = SlotPool::new(SlotLayout::new(0x1000, 4096, 1).unwrap()).unwrap();
320        let allocation = pool.alloc(4096).unwrap();
321        let retained = BufferLease::new(pool.clone(), allocation);
322
323        // SAFETY: This test has no views or queue reservations.
324        unsafe { pool.retire_generation(core::iter::empty()) }.unwrap();
325        let reused = pool.alloc(4096).unwrap();
326        assert_eq!(reused.addr, allocation.addr);
327
328        drop(retained);
329        assert_eq!(pool.num_free(), 0);
330        drop(BufferLease::new(pool.clone(), reused));
331        assert_eq!(pool.num_free(), 1);
332    }
333
334    #[test]
335    fn generation_recovery_preserves_queue_reservations() {
336        let pool = SlotPool::new(SlotLayout::new(0x1000, 4096, 2).unwrap()).unwrap();
337        let reserved = pool.alloc(4096).unwrap();
338        let captured = pool.alloc(4096).unwrap();
339        let retained = BufferLease::new(pool.clone(), captured);
340
341        // SAFETY: No view exists. The iterator names the only queue reservation.
342        unsafe { pool.retire_generation(core::iter::once(reserved.addr)) }.unwrap();
343        assert_eq!(pool.num_free(), 1);
344        assert_eq!(pool.alloc(4096).unwrap().addr, captured.addr);
345
346        drop(retained);
347        assert_eq!(pool.num_free(), 0);
348        pool.dealloc(reserved.addr).unwrap();
349        pool.dealloc(captured.addr).unwrap();
350        assert_eq!(pool.num_free(), 2);
351    }
352
353    #[test]
354    fn segments_cursor_advances_across_segments() {
355        let segments = Segments::new([
356            Bytes::from_static(b"abc"),
357            Bytes::from_static(b"def"),
358            Bytes::from_static(b"ghi"),
359        ]);
360        let mut cursor = segments.as_buf();
361
362        assert_eq!(cursor.remaining(), 9);
363        assert_eq!(cursor.chunk(), b"abc");
364
365        cursor.advance(2);
366        assert_eq!(cursor.remaining(), 7);
367        assert_eq!(cursor.chunk(), b"c");
368
369        cursor.advance(1);
370        assert_eq!(cursor.chunk(), b"def");
371
372        cursor.advance(4);
373        assert_eq!(cursor.chunk(), b"hi");
374
375        cursor.advance(2);
376        assert_eq!(cursor.remaining(), 0);
377        assert_eq!(cursor.chunk(), b"");
378    }
379
380    #[test]
381    fn segments_cursor_skips_empty_segments() {
382        let segments = Segments::new([
383            Bytes::new(),
384            Bytes::from_static(b"ab"),
385            Bytes::new(),
386            Bytes::from_static(b"cd"),
387            Bytes::new(),
388        ]);
389        let mut cursor = segments.as_buf();
390
391        assert_eq!(cursor.remaining(), 4);
392        assert_eq!(cursor.chunk(), b"ab");
393
394        cursor.advance(2);
395        assert_eq!(cursor.remaining(), 2);
396        assert_eq!(cursor.chunk(), b"cd");
397
398        cursor.advance(2);
399        assert!(!cursor.has_remaining());
400        assert_eq!(cursor.chunk(), b"");
401    }
402
403    #[test]
404    fn segments_cursor_reads_split_header_without_collecting_all_segments() {
405        let segments = Segments::new([
406            Bytes::from_static(&[0x01, 0x02, 0x03]),
407            Bytes::from_static(&[0x04, 0x05]),
408            Bytes::from_static(&[0x06, 0x07, 0x08, 0xff]),
409        ]);
410        let mut cursor = segments.as_buf();
411        let mut header = [0u8; 8];
412
413        cursor.try_copy_to_slice(&mut header).unwrap();
414
415        assert_eq!(header, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
416        assert_eq!(cursor.remaining(), 1);
417        assert_eq!(cursor.chunk(), &[0xff]);
418    }
419
420    #[test]
421    fn segments_cursor_copy_to_bytes_collects_only_requested_prefix() {
422        let segments = Segments::new([
423            Bytes::from_static(b"hello"),
424            Bytes::from_static(b" "),
425            Bytes::from_static(b"world"),
426        ]);
427        let mut cursor = segments.as_buf();
428
429        let prefix = cursor.copy_to_bytes(6);
430
431        assert_eq!(prefix.as_ref(), b"hello ");
432        assert_eq!(cursor.remaining(), 5);
433        assert_eq!(cursor.chunk(), b"world");
434    }
435
436    #[test]
437    fn segments_split_to_shares_boundary_segment() {
438        let boundary = Bytes::from(vec![b'd', b'e', b'f']);
439        let boundary_ptr = boundary.as_ptr();
440        let mut segments = Segments::new([
441            Bytes::from_static(b"abc"),
442            boundary,
443            Bytes::from_static(b"ghi"),
444        ]);
445
446        let prefix = segments.split_to(5).unwrap();
447
448        assert_eq!(prefix.segment_count(), 2);
449        assert_eq!(prefix.to_bytes().as_ref(), b"abcde");
450        assert_eq!(prefix.as_slice()[1].as_ptr(), boundary_ptr);
451        assert_eq!(segments.segment_count(), 2);
452        assert_eq!(segments.to_bytes().as_ref(), b"fghi");
453        assert_eq!(
454            segments.as_slice()[0].as_ptr(),
455            boundary_ptr.wrapping_add(2)
456        );
457
458        assert!(segments.split_to(5).is_none());
459        assert_eq!(segments.to_bytes().as_ref(), b"fghi");
460    }
461
462    #[test]
463    fn segments_split_to_preserves_empty_segments() {
464        let mut segments = Segments::new([
465            Bytes::new(),
466            Bytes::from_static(b"ab"),
467            Bytes::new(),
468            Bytes::from_static(b"cd"),
469            Bytes::new(),
470        ]);
471        let original = segments.as_slice().to_vec();
472
473        assert!(segments.split_to(5).is_none());
474        assert_eq!(segments.as_slice(), original);
475        assert_eq!(segments.split_to(0).unwrap().segment_count(), 0);
476        assert_eq!(segments.as_slice(), original);
477
478        let first = segments.split_to(2).unwrap();
479        assert_eq!(first.as_slice(), &[Bytes::new(), Bytes::from_static(b"ab")]);
480        assert_eq!(segments.segment_count(), 3);
481
482        let second = segments.split_to(2).unwrap();
483        assert_eq!(
484            second.as_slice(),
485            &[Bytes::new(), Bytes::from_static(b"cd")]
486        );
487        assert!(segments.is_empty());
488        assert_eq!(segments.segment_count(), 1);
489        assert_eq!(segments.split_to(0).unwrap().segment_count(), 0);
490        assert!(segments.split_to(1).is_none());
491        assert_eq!(segments.as_slice(), &[Bytes::new()]);
492    }
493
494    #[test]
495    fn segments_split_to_reuses_full_segment_list() {
496        let mut segments = Segments::new((0..8).map(|byte| Bytes::from(vec![byte])));
497        let storage = segments.chunks.as_ptr();
498
499        let prefix = segments.split_to(8).unwrap();
500
501        assert_eq!(prefix.chunks.as_ptr(), storage);
502        assert_eq!(prefix.segment_count(), 8);
503        assert_eq!(prefix.into_bytes().as_ref(), &[0, 1, 2, 3, 4, 5, 6, 7]);
504        assert!(segments.is_empty());
505        assert_eq!(segments.segment_count(), 0);
506    }
507
508    #[test]
509    fn segments_split_to_keeps_small_suffix_inline() {
510        #[cfg(miri)]
511        let count = 64;
512        #[cfg(not(miri))]
513        let count = 8192;
514
515        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"x")));
516        drop(segments.split_to(count - 1).unwrap());
517
518        let suffix = segments.split_to(1).unwrap();
519
520        assert!(!suffix.chunks.spilled());
521        assert_eq!(suffix.head, 0);
522        assert_eq!(suffix.into_bytes().as_ref(), b"x");
523        assert!(segments.is_empty());
524        assert_eq!(segments.segment_count(), 0);
525    }
526
527    #[test]
528    fn segments_split_to_keeps_large_remainder_in_place() {
529        #[cfg(miri)]
530        let count = 64;
531        #[cfg(not(miri))]
532        let count = 8192;
533        let half = count / 2;
534
535        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"x")));
536        let storage = segments.chunks.as_ptr();
537
538        let prefix = segments.split_to(half).unwrap();
539
540        assert_eq!(prefix.segment_count(), half);
541        assert!(prefix.iter().all(|segment| segment.as_ref() == b"x"));
542        assert_eq!(segments.segment_count(), half);
543        assert_eq!(segments.as_slice().as_ptr(), storage.wrapping_add(half));
544        assert!(segments.chunks[..segments.head].iter().all(Bytes::is_empty));
545    }
546
547    #[test]
548    fn segments_split_to_repeatedly_consumes_fragments() {
549        #[cfg(miri)]
550        let count = 64;
551        #[cfg(not(miri))]
552        let count = 4096;
553
554        let mut segments = Segments::new((0..count).map(|_| Bytes::from_static(b"ab")));
555
556        for _ in 0..count {
557            assert_eq!(segments.split_to(1).unwrap().into_bytes().as_ref(), b"a");
558            assert_eq!(segments.split_to(1).unwrap().into_bytes().as_ref(), b"b");
559        }
560
561        assert!(segments.is_empty());
562        assert_eq!(segments.segment_count(), 0);
563        assert!(segments.split_to(1).is_none());
564    }
565
566    #[test]
567    fn segments_split_to_releases_owners_independently() {
568        let first: Arc<[u8]> = Arc::from(&b"abc"[..]);
569        let boundary: Arc<[u8]> = Arc::from(&b"def"[..]);
570        let last: Arc<[u8]> = Arc::from(&b"ghi"[..]);
571        let first_owner = Arc::downgrade(&first);
572        let boundary_owner = Arc::downgrade(&boundary);
573        let last_owner = Arc::downgrade(&last);
574        let mut segments = Segments::new([
575            Bytes::from_owner(first),
576            Bytes::from_owner(boundary),
577            Bytes::from_owner(last),
578        ]);
579
580        let prefix = segments.split_to(4).unwrap();
581        assert_eq!(prefix.to_bytes().as_ref(), b"abcd");
582        assert!(first_owner.upgrade().is_some());
583
584        drop(prefix);
585
586        assert!(first_owner.upgrade().is_none());
587        assert!(boundary_owner.upgrade().is_some());
588        assert!(last_owner.upgrade().is_some());
589        assert_eq!(segments.to_bytes().as_ref(), b"efghi");
590
591        drop(segments);
592
593        assert!(boundary_owner.upgrade().is_none());
594        assert!(last_owner.upgrade().is_none());
595    }
596
597    #[test]
598    fn segments_views_ignore_consumed_segments() {
599        let mut segments = Segments::new([
600            Bytes::from_static(b"ab"),
601            Bytes::from_static(b"cd"),
602            Bytes::from_static(b"ef"),
603        ]);
604        drop(segments.split_to(3).unwrap());
605        let expected = [Bytes::from_static(b"d"), Bytes::from_static(b"ef")];
606
607        assert_eq!(segments.len(), 3);
608        assert!(!segments.is_empty());
609        assert_eq!(segments.segment_count(), 2);
610        assert_eq!(segments.as_slice(), &expected);
611        assert!(segments.iter().eq(expected.iter()));
612        assert_eq!(segments.to_bytes().as_ref(), b"def");
613
614        let mut cursor = segments.as_buf();
615        let mut bytes = [0; 3];
616        cursor.copy_to_slice(&mut bytes);
617        assert_eq!(&bytes, b"def");
618        assert_eq!(cursor.remaining(), 0);
619
620        let cloned = segments.clone();
621        assert_eq!(cloned.head, 0);
622        assert_eq!(cloned.chunks.len(), 2);
623        assert_eq!(cloned.into_bytes().as_ref(), b"def");
624        assert_eq!(segments.into_chunks(), expected);
625    }
626
627    #[test]
628    fn segments_into_bytes_reuses_single_segment() {
629        let segment = Bytes::from(vec![1, 2, 3, 4]);
630        let ptr = segment.as_ptr();
631
632        let collected = Segments::single(segment).into_bytes();
633
634        assert_eq!(collected.as_ptr(), ptr);
635        assert_eq!(collected.as_ref(), &[1, 2, 3, 4]);
636    }
637
638    #[test]
639    fn segments_into_bytes_reuses_remaining_segment() {
640        let segment = Bytes::from(vec![1, 2, 3, 4]);
641        let ptr = segment.as_ptr();
642        let mut segments = Segments::new([Bytes::from_static(b"prefix"), segment]);
643        drop(segments.split_to(6).unwrap());
644
645        assert_eq!(segments.to_bytes().as_ptr(), ptr);
646
647        let collected = segments.into_bytes();
648
649        assert_eq!(collected.as_ptr(), ptr);
650        assert_eq!(collected.as_ref(), &[1, 2, 3, 4]);
651    }
652
653    #[test]
654    fn segments_into_chunks_preserves_segment_storage() {
655        let first = Bytes::from(vec![1, 2]);
656        let second = Bytes::from(vec![3, 4]);
657        let first_ptr = first.as_ptr();
658        let second_ptr = second.as_ptr();
659
660        let chunks = Segments::new([first, second]).into_chunks();
661
662        assert_eq!(chunks.len(), 2);
663        assert_eq!(chunks[0].as_ptr(), first_ptr);
664        assert_eq!(chunks[1].as_ptr(), second_ptr);
665    }
666
667    #[test]
668    fn segments_append_keeps_small_payloads_inline() {
669        let chunks = [
670            Bytes::from(vec![1]),
671            Bytes::from(vec![2]),
672            Bytes::from(vec![3]),
673            Bytes::from(vec![4]),
674        ];
675        let ptrs = chunks.each_ref().map(|chunk| chunk.as_ptr());
676        let [first, second, third, fourth] = chunks;
677        let mut segments = Segments::new([first, second]);
678
679        // Four segments fit in the inline storage.
680        segments.append(Segments::new([third, fourth]));
681        assert!(!segments.chunks.spilled());
682        assert_eq!(segments.len(), 4);
683        assert_eq!(segments.segment_count(), 4);
684        assert_eq!(segments.to_bytes().as_ref(), &[1, 2, 3, 4]);
685        for (segment, ptr) in segments.iter().zip(ptrs) {
686            assert_eq!(segment.as_ptr(), ptr);
687        }
688    }
689
690    #[test]
691    fn segments_append_spills_in_order() {
692        let mut segments = Segments::new((0..4).map(|byte| Bytes::from(vec![byte])));
693        let last = Bytes::from(vec![4]);
694        let ptr = last.as_ptr();
695        assert!(!segments.chunks.spilled());
696
697        // The fifth segment requires heap storage for the segment list.
698        segments.append(Segments::single(last));
699        assert!(segments.chunks.spilled());
700        assert_eq!(segments.len(), 5);
701        assert_eq!(segments.segment_count(), 5);
702        assert_eq!(segments.to_bytes().as_ref(), &[0, 1, 2, 3, 4]);
703        assert_eq!(segments.as_slice()[4].as_ptr(), ptr);
704    }
705
706    #[test]
707    fn segments_append_to_empty() {
708        let bytes = Bytes::from(vec![1, 2, 3]);
709        let ptr = bytes.as_ptr();
710        let mut segments = Segments::default();
711
712        segments.append(Segments::single(bytes));
713
714        assert_eq!(segments.len(), 3);
715        assert_eq!(segments.segment_count(), 1);
716        assert_eq!(segments.as_slice()[0].as_ptr(), ptr);
717        assert_eq!(segments.as_slice()[0].as_ref(), &[1, 2, 3]);
718    }
719
720    #[test]
721    fn segments_append_empty_payload() {
722        let bytes = Bytes::from(vec![1, 2, 3]);
723        let ptr = bytes.as_ptr();
724        let mut segments = Segments::single(bytes);
725
726        segments.append(Segments::default());
727
728        assert_eq!(segments.len(), 3);
729        assert_eq!(segments.segment_count(), 1);
730        assert_eq!(segments.as_slice()[0].as_ptr(), ptr);
731        assert_eq!(segments.as_slice()[0].as_ref(), &[1, 2, 3]);
732    }
733
734    #[test]
735    fn segments_append_compacts_consumed_slots() {
736        let mut segments = Segments::new([
737            Bytes::from_static(b"a"),
738            Bytes::from_static(b"b"),
739            Bytes::from_static(b"c"),
740            Bytes::from_static(b"d"),
741        ]);
742
743        let mut other = Segments::new([
744            Bytes::from_static(b"x"),
745            Bytes::from_static(b"y"),
746            Bytes::from_static(b"z"),
747        ]);
748
749        drop(segments.split_to(2).unwrap());
750        drop(other.split_to(1).unwrap());
751
752        segments.append(other);
753
754        assert!(!segments.chunks.spilled());
755        assert_eq!(segments.head, 0);
756        assert_eq!(segments.segment_count(), 4);
757        assert_eq!(segments.to_bytes().as_ref(), b"cdyz");
758    }
759
760    #[test]
761    fn segments_append_preserves_owner_lifetime() {
762        let storage: Arc<[u8]> = Arc::from(&b"data"[..]);
763        let owner = Arc::downgrade(&storage);
764        let mut segments = Segments::single(Bytes::from_static(b"header"));
765
766        // The appended segment becomes the only owner of this storage.
767        segments.append(Segments::single(Bytes::from_owner(storage)));
768        assert_eq!(owner.strong_count(), 1);
769        assert_eq!(segments.to_bytes().as_ref(), b"headerdata");
770
771        drop(segments);
772        assert!(owner.upgrade().is_none());
773    }
774}