1 use crate::buf
::{limit, Chain, Limit, UninitSlice}
;
2 #[cfg(feature = "std")]
3 use crate::buf
::{writer, Writer}
;
5 use core
::{cmp, mem, ptr, usize}
;
7 use alloc
::{boxed::Box, vec::Vec}
;
9 /// A trait for values that provide sequential write access to bytes.
11 /// Write bytes to a buffer
13 /// A buffer stores bytes in memory such that write operations are infallible.
14 /// The underlying storage may or may not be in contiguous memory. A `BufMut`
15 /// value is a cursor into the buffer. Writing to `BufMut` advances the cursor
18 /// The simplest `BufMut` is a `Vec<u8>`.
21 /// use bytes::BufMut;
23 /// let mut buf = vec![];
25 /// buf.put(&b"hello world"[..]);
27 /// assert_eq!(buf, b"hello world");
29 pub unsafe trait BufMut
{
30 /// Returns the number of bytes that can be written from the current
31 /// position until the end of the buffer is reached.
33 /// This value is greater than or equal to the length of the slice returned
36 /// Writing to a `BufMut` may involve allocating more memory on the fly.
37 /// Implementations may fail before reaching the number of bytes indicated
38 /// by this method if they encounter an allocation failure.
43 /// use bytes::BufMut;
45 /// let mut dst = [0; 10];
46 /// let mut buf = &mut dst[..];
48 /// let original_remaining = buf.remaining_mut();
49 /// buf.put(&b"hello"[..]);
51 /// assert_eq!(original_remaining - 5, buf.remaining_mut());
54 /// # Implementer notes
56 /// Implementations of `remaining_mut` should ensure that the return value
57 /// does not change unless a call is made to `advance_mut` or any other
58 /// function that is documented to change the `BufMut`'s current position.
62 /// `remaining_mut` may return value smaller than actual available space.
63 fn remaining_mut(&self) -> usize;
65 /// Advance the internal cursor of the BufMut
67 /// The next call to `chunk_mut` will return a slice starting `cnt` bytes
68 /// further into the underlying buffer.
70 /// This function is unsafe because there is no guarantee that the bytes
71 /// being advanced past have been initialized.
76 /// use bytes::BufMut;
78 /// let mut buf = Vec::with_capacity(16);
80 /// // Write some data
81 /// buf.chunk_mut()[0..2].copy_from_slice(b"he");
82 /// unsafe { buf.advance_mut(2) };
84 /// // write more bytes
85 /// buf.chunk_mut()[0..3].copy_from_slice(b"llo");
87 /// unsafe { buf.advance_mut(3); }
89 /// assert_eq!(5, buf.len());
90 /// assert_eq!(buf, b"hello");
95 /// This function **may** panic if `cnt > self.remaining_mut()`.
97 /// # Implementer notes
99 /// It is recommended for implementations of `advance_mut` to panic if
100 /// `cnt > self.remaining_mut()`. If the implementation does not panic,
101 /// the call must behave as if `cnt == self.remaining_mut()`.
103 /// A call with `cnt == 0` should never panic and be a no-op.
104 unsafe fn advance_mut(&mut self, cnt
: usize);
106 /// Returns true if there is space in `self` for more bytes.
108 /// This is equivalent to `self.remaining_mut() != 0`.
113 /// use bytes::BufMut;
115 /// let mut dst = [0; 5];
116 /// let mut buf = &mut dst[..];
118 /// assert!(buf.has_remaining_mut());
120 /// buf.put(&b"hello"[..]);
122 /// assert!(!buf.has_remaining_mut());
124 fn has_remaining_mut(&self) -> bool
{
125 self.remaining_mut() > 0
128 /// Returns a mutable slice starting at the current BufMut position and of
129 /// length between 0 and `BufMut::remaining_mut()`. Note that this *can* be shorter than the
130 /// whole remainder of the buffer (this allows non-continuous implementation).
132 /// This is a lower level function. Most operations are done with other
135 /// The returned byte slice may represent uninitialized memory.
140 /// use bytes::BufMut;
142 /// let mut buf = Vec::with_capacity(16);
145 /// // MaybeUninit::as_mut_ptr
146 /// buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
147 /// buf.chunk_mut()[1..].as_mut_ptr().write(b'e');
149 /// buf.advance_mut(2);
151 /// buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
152 /// buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
153 /// buf.chunk_mut()[2..].as_mut_ptr().write(b'o');
155 /// buf.advance_mut(3);
158 /// assert_eq!(5, buf.len());
159 /// assert_eq!(buf, b"hello");
162 /// # Implementer notes
164 /// This function should never panic. `chunk_mut` should return an empty
165 /// slice **if and only if** `remaining_mut()` returns 0. In other words,
166 /// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
167 /// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
168 /// return an empty slice.
170 /// This function may trigger an out-of-memory abort if it tries to allocate
171 /// memory and fails to do so.
172 // The `chunk_mut` method was previously called `bytes_mut`. This alias makes the
173 // rename more easily discoverable.
174 #[cfg_attr(docsrs, doc(alias = "bytes_mut"))]
175 fn chunk_mut(&mut self) -> &mut UninitSlice
;
177 /// Transfer bytes into `self` from `src` and advance the cursor by the
178 /// number of bytes written.
183 /// use bytes::BufMut;
185 /// let mut buf = vec![];
187 /// buf.put_u8(b'h');
188 /// buf.put(&b"ello"[..]);
189 /// buf.put(&b" world"[..]);
191 /// assert_eq!(buf, b"hello world");
196 /// Panics if `self` does not have enough capacity to contain `src`.
197 fn put
<T
: super::Buf
>(&mut self, mut src
: T
)
201 assert
!(self.remaining_mut() >= src
.remaining());
203 while src
.has_remaining() {
208 let d
= self.chunk_mut();
209 l
= cmp
::min(s
.len(), d
.len());
211 ptr
::copy_nonoverlapping(s
.as_ptr(), d
.as_mut_ptr() as *mut u8, l
);
221 /// Transfer bytes into `self` from `src` and advance the cursor by the
222 /// number of bytes written.
224 /// `self` must have enough remaining capacity to contain all of `src`.
227 /// use bytes::BufMut;
229 /// let mut dst = [0; 6];
232 /// let mut buf = &mut dst[..];
233 /// buf.put_slice(b"hello");
235 /// assert_eq!(1, buf.remaining_mut());
238 /// assert_eq!(b"hello\0", &dst);
240 fn put_slice(&mut self, src
: &[u8]) {
244 self.remaining_mut() >= src
.len(),
245 "buffer overflow; remaining = {}; src = {}",
246 self.remaining_mut(),
250 while off
< src
.len() {
254 let dst
= self.chunk_mut();
255 cnt
= cmp
::min(dst
.len(), src
.len() - off
);
257 ptr
::copy_nonoverlapping(src
[off
..].as_ptr(), dst
.as_mut_ptr() as *mut u8, cnt
);
263 self.advance_mut(cnt
);
268 /// Put `cnt` bytes `val` into `self`.
270 /// Logically equivalent to calling `self.put_u8(val)` `cnt` times, but may work faster.
272 /// `self` must have at least `cnt` remaining capacity.
275 /// use bytes::BufMut;
277 /// let mut dst = [0; 6];
280 /// let mut buf = &mut dst[..];
281 /// buf.put_bytes(b'a', 4);
283 /// assert_eq!(2, buf.remaining_mut());
286 /// assert_eq!(b"aaaa\0\0", &dst);
291 /// This function panics if there is not enough remaining capacity in
293 fn put_bytes(&mut self, val
: u8, cnt
: usize) {
299 /// Writes an unsigned 8 bit integer to `self`.
301 /// The current position is advanced by 1.
306 /// use bytes::BufMut;
308 /// let mut buf = vec![];
309 /// buf.put_u8(0x01);
310 /// assert_eq!(buf, b"\x01");
315 /// This function panics if there is not enough remaining capacity in
317 fn put_u8(&mut self, n
: u8) {
319 self.put_slice(&src
);
322 /// Writes a signed 8 bit integer to `self`.
324 /// The current position is advanced by 1.
329 /// use bytes::BufMut;
331 /// let mut buf = vec![];
332 /// buf.put_i8(0x01);
333 /// assert_eq!(buf, b"\x01");
338 /// This function panics if there is not enough remaining capacity in
340 fn put_i8(&mut self, n
: i8) {
345 /// Writes an unsigned 16 bit integer to `self` in big-endian byte order.
347 /// The current position is advanced by 2.
352 /// use bytes::BufMut;
354 /// let mut buf = vec![];
355 /// buf.put_u16(0x0809);
356 /// assert_eq!(buf, b"\x08\x09");
361 /// This function panics if there is not enough remaining capacity in
363 fn put_u16(&mut self, n
: u16) {
364 self.put_slice(&n
.to_be_bytes())
367 /// Writes an unsigned 16 bit integer to `self` in little-endian byte order.
369 /// The current position is advanced by 2.
374 /// use bytes::BufMut;
376 /// let mut buf = vec![];
377 /// buf.put_u16_le(0x0809);
378 /// assert_eq!(buf, b"\x09\x08");
383 /// This function panics if there is not enough remaining capacity in
385 fn put_u16_le(&mut self, n
: u16) {
386 self.put_slice(&n
.to_le_bytes())
389 /// Writes a signed 16 bit integer to `self` in big-endian byte order.
391 /// The current position is advanced by 2.
396 /// use bytes::BufMut;
398 /// let mut buf = vec![];
399 /// buf.put_i16(0x0809);
400 /// assert_eq!(buf, b"\x08\x09");
405 /// This function panics if there is not enough remaining capacity in
407 fn put_i16(&mut self, n
: i16) {
408 self.put_slice(&n
.to_be_bytes())
411 /// Writes a signed 16 bit integer to `self` in little-endian byte order.
413 /// The current position is advanced by 2.
418 /// use bytes::BufMut;
420 /// let mut buf = vec![];
421 /// buf.put_i16_le(0x0809);
422 /// assert_eq!(buf, b"\x09\x08");
427 /// This function panics if there is not enough remaining capacity in
429 fn put_i16_le(&mut self, n
: i16) {
430 self.put_slice(&n
.to_le_bytes())
433 /// Writes an unsigned 32 bit integer to `self` in big-endian byte order.
435 /// The current position is advanced by 4.
440 /// use bytes::BufMut;
442 /// let mut buf = vec![];
443 /// buf.put_u32(0x0809A0A1);
444 /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
449 /// This function panics if there is not enough remaining capacity in
451 fn put_u32(&mut self, n
: u32) {
452 self.put_slice(&n
.to_be_bytes())
455 /// Writes an unsigned 32 bit integer to `self` in little-endian byte order.
457 /// The current position is advanced by 4.
462 /// use bytes::BufMut;
464 /// let mut buf = vec![];
465 /// buf.put_u32_le(0x0809A0A1);
466 /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
471 /// This function panics if there is not enough remaining capacity in
473 fn put_u32_le(&mut self, n
: u32) {
474 self.put_slice(&n
.to_le_bytes())
477 /// Writes a signed 32 bit integer to `self` in big-endian byte order.
479 /// The current position is advanced by 4.
484 /// use bytes::BufMut;
486 /// let mut buf = vec![];
487 /// buf.put_i32(0x0809A0A1);
488 /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
493 /// This function panics if there is not enough remaining capacity in
495 fn put_i32(&mut self, n
: i32) {
496 self.put_slice(&n
.to_be_bytes())
499 /// Writes a signed 32 bit integer to `self` in little-endian byte order.
501 /// The current position is advanced by 4.
506 /// use bytes::BufMut;
508 /// let mut buf = vec![];
509 /// buf.put_i32_le(0x0809A0A1);
510 /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
515 /// This function panics if there is not enough remaining capacity in
517 fn put_i32_le(&mut self, n
: i32) {
518 self.put_slice(&n
.to_le_bytes())
521 /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order.
523 /// The current position is advanced by 8.
528 /// use bytes::BufMut;
530 /// let mut buf = vec![];
531 /// buf.put_u64(0x0102030405060708);
532 /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
537 /// This function panics if there is not enough remaining capacity in
539 fn put_u64(&mut self, n
: u64) {
540 self.put_slice(&n
.to_be_bytes())
543 /// Writes an unsigned 64 bit integer to `self` in little-endian byte order.
545 /// The current position is advanced by 8.
550 /// use bytes::BufMut;
552 /// let mut buf = vec![];
553 /// buf.put_u64_le(0x0102030405060708);
554 /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
559 /// This function panics if there is not enough remaining capacity in
561 fn put_u64_le(&mut self, n
: u64) {
562 self.put_slice(&n
.to_le_bytes())
565 /// Writes a signed 64 bit integer to `self` in the big-endian byte order.
567 /// The current position is advanced by 8.
572 /// use bytes::BufMut;
574 /// let mut buf = vec![];
575 /// buf.put_i64(0x0102030405060708);
576 /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
581 /// This function panics if there is not enough remaining capacity in
583 fn put_i64(&mut self, n
: i64) {
584 self.put_slice(&n
.to_be_bytes())
587 /// Writes a signed 64 bit integer to `self` in little-endian byte order.
589 /// The current position is advanced by 8.
594 /// use bytes::BufMut;
596 /// let mut buf = vec![];
597 /// buf.put_i64_le(0x0102030405060708);
598 /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
603 /// This function panics if there is not enough remaining capacity in
605 fn put_i64_le(&mut self, n
: i64) {
606 self.put_slice(&n
.to_le_bytes())
609 /// Writes an unsigned 128 bit integer to `self` in the big-endian byte order.
611 /// The current position is advanced by 16.
616 /// use bytes::BufMut;
618 /// let mut buf = vec![];
619 /// buf.put_u128(0x01020304050607080910111213141516);
620 /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
625 /// This function panics if there is not enough remaining capacity in
627 fn put_u128(&mut self, n
: u128
) {
628 self.put_slice(&n
.to_be_bytes())
631 /// Writes an unsigned 128 bit integer to `self` in little-endian byte order.
633 /// The current position is advanced by 16.
638 /// use bytes::BufMut;
640 /// let mut buf = vec![];
641 /// buf.put_u128_le(0x01020304050607080910111213141516);
642 /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
647 /// This function panics if there is not enough remaining capacity in
649 fn put_u128_le(&mut self, n
: u128
) {
650 self.put_slice(&n
.to_le_bytes())
653 /// Writes a signed 128 bit integer to `self` in the big-endian byte order.
655 /// The current position is advanced by 16.
660 /// use bytes::BufMut;
662 /// let mut buf = vec![];
663 /// buf.put_i128(0x01020304050607080910111213141516);
664 /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
669 /// This function panics if there is not enough remaining capacity in
671 fn put_i128(&mut self, n
: i128
) {
672 self.put_slice(&n
.to_be_bytes())
675 /// Writes a signed 128 bit integer to `self` in little-endian byte order.
677 /// The current position is advanced by 16.
682 /// use bytes::BufMut;
684 /// let mut buf = vec![];
685 /// buf.put_i128_le(0x01020304050607080910111213141516);
686 /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
691 /// This function panics if there is not enough remaining capacity in
693 fn put_i128_le(&mut self, n
: i128
) {
694 self.put_slice(&n
.to_le_bytes())
697 /// Writes an unsigned n-byte integer to `self` in big-endian byte order.
699 /// The current position is advanced by `nbytes`.
704 /// use bytes::BufMut;
706 /// let mut buf = vec![];
707 /// buf.put_uint(0x010203, 3);
708 /// assert_eq!(buf, b"\x01\x02\x03");
713 /// This function panics if there is not enough remaining capacity in
715 fn put_uint(&mut self, n
: u64, nbytes
: usize) {
716 self.put_slice(&n
.to_be_bytes()[mem
::size_of_val(&n
) - nbytes
..]);
719 /// Writes an unsigned n-byte integer to `self` in the little-endian byte order.
721 /// The current position is advanced by `nbytes`.
726 /// use bytes::BufMut;
728 /// let mut buf = vec![];
729 /// buf.put_uint_le(0x010203, 3);
730 /// assert_eq!(buf, b"\x03\x02\x01");
735 /// This function panics if there is not enough remaining capacity in
737 fn put_uint_le(&mut self, n
: u64, nbytes
: usize) {
738 self.put_slice(&n
.to_le_bytes()[0..nbytes
]);
741 /// Writes low `nbytes` of a signed integer to `self` in big-endian byte order.
743 /// The current position is advanced by `nbytes`.
748 /// use bytes::BufMut;
750 /// let mut buf = vec![];
751 /// buf.put_int(0x0504010203, 3);
752 /// assert_eq!(buf, b"\x01\x02\x03");
757 /// This function panics if there is not enough remaining capacity in
758 /// `self` or if `nbytes` is greater than 8.
759 fn put_int(&mut self, n
: i64, nbytes
: usize) {
760 self.put_slice(&n
.to_be_bytes()[mem
::size_of_val(&n
) - nbytes
..]);
763 /// Writes low `nbytes` of a signed integer to `self` in little-endian byte order.
765 /// The current position is advanced by `nbytes`.
770 /// use bytes::BufMut;
772 /// let mut buf = vec![];
773 /// buf.put_int_le(0x0504010203, 3);
774 /// assert_eq!(buf, b"\x03\x02\x01");
779 /// This function panics if there is not enough remaining capacity in
780 /// `self` or if `nbytes` is greater than 8.
781 fn put_int_le(&mut self, n
: i64, nbytes
: usize) {
782 self.put_slice(&n
.to_le_bytes()[0..nbytes
]);
785 /// Writes an IEEE754 single-precision (4 bytes) floating point number to
786 /// `self` in big-endian byte order.
788 /// The current position is advanced by 4.
793 /// use bytes::BufMut;
795 /// let mut buf = vec![];
796 /// buf.put_f32(1.2f32);
797 /// assert_eq!(buf, b"\x3F\x99\x99\x9A");
802 /// This function panics if there is not enough remaining capacity in
804 fn put_f32(&mut self, n
: f32) {
805 self.put_u32(n
.to_bits());
808 /// Writes an IEEE754 single-precision (4 bytes) floating point number to
809 /// `self` in little-endian byte order.
811 /// The current position is advanced by 4.
816 /// use bytes::BufMut;
818 /// let mut buf = vec![];
819 /// buf.put_f32_le(1.2f32);
820 /// assert_eq!(buf, b"\x9A\x99\x99\x3F");
825 /// This function panics if there is not enough remaining capacity in
827 fn put_f32_le(&mut self, n
: f32) {
828 self.put_u32_le(n
.to_bits());
831 /// Writes an IEEE754 double-precision (8 bytes) floating point number to
832 /// `self` in big-endian byte order.
834 /// The current position is advanced by 8.
839 /// use bytes::BufMut;
841 /// let mut buf = vec![];
842 /// buf.put_f64(1.2f64);
843 /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
848 /// This function panics if there is not enough remaining capacity in
850 fn put_f64(&mut self, n
: f64) {
851 self.put_u64(n
.to_bits());
854 /// Writes an IEEE754 double-precision (8 bytes) floating point number to
855 /// `self` in little-endian byte order.
857 /// The current position is advanced by 8.
862 /// use bytes::BufMut;
864 /// let mut buf = vec![];
865 /// buf.put_f64_le(1.2f64);
866 /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
871 /// This function panics if there is not enough remaining capacity in
873 fn put_f64_le(&mut self, n
: f64) {
874 self.put_u64_le(n
.to_bits());
877 /// Creates an adaptor which can write at most `limit` bytes to `self`.
882 /// use bytes::BufMut;
884 /// let arr = &mut [0u8; 128][..];
885 /// assert_eq!(arr.remaining_mut(), 128);
887 /// let dst = arr.limit(10);
888 /// assert_eq!(dst.remaining_mut(), 10);
890 fn limit(self, limit
: usize) -> Limit
<Self>
894 limit
::new(self, limit
)
897 /// Creates an adaptor which implements the `Write` trait for `self`.
899 /// This function returns a new value which implements `Write` by adapting
900 /// the `Write` trait functions to the `BufMut` trait functions. Given that
901 /// `BufMut` operations are infallible, none of the `Write` functions will
902 /// return with `Err`.
907 /// use bytes::BufMut;
908 /// use std::io::Write;
910 /// let mut buf = vec![].writer();
912 /// let num = buf.write(&b"hello world"[..]).unwrap();
913 /// assert_eq!(11, num);
915 /// let buf = buf.into_inner();
917 /// assert_eq!(*buf, b"hello world"[..]);
919 #[cfg(feature = "std")]
920 fn writer(self) -> Writer
<Self>
927 /// Creates an adapter which will chain this buffer with another.
929 /// The returned `BufMut` instance will first write to all bytes from
930 /// `self`. Afterwards, it will write to `next`.
935 /// use bytes::BufMut;
937 /// let mut a = [0u8; 5];
938 /// let mut b = [0u8; 6];
940 /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]);
942 /// chain.put_slice(b"hello world");
944 /// assert_eq!(&a[..], b"hello");
945 /// assert_eq!(&b[..], b" world");
947 fn chain_mut
<U
: BufMut
>(self, next
: U
) -> Chain
<Self, U
>
951 Chain
::new(self, next
)
955 macro_rules
! deref_forward_bufmut
{
957 fn remaining_mut(&self) -> usize {
958 (**self).remaining_mut()
961 fn chunk_mut(&mut self) -> &mut UninitSlice
{
965 unsafe fn advance_mut(&mut self, cnt
: usize) {
966 (**self).advance_mut(cnt
)
969 fn put_slice(&mut self, src
: &[u8]) {
970 (**self).put_slice(src
)
973 fn put_u8(&mut self, n
: u8) {
977 fn put_i8(&mut self, n
: i8) {
981 fn put_u16(&mut self, n
: u16) {
985 fn put_u16_le(&mut self, n
: u16) {
986 (**self).put_u16_le(n
)
989 fn put_i16(&mut self, n
: i16) {
993 fn put_i16_le(&mut self, n
: i16) {
994 (**self).put_i16_le(n
)
997 fn put_u32(&mut self, n
: u32) {
1001 fn put_u32_le(&mut self, n
: u32) {
1002 (**self).put_u32_le(n
)
1005 fn put_i32(&mut self, n
: i32) {
1009 fn put_i32_le(&mut self, n
: i32) {
1010 (**self).put_i32_le(n
)
1013 fn put_u64(&mut self, n
: u64) {
1017 fn put_u64_le(&mut self, n
: u64) {
1018 (**self).put_u64_le(n
)
1021 fn put_i64(&mut self, n
: i64) {
1025 fn put_i64_le(&mut self, n
: i64) {
1026 (**self).put_i64_le(n
)
1031 unsafe impl<T
: BufMut
+ ?Sized
> BufMut
for &mut T
{
1032 deref_forward_bufmut
!();
1035 unsafe impl<T
: BufMut
+ ?Sized
> BufMut
for Box
<T
> {
1036 deref_forward_bufmut
!();
1039 unsafe impl BufMut
for &mut [u8] {
1041 fn remaining_mut(&self) -> usize {
1046 fn chunk_mut(&mut self) -> &mut UninitSlice
{
1047 // UninitSlice is repr(transparent), so safe to transmute
1048 unsafe { &mut *(*self as *mut [u8] as *mut _) }
1052 unsafe fn advance_mut(&mut self, cnt
: usize) {
1053 // Lifetime dance taken from `impl Write for &mut [u8]`.
1054 let (_
, b
) = core
::mem
::replace(self, &mut []).split_at_mut(cnt
);
1059 fn put_slice(&mut self, src
: &[u8]) {
1060 self[..src
.len()].copy_from_slice(src
);
1062 self.advance_mut(src
.len());
1066 fn put_bytes(&mut self, val
: u8, cnt
: usize) {
1067 assert
!(self.remaining_mut() >= cnt
);
1069 ptr
::write_bytes(self.as_mut_ptr(), val
, cnt
);
1070 self.advance_mut(cnt
);
1075 unsafe impl BufMut
for Vec
<u8> {
1077 fn remaining_mut(&self) -> usize {
1078 // A vector can never have more than isize::MAX bytes
1079 core
::isize::MAX
as usize - self.len()
1083 unsafe fn advance_mut(&mut self, cnt
: usize) {
1084 let len
= self.len();
1085 let remaining
= self.capacity() - len
;
1089 "cannot advance past `remaining_mut`: {:?} <= {:?}",
1094 self.set_len(len
+ cnt
);
1098 fn chunk_mut(&mut self) -> &mut UninitSlice
{
1099 if self.capacity() == self.len() {
1100 self.reserve(64); // Grow the vec
1103 let cap
= self.capacity();
1104 let len
= self.len();
1106 let ptr
= self.as_mut_ptr();
1107 unsafe { &mut UninitSlice::from_raw_parts_mut(ptr, cap)[len..] }
1110 // Specialize these methods so they can skip checking `remaining_mut`
1111 // and `advance_mut`.
1112 fn put
<T
: super::Buf
>(&mut self, mut src
: T
)
1116 // In case the src isn't contiguous, reserve upfront
1117 self.reserve(src
.remaining());
1119 while src
.has_remaining() {
1122 // a block to contain the src.bytes() borrow
1124 let s
= src
.chunk();
1126 self.extend_from_slice(s
);
1134 fn put_slice(&mut self, src
: &[u8]) {
1135 self.extend_from_slice(src
);
1138 fn put_bytes(&mut self, val
: u8, cnt
: usize) {
1139 let new_len
= self.len().checked_add(cnt
).unwrap();
1140 self.resize(new_len
, val
);
1144 // The existence of this function makes the compiler catch if the BufMut
1145 // trait is "object-safe" or not.
1146 fn _assert_trait_object(_b
: &dyn BufMut
) {}