1use core::mem::MaybeUninit;
44
45use bytes::{Buf, BufMut, Bytes, BytesMut};
46
47use crate::alloc::vec::Vec;
48
49pub trait EncodeSink {
89 const IS_SEGMENTED: bool = false;
99
100 fn put_u8(&mut self, value: u8);
102
103 fn put_slice(&mut self, src: &[u8]);
105
106 fn put_u32_le(&mut self, value: u32);
108
109 fn put_u64_le(&mut self, value: u64);
111
112 #[inline]
114 fn put_i32_le(&mut self, value: i32) {
115 #[allow(clippy::cast_sign_loss)]
117 self.put_u32_le(value as u32);
118 }
119
120 #[inline]
122 fn put_i64_le(&mut self, value: i64) {
123 #[allow(clippy::cast_sign_loss)]
125 self.put_u64_le(value as u64);
126 }
127
128 #[inline]
130 fn put_f32_le(&mut self, value: f32) {
131 self.put_u32_le(value.to_bits());
132 }
133
134 #[inline]
136 fn put_f64_le(&mut self, value: f64) {
137 self.put_u64_le(value.to_bits());
138 }
139
140 #[inline]
147 fn put_shared(&mut self, bytes: Bytes) {
148 self.put_slice(&bytes);
149 }
150
151 #[doc(hidden)]
156 const __PRE_SIZED: bool = false;
157
158 #[doc(hidden)]
176 #[inline]
177 fn __with_pre_sized(&mut self, f: &mut dyn FnMut(&mut PreSized<'_>)) -> bool {
178 let _ = f;
179 false
180 }
181
182 #[doc(hidden)]
192 #[inline]
193 fn __write_pre_sized<F: FnOnce(&mut PreSized<'_>)>(
194 &mut self,
195 len: usize,
196 fill: F,
197 ) -> Result<(), F> {
198 let _ = len;
199 Err(fill)
200 }
201}
202
203impl<T: BufMut + ?Sized> EncodeSink for T {
204 const __PRE_SIZED: bool = true;
205
206 #[inline]
207 fn __write_pre_sized<F: FnOnce(&mut PreSized<'_>)>(
208 &mut self,
209 len: usize,
210 fill: F,
211 ) -> Result<(), F> {
212 let spare: &mut [MaybeUninit<u8>] = if len == 0 {
214 &mut []
215 } else {
216 let chunk = BufMut::chunk_mut(self);
217 if chunk.len() < len {
218 return Err(fill);
219 }
220 unsafe {
225 core::slice::from_raw_parts_mut(chunk.as_mut_ptr().cast::<MaybeUninit<u8>>(), len)
226 }
227 };
228 let mut cursor = PreSized::new(spare);
229 fill(&mut cursor);
230 let written = cursor.written();
231 unsafe { BufMut::advance_mut(self, written) };
234 Ok(())
235 }
236
237 #[inline]
238 fn put_u8(&mut self, value: u8) {
239 BufMut::put_u8(self, value);
240 }
241
242 #[inline]
243 fn put_slice(&mut self, src: &[u8]) {
244 BufMut::put_slice(self, src);
245 }
246
247 #[inline]
248 fn put_u32_le(&mut self, value: u32) {
249 BufMut::put_u32_le(self, value);
250 }
251
252 #[inline]
253 fn put_u64_le(&mut self, value: u64) {
254 BufMut::put_u64_le(self, value);
255 }
256}
257
258#[doc(hidden)]
268pub struct PreSized<'a> {
269 dst: &'a mut [MaybeUninit<u8>],
270 pos: usize,
271}
272
273#[cold]
274#[inline(never)]
275fn pre_sized_overflow() -> ! {
276 panic!("write_to produced more bytes than compute_size declared (two-pass traversal mismatch)")
277}
278
279impl<'a> PreSized<'a> {
280 #[inline]
286 pub(crate) fn new(dst: &'a mut [MaybeUninit<u8>]) -> Self {
287 Self { dst, pos: 0 }
288 }
289
290 #[inline]
292 pub(crate) const fn written(&self) -> usize {
293 self.pos
294 }
295}
296
297impl EncodeSink for PreSized<'_> {
298 #[inline]
299 fn __with_pre_sized(&mut self, f: &mut dyn FnMut(&mut PreSized<'_>)) -> bool {
300 f(self);
301 true
302 }
303
304 #[inline]
305 fn put_u8(&mut self, value: u8) {
306 let Some(slot) = self.dst.get_mut(self.pos) else {
307 pre_sized_overflow()
308 };
309 slot.write(value);
310 self.pos += 1;
311 }
312
313 #[inline]
314 fn put_slice(&mut self, src: &[u8]) {
315 let Some(dst) = self
316 .dst
317 .get_mut(self.pos..)
318 .and_then(|rest| rest.get_mut(..src.len()))
319 else {
320 pre_sized_overflow()
321 };
322 unsafe {
325 core::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr().cast::<u8>(), src.len());
326 }
327 self.pos += src.len();
328 }
329
330 #[inline]
331 fn put_u32_le(&mut self, value: u32) {
332 self.put_slice(&value.to_le_bytes());
333 }
334
335 #[inline]
336 fn put_u64_le(&mut self, value: u64) {
337 self.put_slice(&value.to_le_bytes());
338 }
339}
340
341#[inline]
354pub(crate) fn write_contiguous<S: EncodeSink>(
355 size: usize,
356 cache: &mut crate::SizeCache,
357 buf: &mut S,
358 pre_sized: impl FnOnce(&mut crate::SizeCache, &mut PreSized<'_>),
359 per_write: impl FnOnce(&mut crate::SizeCache, &mut S),
360) {
361 if S::IS_SEGMENTED || !S::__PRE_SIZED {
362 per_write(cache, buf);
363 return;
364 }
365 let fill = move |cursor: &mut PreSized<'_>| {
366 pre_sized(cache, cursor);
367 crate::message::debug_assert_two_pass(cursor.written(), size);
368 };
369 if let Err(fill) = buf.__write_pre_sized(size, fill) {
370 let scratch = write_to_new_vec(size, fill);
371 buf.put_slice(&scratch);
372 }
373}
374
375#[inline]
378pub(crate) fn write_to_new_vec(size: usize, fill: impl FnOnce(&mut PreSized<'_>)) -> Vec<u8> {
379 debug_assert!(size <= crate::MAX_MESSAGE_BYTES as usize);
380 let mut vec = Vec::with_capacity(size);
381 let written = {
382 let mut cursor = PreSized::new(&mut vec.spare_capacity_mut()[..size]);
383 fill(&mut cursor);
384 cursor.written()
385 };
386 unsafe { vec.set_len(written) };
389 vec
390}
391
392pub const DEFAULT_MIN_SEGMENT: usize = 4 * 1024;
401
402#[derive(Debug)]
422pub struct Rope {
423 segments: Vec<Bytes>,
425 tail: BytesMut,
427 backing: Option<Bytes>,
430 min_segment: usize,
432}
433
434impl Default for Rope {
435 fn default() -> Self {
438 Self::new()
439 }
440}
441
442impl Rope {
443 #[must_use]
445 pub fn new() -> Self {
446 Self::with_min_segment(DEFAULT_MIN_SEGMENT)
447 }
448
449 #[must_use]
456 pub fn with_min_segment(min_segment: usize) -> Self {
457 Self {
458 segments: Vec::new(),
459 tail: BytesMut::new(),
460 backing: None,
461 min_segment: min_segment.max(1),
462 }
463 }
464
465 #[must_use]
480 pub fn with_backing(mut self, backing: Bytes) -> Self {
481 self.backing = Some(backing);
482 self
483 }
484
485 #[must_use]
487 pub fn len(&self) -> usize {
488 self.segments.iter().map(Bytes::len).sum::<usize>() + self.tail.len()
489 }
490
491 #[must_use]
493 pub fn is_empty(&self) -> bool {
494 self.tail.is_empty() && self.segments.is_empty()
497 }
498
499 #[must_use]
502 pub fn segment_count(&self) -> usize {
503 self.segments.len() + usize::from(!self.tail.is_empty())
504 }
505
506 #[must_use]
508 pub fn into_segments(mut self) -> Vec<Bytes> {
509 self.flush_tail();
510 self.segments
511 }
512
513 #[must_use]
522 pub fn to_contiguous_bytes(&self) -> Bytes {
523 let mut out = Vec::with_capacity(self.len());
524 for segment in &self.segments {
525 out.extend_from_slice(segment);
526 }
527 out.extend_from_slice(&self.tail);
528 Bytes::from(out)
529 }
530
531 fn flush_tail(&mut self) {
533 if !self.tail.is_empty() {
534 self.segments.push(self.tail.split().freeze());
535 }
536 }
537}
538
539impl EncodeSink for Rope {
540 const IS_SEGMENTED: bool = true;
541
542 #[inline]
546 fn put_u8(&mut self, value: u8) {
547 self.tail.extend_from_slice(&[value]);
548 }
549
550 #[inline]
551 fn put_slice(&mut self, src: &[u8]) {
552 if src.len() >= self.min_segment {
555 if let Some(segment) = self
556 .backing
557 .as_ref()
558 .and_then(|b| crate::view::try_slice_ref(b, src))
559 {
560 self.flush_tail();
561 self.segments.push(segment);
562 return;
563 }
564 }
565 self.tail.extend_from_slice(src);
566 }
567
568 #[inline]
569 fn put_u32_le(&mut self, value: u32) {
570 self.tail.extend_from_slice(&value.to_le_bytes());
571 }
572
573 #[inline]
574 fn put_u64_le(&mut self, value: u64) {
575 self.tail.extend_from_slice(&value.to_le_bytes());
576 }
577
578 #[inline]
579 fn put_shared(&mut self, bytes: Bytes) {
580 if bytes.len() >= self.min_segment {
581 self.flush_tail();
582 self.segments.push(bytes);
583 } else {
584 self.tail.extend_from_slice(&bytes);
585 }
586 }
587}
588
589impl EncodeSink for &mut Rope {
595 const IS_SEGMENTED: bool = true;
596
597 #[inline]
598 fn put_u8(&mut self, value: u8) {
599 Rope::put_u8(self, value);
600 }
601
602 #[inline]
603 fn put_slice(&mut self, src: &[u8]) {
604 Rope::put_slice(self, src);
605 }
606
607 #[inline]
608 fn put_u32_le(&mut self, value: u32) {
609 Rope::put_u32_le(self, value);
610 }
611
612 #[inline]
613 fn put_u64_le(&mut self, value: u64) {
614 Rope::put_u64_le(self, value);
615 }
616
617 #[inline]
618 fn put_shared(&mut self, bytes: Bytes) {
619 Rope::put_shared(self, bytes);
620 }
621}
622
623#[derive(Debug)]
626pub struct RopeBuf {
627 segments: Vec<Bytes>,
629 pos: usize,
630 remaining: usize,
631}
632
633impl From<Rope> for RopeBuf {
634 fn from(rope: Rope) -> Self {
635 let segments = rope.into_segments();
636 let remaining = segments.iter().map(Bytes::len).sum();
637 Self {
638 segments,
639 pos: 0,
640 remaining,
641 }
642 }
643}
644
645impl Buf for RopeBuf {
646 fn remaining(&self) -> usize {
647 self.remaining
648 }
649
650 fn chunk(&self) -> &[u8] {
651 self.segments.get(self.pos).map_or(&[], |b| &b[..])
652 }
653
654 fn advance(&mut self, mut cnt: usize) {
655 assert!(cnt <= self.remaining, "advance past end of RopeBuf");
656 self.remaining -= cnt;
657 while cnt > 0 {
658 let front = &mut self.segments[self.pos];
659 if cnt < front.len() {
660 front.advance(cnt);
661 return;
662 }
663 cnt -= front.len();
664 *front = Bytes::new();
668 self.pos += 1;
669 }
670 }
671
672 #[inline]
680 fn copy_to_bytes(&mut self, len: usize) -> Bytes {
681 assert!(len <= self.remaining, "copy_to_bytes past end of RopeBuf");
682 if len == 0 {
683 return Bytes::new();
684 }
685 if len <= self.chunk().len() {
686 let front = &mut self.segments[self.pos];
687 let bytes = front.split_to(len);
688 self.remaining -= len;
689 if front.is_empty() {
690 self.pos += 1;
691 }
692 return bytes;
693 }
694
695 let mut bytes = BytesMut::with_capacity(len);
696 bytes.put(self.take(len));
697 bytes.freeze()
698 }
699
700 #[cfg(feature = "std")]
705 fn chunks_vectored<'a>(&'a self, dst: &mut [std::io::IoSlice<'a>]) -> usize {
706 let mut n = 0;
707 for segment in &self.segments[self.pos..] {
708 if n == dst.len() {
709 break;
710 }
711 debug_assert!(!segment.is_empty());
714 dst[n] = std::io::IoSlice::new(segment);
715 n += 1;
716 }
717 n
718 }
719}
720
721#[cfg(test)]
722mod tests {
723 use super::*;
724 use crate::test_doubles::Probe;
725
726 #[test]
730 fn default_matches_new() {
731 let mut rope = Rope::default();
732 rope.put_shared(Bytes::from_static(b"tiny"));
735 rope.put_shared(Bytes::new());
736 assert_eq!(rope.segment_count(), 1);
737 let segments = rope.into_segments();
738 assert_eq!(segments.len(), 1);
739 assert!(!segments[0].is_empty());
740 }
741
742 #[test]
745 fn mut_ref_forwarding_impl() {
746 fn write_through(mut sink: impl EncodeSink) {
747 sink.put_slice(b"via reborrow");
748 sink.put_shared(Bytes::from(crate::alloc::vec![9u8; 64]));
749 }
750 let mut rope = Rope::with_min_segment(64);
751 write_through(&mut rope);
752 assert_eq!(rope.segment_count(), 2);
753 assert_eq!(rope.len(), 12 + 64);
754 }
755
756 #[test]
759 fn advance_releases_consumed_segments() {
760 let payload = Bytes::from(crate::alloc::vec![3u8; 128]);
761 let mut rope = Rope::with_min_segment(64);
762 rope.put_shared(payload.clone());
763 rope.put_slice(b"after");
764 let mut buf = RopeBuf::from(rope);
765
766 assert!(payload.clone().try_into_mut().is_err(), "not unique yet");
768 buf.advance(128);
769 assert!(
771 payload.try_into_mut().is_ok(),
772 "consumed segment must be released before RopeBuf drop"
773 );
774 assert_eq!(buf.copy_to_bytes(buf.remaining()), &b"after"[..]);
775 }
776
777 #[test]
779 fn small_writes_coalesce() {
780 let mut rope = Rope::with_min_segment(16);
781 rope.put_u8(1);
782 rope.put_slice(b"abc");
783 rope.put_u32_le(7);
784 rope.put_u64_le(9);
785 assert_eq!(rope.segment_count(), 1);
786 let segments = rope.into_segments();
787 assert_eq!(segments.len(), 1);
788 assert_eq!(segments[0].len(), 1 + 3 + 4 + 8);
789 }
790
791 #[test]
794 fn large_shared_segment_is_not_copied() {
795 let payload = Bytes::from(crate::alloc::vec![0xAB; 64]);
796 let ptr = payload.as_ptr();
797
798 let mut rope = Rope::with_min_segment(64);
799 rope.put_slice(b"head");
800 rope.put_shared(payload);
801 rope.put_slice(b"tail");
802
803 let segments = rope.into_segments();
804 assert_eq!(segments.len(), 3);
805 assert_eq!(&segments[0][..], b"head");
806 assert!(core::ptr::eq(segments[1].as_ptr(), ptr), "must not copy");
807 assert_eq!(&segments[2][..], b"tail");
808 }
809
810 #[test]
812 fn small_shared_write_coalesces() {
813 let mut rope = Rope::with_min_segment(64);
814 rope.put_slice(b"a");
815 rope.put_shared(Bytes::from_static(b"bc"));
816 rope.put_slice(b"d");
817 let segments = rope.into_segments();
818 assert_eq!(segments.len(), 1);
819 assert_eq!(&segments[0][..], b"abcd");
820 }
821
822 #[test]
824 fn backed_slice_is_captured_zero_copy() {
825 let backing = Bytes::from(crate::alloc::vec![0x5A; 256]);
826 let inside: &[u8] = &backing[32..224];
827 let outside = crate::alloc::vec![0x5A; 192];
828
829 let mut rope = Rope::with_min_segment(64).with_backing(backing.clone());
830 rope.put_slice(inside);
831 rope.put_slice(&outside);
832
833 let segments = rope.into_segments();
834 assert_eq!(segments.len(), 2);
835 assert!(
836 core::ptr::eq(segments[0].as_ptr(), inside.as_ptr()),
837 "backed slice must be zero-copy"
838 );
839 assert!(
840 !core::ptr::eq(segments[1].as_ptr(), outside.as_ptr()),
841 "unbacked slice must be copied"
842 );
843 assert_eq!(&segments[1][..], &outside[..]);
844 }
845
846 #[test]
848 fn segments_concatenate_to_contiguous_output() {
849 let payload = Bytes::from(crate::alloc::vec![0x11; 128]);
850
851 let mut contiguous: Vec<u8> = Vec::new();
852 EncodeSink::put_u8(&mut contiguous, 0x0A);
853 EncodeSink::put_slice(&mut contiguous, &payload);
854 EncodeSink::put_u32_le(&mut contiguous, 42);
855 EncodeSink::put_u64_le(&mut contiguous, 0x0102_0304_0506_0708);
856
857 let mut rope = Rope::with_min_segment(64);
858 rope.put_u8(0x0A);
859 rope.put_shared(payload);
860 rope.put_u32_le(42);
861 rope.put_u64_le(0x0102_0304_0506_0708);
862
863 assert_eq!(&rope.to_contiguous_bytes()[..], &contiguous[..]);
864 }
865
866 #[test]
868 fn rope_buf_traverses_segments() {
869 let mut rope = Rope::with_min_segment(4);
870 rope.put_slice(b"ab");
871 rope.put_shared(Bytes::from_static(b"cdefgh"));
872 rope.put_slice(b"ij");
873 let mut buf = RopeBuf::from(rope);
874
875 assert_eq!(buf.remaining(), 10);
876 assert_eq!(buf.chunk(), b"ab");
877 buf.advance(3); assert_eq!(buf.chunk(), b"defgh");
879 let rest = buf.copy_to_bytes(buf.remaining());
880 assert_eq!(&rest[..], b"defghij");
881 }
882
883 fn rope_buf(segments: &[Bytes]) -> RopeBuf {
884 let mut rope = Rope::with_min_segment(1);
885 for segment in segments {
886 rope.put_shared(segment.clone());
887 }
888 RopeBuf::from(rope)
889 }
890
891 #[test]
892 fn rope_buf_copy_to_bytes_shares_whole_segment() {
893 let payload = Bytes::from(crate::alloc::vec![0xAB; 64]);
894 let ptr = payload.as_ptr();
895 let mut rope = Rope::with_min_segment(1);
896 rope.put_shared(payload);
897 let mut buf = RopeBuf::from(rope);
898
899 let copied = buf.copy_to_bytes(64);
900 assert_eq!(copied.as_ptr(), ptr);
901 assert_eq!(copied.as_ref(), &[0xAB; 64]);
902 assert_eq!(buf.remaining(), 0);
903 assert!(buf.chunk().is_empty());
904 assert!(copied.try_into_mut().is_ok());
905 }
906
907 #[test]
908 fn rope_buf_copy_to_bytes_shares_partial_segment() {
909 let payload = Bytes::copy_from_slice(b"abcdefgh");
910 let mut buf = rope_buf(core::slice::from_ref(&payload));
911 let copied = buf.copy_to_bytes(3);
912 assert_eq!(copied, &b"abc"[..]);
913 assert_eq!(copied.as_ptr(), payload.as_ptr());
914 assert_eq!(buf.remaining(), 5);
915 assert_eq!(buf.chunk(), b"defgh");
916 let rest = buf.copy_to_bytes(5);
917 assert_eq!(rest.as_ptr(), payload.as_ptr().wrapping_add(3));
918 assert_eq!(rest, &b"defgh"[..]);
919 assert_eq!(buf.remaining(), 0);
920 }
921
922 #[test]
923 fn rope_buf_copy_to_bytes_partial_result_outlives_buffer() {
924 let (copied, ptr) = {
925 let payload = Bytes::copy_from_slice(b"abcdefgh");
926 let ptr = payload.as_ptr();
927 let mut buf = rope_buf(&[payload]);
928 (buf.copy_to_bytes(3), ptr)
929 };
930 assert_eq!(copied.as_ptr(), ptr);
931 assert_eq!(copied, &b"abc"[..]);
932 assert!(copied.try_into_mut().is_ok());
933 }
934
935 #[test]
936 fn rope_buf_copy_to_bytes_shares_segment_after_advance() {
937 let payload = Bytes::from(crate::alloc::vec![0x5A; 64]);
938 let mut buf = rope_buf(&[Bytes::from_static(b"head"), payload.clone()]);
939 buf.advance(7);
940 let copied = buf.copy_to_bytes(10);
941 assert_eq!(copied.as_ptr(), payload.as_ptr().wrapping_add(3));
942 assert_eq!(copied.as_ref(), &[0x5A; 10]);
943 assert_eq!(buf.remaining(), 51);
944 assert_eq!(buf.chunk().as_ptr(), payload.as_ptr().wrapping_add(13));
945 }
946
947 #[test]
948 fn rope_buf_copy_to_bytes_zero_does_not_advance() {
949 let mut empty = RopeBuf::from(Rope::new());
950 assert!(empty.copy_to_bytes(0).is_empty());
951 assert_eq!(empty.remaining(), 0);
952
953 let mut buf = rope_buf(&[Bytes::from_static(b"abc")]);
954 assert!(buf.copy_to_bytes(0).is_empty());
955 assert_eq!(buf.remaining(), 3);
956 assert_eq!(buf.chunk(), b"abc");
957 assert_eq!(buf.copy_to_bytes(3), &b"abc"[..]);
958 assert!(buf.copy_to_bytes(0).is_empty());
959 assert!(buf.chunk().is_empty());
960 }
961
962 #[test]
963 fn rope_buf_copy_to_bytes_matches_contiguous_buffer_at_every_boundary() {
964 let segments = [
965 Bytes::from_static(b"a"),
966 Bytes::from_static(b"bcde"),
967 Bytes::from_static(b"fgh"),
968 Bytes::from_static(b"ijklmn"),
969 ];
970 let expected = b"abcdefghijklmn";
971 for offset in 0..=expected.len() {
972 for len in 0..=expected.len() - offset {
973 let mut buf = rope_buf(&segments);
974 buf.advance(offset);
975 assert_eq!(buf.copy_to_bytes(len), &expected[offset..offset + len]);
976 assert_eq!(buf.remaining(), expected.len() - offset - len);
977 let remaining = buf.remaining();
978 assert_eq!(buf.copy_to_bytes(remaining), &expected[offset + len..]);
979 assert!(buf.chunk().is_empty());
980 }
981 }
982 }
983
984 #[test]
985 fn rope_buf_copy_to_bytes_take_preserves_limit_and_sharing() {
986 let payload = Bytes::from_static(b"abcdefgh");
987 let mut buf = rope_buf(core::slice::from_ref(&payload));
988 let mut limited = (&mut buf).take(3);
989 let copied = limited.copy_to_bytes(2);
990 assert_eq!(copied.as_ptr(), payload.as_ptr());
991 assert_eq!(copied, &b"ab"[..]);
992 assert_eq!(limited.remaining(), 1);
993 assert_eq!(limited.copy_to_bytes(1), &b"c"[..]);
994 assert_eq!(limited.remaining(), 0);
995 assert_eq!(buf.remaining(), 5);
996 assert_eq!(buf.chunk(), b"defgh");
997 }
998
999 #[cfg(feature = "std")]
1000 #[test]
1001 fn rope_buf_copy_to_bytes_updates_vectored_chunks() {
1002 let segments = [Bytes::from_static(b"abc"), Bytes::from_static(b"def")];
1003 let mut buf = rope_buf(&segments);
1004 assert_eq!(buf.copy_to_bytes(3), &b"abc"[..]);
1005 {
1006 let mut slices = [std::io::IoSlice::new(&[]); 2];
1007 assert_eq!(buf.chunks_vectored(&mut slices), 1);
1008 assert_eq!(slices[0].as_ref(), b"def");
1009 }
1010 assert_eq!(buf.copy_to_bytes(3), &b"def"[..]);
1011 let mut slices = [std::io::IoSlice::new(&[]); 2];
1012 assert_eq!(buf.chunks_vectored(&mut slices), 0);
1013 }
1014
1015 #[test]
1016 #[should_panic(expected = "copy_to_bytes past end of RopeBuf")]
1017 fn rope_buf_copy_to_bytes_rejects_overread() {
1018 let mut buf = rope_buf(&[Bytes::from_static(b"a"), Bytes::from_static(b"bc")]);
1019 buf.copy_to_bytes(4);
1020 }
1021
1022 #[test]
1023 #[should_panic(expected = "copy_to_bytes past end of RopeBuf")]
1024 fn rope_buf_copy_to_bytes_rejects_read_from_empty_buffer() {
1025 let mut buf = RopeBuf::from(Rope::new());
1026 buf.copy_to_bytes(1);
1027 }
1028
1029 #[test]
1031 fn max_threshold_never_segments() {
1032 let mut rope = Rope::with_min_segment(usize::MAX);
1033 rope.put_shared(Bytes::from(crate::alloc::vec![1u8; 1024]));
1034 rope.put_slice(&crate::alloc::vec![2u8; 1024]);
1035 assert_eq!(rope.segment_count(), 1);
1036 }
1037
1038 const PAYLOAD_LEN: usize = 16;
1042
1043 fn write_payload<S: EncodeSink>(sink: &mut S) {
1044 sink.put_u8(0xAA);
1045 sink.put_slice(b"abc");
1046 sink.put_u32_le(0x0403_0201);
1047 sink.put_u64_le(0x0807_0605_0403_0201);
1048 }
1049
1050 fn payload() -> Vec<u8> {
1051 let mut expected = Vec::new();
1052 write_payload(&mut expected);
1053 assert_eq!(expected.len(), PAYLOAD_LEN);
1054 expected
1055 }
1056
1057 #[derive(Debug, PartialEq)]
1059 enum Ran {
1060 Cursor,
1061 Sink,
1062 Neither,
1063 }
1064
1065 fn run<S: EncodeSink>(size: usize, buf: &mut S) -> Ran {
1066 let ran = core::cell::Cell::new(Ran::Neither);
1067 write_contiguous(
1068 size,
1069 &mut crate::SizeCache::new(),
1070 buf,
1071 |_, cursor| {
1072 ran.set(Ran::Cursor);
1073 write_payload(cursor);
1074 },
1075 |_, sink| {
1076 ran.set(Ran::Sink);
1077 write_payload(sink);
1078 },
1079 );
1080 ran.into_inner()
1081 }
1082
1083 #[test]
1084 fn pre_sized_matches_vec_for_every_write() {
1085 let mut storage = [MaybeUninit::<u8>::uninit(); PAYLOAD_LEN];
1086 let mut cursor = PreSized::new(&mut storage);
1087 write_payload(&mut cursor);
1088 assert_eq!(cursor.written(), PAYLOAD_LEN);
1089 let got: Vec<u8> = storage.iter().map(|b| unsafe { b.assume_init() }).collect();
1091 assert_eq!(got, payload());
1092 }
1093
1094 #[test]
1095 #[should_panic(expected = "more bytes than compute_size declared")]
1096 fn pre_sized_put_u8_past_end_panics() {
1097 let mut storage = [MaybeUninit::<u8>::uninit(); 1];
1098 let mut cursor = PreSized::new(&mut storage);
1099 cursor.put_u8(1);
1100 cursor.put_u8(2);
1101 }
1102
1103 #[test]
1104 #[should_panic(expected = "more bytes than compute_size declared")]
1105 fn pre_sized_put_slice_past_end_panics() {
1106 let mut storage = [MaybeUninit::<u8>::uninit(); 2];
1107 PreSized::new(&mut storage).put_slice(b"abc");
1108 }
1109
1110 #[test]
1111 #[should_panic(expected = "more bytes than compute_size declared")]
1112 fn pre_sized_fixed_width_past_end_panics() {
1113 let mut storage = [MaybeUninit::<u8>::uninit(); 7];
1114 PreSized::new(&mut storage).put_u64_le(1);
1115 }
1116
1117 #[test]
1118 fn spare_capacity_is_filled_in_place() {
1119 let mut sink = Probe {
1120 inner: Vec::with_capacity(64),
1121 ..Probe::default()
1122 };
1123 sink.inner.extend_from_slice(b"prefix");
1124 assert_eq!(run(PAYLOAD_LEN, &mut sink), Ran::Cursor);
1125 assert_eq!(sink.put_slice_calls, 0, "no scratch buffer");
1126 assert_eq!(sink.advance_mut_calls, 1);
1127 assert_eq!(&sink.inner[..6], b"prefix");
1128 assert_eq!(&sink.inner[6..], &payload()[..]);
1129 assert_eq!(sink.inner.capacity(), 64, "the sink did not grow");
1130 }
1131
1132 #[test]
1135 fn short_chunk_is_filled_through_a_scratch_buffer() {
1136 let mut sink = Probe {
1137 chunk_limit: Some(PAYLOAD_LEN - 1),
1138 ..Probe::default()
1139 };
1140 assert_eq!(run(PAYLOAD_LEN, &mut sink), Ran::Cursor);
1141 assert_eq!(sink.advance_mut_calls, 0);
1142 assert_eq!(sink.put_slice_calls, 1);
1143 assert_eq!(sink.inner, payload());
1144 }
1145
1146 #[test]
1147 fn chained_buffers_receive_the_bytes_across_the_boundary() {
1148 let mut head = [0u8; 4];
1149 let mut tail = Vec::new();
1150 let mut sink = (&mut head[..]).chain_mut(&mut tail);
1151 assert_eq!(run(PAYLOAD_LEN, &mut sink), Ran::Cursor);
1152 let expected = payload();
1153 assert_eq!(head, expected[..4]);
1154 assert_eq!(tail, expected[4..]);
1155 }
1156
1157 #[test]
1158 fn bytes_mut_and_slice_sinks_receive_the_same_bytes() {
1159 let expected = payload();
1160
1161 let mut bytes_mut = BytesMut::with_capacity(PAYLOAD_LEN);
1162 assert_eq!(run(PAYLOAD_LEN, &mut bytes_mut), Ran::Cursor);
1163 assert_eq!(&bytes_mut[..], &expected[..]);
1164
1165 let mut storage = [0u8; PAYLOAD_LEN];
1166 let mut slice: &mut [u8] = &mut storage;
1167 assert_eq!(run(PAYLOAD_LEN, &mut slice), Ran::Cursor);
1168 assert_eq!(
1169 slice.len(),
1170 0,
1171 "the slice sink was advanced past the message"
1172 );
1173 assert_eq!(&storage[..], &expected[..]);
1174 }
1175
1176 #[test]
1178 #[should_panic]
1179 fn slice_sink_too_small_still_panics() {
1180 let mut storage = [0u8; PAYLOAD_LEN - 1];
1181 let mut slice: &mut [u8] = &mut storage;
1182 run(PAYLOAD_LEN, &mut slice);
1183 }
1184
1185 #[test]
1186 fn empty_message_appends_nothing() {
1187 let mut sink = Probe::default();
1188 write_contiguous(
1189 0,
1190 &mut crate::SizeCache::new(),
1191 &mut sink,
1192 |_, _| {},
1193 |_, _| unreachable!("a `BufMut` is written through the cursor"),
1194 );
1195 assert!(sink.inner.is_empty());
1196 assert_eq!(sink.chunk_mut_calls, 0);
1197 assert_eq!(sink.put_slice_calls, 0);
1198 }
1199
1200 #[test]
1203 fn direct_encode_sink_receives_every_write() {
1204 #[derive(Default)]
1205 struct Recording {
1206 puts: Vec<&'static str>,
1207 bytes: Vec<u8>,
1208 }
1209 impl EncodeSink for Recording {
1210 fn put_u8(&mut self, v: u8) {
1211 self.puts.push("u8");
1212 self.bytes.push(v);
1213 }
1214 fn put_slice(&mut self, src: &[u8]) {
1215 self.puts.push("slice");
1216 self.bytes.extend_from_slice(src);
1217 }
1218 fn put_u32_le(&mut self, v: u32) {
1219 self.puts.push("u32");
1220 self.bytes.extend_from_slice(&v.to_le_bytes());
1221 }
1222 fn put_u64_le(&mut self, v: u64) {
1223 self.puts.push("u64");
1224 self.bytes.extend_from_slice(&v.to_le_bytes());
1225 }
1226 }
1227
1228 let mut sink = Recording::default();
1229 assert_eq!(run(PAYLOAD_LEN, &mut sink), Ran::Sink);
1230 assert_eq!(sink.puts, ["u8", "slice", "u32", "u64"]);
1231 assert_eq!(sink.bytes, payload());
1232 }
1233
1234 #[test]
1236 fn rope_takes_the_sink_path() {
1237 let mut rope = Rope::new();
1238 assert_eq!(run(PAYLOAD_LEN, &mut rope), Ran::Sink);
1239 assert_eq!(&rope.to_contiguous_bytes()[..], &payload()[..]);
1240 }
1241
1242 #[cfg(debug_assertions)]
1243 #[test]
1244 #[should_panic(expected = "two-pass traversal mismatch")]
1245 fn under_writing_is_caught_in_debug_builds() {
1246 write_contiguous(
1247 10,
1248 &mut crate::SizeCache::new(),
1249 &mut Vec::with_capacity(32),
1250 |_, cursor| cursor.put_slice(b"abc"),
1251 |_, _| unreachable!("a Vec is written through the cursor"),
1252 );
1253 }
1254
1255 #[test]
1256 fn write_to_new_vec_is_exactly_sized() {
1257 let vec = write_to_new_vec(PAYLOAD_LEN, |cursor| write_payload(cursor));
1258 assert_eq!(vec, payload());
1259 assert_eq!(vec.capacity(), PAYLOAD_LEN);
1260 }
1261
1262 #[test]
1263 fn under_writing_publishes_only_the_written_bytes() {
1264 let mut vec = Vec::with_capacity(32);
1265 assert!(vec
1266 .__write_pre_sized(10, |cursor| cursor.put_slice(b"abc"))
1267 .is_ok());
1268 assert_eq!(vec, b"abc");
1269
1270 let scratch = write_to_new_vec(10, |cursor| cursor.put_slice(b"abc"));
1271 assert_eq!(scratch, b"abc");
1272 assert_eq!(scratch.capacity(), 10);
1273 }
1274
1275 #[cfg(feature = "std")]
1276 #[test]
1277 fn a_panic_mid_write_leaves_the_sink_unchanged() {
1278 let mut vec = Vec::with_capacity(32);
1279 vec.extend_from_slice(b"kept");
1280 let result = std::panic::catch_unwind(core::panic::AssertUnwindSafe(|| {
1281 let _ = vec.__write_pre_sized(8, |cursor| {
1282 cursor.put_slice(b"abc");
1283 panic!("write_to failed");
1284 });
1285 }));
1286 assert!(result.is_err());
1287 assert_eq!(vec, b"kept");
1288 }
1289
1290 #[test]
1291 fn limit_sink_is_filled_within_its_limit() {
1292 let mut sink = Vec::new().limit(PAYLOAD_LEN);
1293 assert_eq!(run(PAYLOAD_LEN, &mut sink), Ran::Cursor);
1294 assert_eq!(sink.remaining_mut(), 0);
1295 assert_eq!(sink.into_inner(), payload());
1296 }
1297
1298 #[test]
1299 #[should_panic(expected = "more bytes than compute_size declared")]
1300 fn empty_message_that_writes_anyway_panics() {
1301 run(0, &mut Vec::<u8>::new());
1302 }
1303}