1 use crate::buf::{limit, Chain, Limit, UninitSlice};
2 #[cfg(feature = "std")]
3 use crate::buf::{writer, Writer};
4 use crate::{panic_advance, panic_does_not_fit};
5 
6 use core::{mem, ptr, usize};
7 
8 use alloc::{boxed::Box, vec::Vec};
9 
10 /// A trait for values that provide sequential write access to bytes.
11 ///
12 /// Write bytes to a buffer
13 ///
14 /// A buffer stores bytes in memory such that write operations are infallible.
15 /// The underlying storage may or may not be in contiguous memory. A `BufMut`
16 /// value is a cursor into the buffer. Writing to `BufMut` advances the cursor
17 /// position.
18 ///
19 /// The simplest `BufMut` is a `Vec<u8>`.
20 ///
21 /// ```
22 /// use bytes::BufMut;
23 ///
24 /// let mut buf = vec![];
25 ///
26 /// buf.put(&b"hello world"[..]);
27 ///
28 /// assert_eq!(buf, b"hello world");
29 /// ```
30 pub unsafe trait BufMut {
31     /// Returns the number of bytes that can be written from the current
32     /// position until the end of the buffer is reached.
33     ///
34     /// This value is greater than or equal to the length of the slice returned
35     /// by `chunk_mut()`.
36     ///
37     /// Writing to a `BufMut` may involve allocating more memory on the fly.
38     /// Implementations may fail before reaching the number of bytes indicated
39     /// by this method if they encounter an allocation failure.
40     ///
41     /// # Examples
42     ///
43     /// ```
44     /// use bytes::BufMut;
45     ///
46     /// let mut dst = [0; 10];
47     /// let mut buf = &mut dst[..];
48     ///
49     /// let original_remaining = buf.remaining_mut();
50     /// buf.put(&b"hello"[..]);
51     ///
52     /// assert_eq!(original_remaining - 5, buf.remaining_mut());
53     /// ```
54     ///
55     /// # Implementer notes
56     ///
57     /// Implementations of `remaining_mut` should ensure that the return value
58     /// does not change unless a call is made to `advance_mut` or any other
59     /// function that is documented to change the `BufMut`'s current position.
60     ///
61     /// # Note
62     ///
63     /// `remaining_mut` may return value smaller than actual available space.
remaining_mut(&self) -> usize64     fn remaining_mut(&self) -> usize;
65 
66     /// Advance the internal cursor of the BufMut
67     ///
68     /// The next call to `chunk_mut` will return a slice starting `cnt` bytes
69     /// further into the underlying buffer.
70     ///
71     /// # Safety
72     ///
73     /// The caller must ensure that the next `cnt` bytes of `chunk` are
74     /// initialized.
75     ///
76     /// # Examples
77     ///
78     /// ```
79     /// use bytes::BufMut;
80     ///
81     /// let mut buf = Vec::with_capacity(16);
82     ///
83     /// // Write some data
84     /// buf.chunk_mut()[0..2].copy_from_slice(b"he");
85     /// unsafe { buf.advance_mut(2) };
86     ///
87     /// // write more bytes
88     /// buf.chunk_mut()[0..3].copy_from_slice(b"llo");
89     ///
90     /// unsafe { buf.advance_mut(3); }
91     ///
92     /// assert_eq!(5, buf.len());
93     /// assert_eq!(buf, b"hello");
94     /// ```
95     ///
96     /// # Panics
97     ///
98     /// This function **may** panic if `cnt > self.remaining_mut()`.
99     ///
100     /// # Implementer notes
101     ///
102     /// It is recommended for implementations of `advance_mut` to panic if
103     /// `cnt > self.remaining_mut()`. If the implementation does not panic,
104     /// the call must behave as if `cnt == self.remaining_mut()`.
105     ///
106     /// A call with `cnt == 0` should never panic and be a no-op.
advance_mut(&mut self, cnt: usize)107     unsafe fn advance_mut(&mut self, cnt: usize);
108 
109     /// Returns true if there is space in `self` for more bytes.
110     ///
111     /// This is equivalent to `self.remaining_mut() != 0`.
112     ///
113     /// # Examples
114     ///
115     /// ```
116     /// use bytes::BufMut;
117     ///
118     /// let mut dst = [0; 5];
119     /// let mut buf = &mut dst[..];
120     ///
121     /// assert!(buf.has_remaining_mut());
122     ///
123     /// buf.put(&b"hello"[..]);
124     ///
125     /// assert!(!buf.has_remaining_mut());
126     /// ```
127     #[inline]
has_remaining_mut(&self) -> bool128     fn has_remaining_mut(&self) -> bool {
129         self.remaining_mut() > 0
130     }
131 
132     /// Returns a mutable slice starting at the current BufMut position and of
133     /// length between 0 and `BufMut::remaining_mut()`. Note that this *can* be shorter than the
134     /// whole remainder of the buffer (this allows non-continuous implementation).
135     ///
136     /// This is a lower level function. Most operations are done with other
137     /// functions.
138     ///
139     /// The returned byte slice may represent uninitialized memory.
140     ///
141     /// # Examples
142     ///
143     /// ```
144     /// use bytes::BufMut;
145     ///
146     /// let mut buf = Vec::with_capacity(16);
147     ///
148     /// unsafe {
149     ///     // MaybeUninit::as_mut_ptr
150     ///     buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
151     ///     buf.chunk_mut()[1..].as_mut_ptr().write(b'e');
152     ///
153     ///     buf.advance_mut(2);
154     ///
155     ///     buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
156     ///     buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
157     ///     buf.chunk_mut()[2..].as_mut_ptr().write(b'o');
158     ///
159     ///     buf.advance_mut(3);
160     /// }
161     ///
162     /// assert_eq!(5, buf.len());
163     /// assert_eq!(buf, b"hello");
164     /// ```
165     ///
166     /// # Implementer notes
167     ///
168     /// This function should never panic. `chunk_mut()` should return an empty
169     /// slice **if and only if** `remaining_mut()` returns 0. In other words,
170     /// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
171     /// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
172     /// return an empty slice.
173     ///
174     /// This function may trigger an out-of-memory abort if it tries to allocate
175     /// memory and fails to do so.
176     // The `chunk_mut` method was previously called `bytes_mut`. This alias makes the
177     // rename more easily discoverable.
178     #[cfg_attr(docsrs, doc(alias = "bytes_mut"))]
chunk_mut(&mut self) -> &mut UninitSlice179     fn chunk_mut(&mut self) -> &mut UninitSlice;
180 
181     /// Transfer bytes into `self` from `src` and advance the cursor by the
182     /// number of bytes written.
183     ///
184     /// # Examples
185     ///
186     /// ```
187     /// use bytes::BufMut;
188     ///
189     /// let mut buf = vec![];
190     ///
191     /// buf.put_u8(b'h');
192     /// buf.put(&b"ello"[..]);
193     /// buf.put(&b" world"[..]);
194     ///
195     /// assert_eq!(buf, b"hello world");
196     /// ```
197     ///
198     /// # Panics
199     ///
200     /// Panics if `self` does not have enough capacity to contain `src`.
201     #[inline]
put<T: super::Buf>(&mut self, mut src: T) where Self: Sized,202     fn put<T: super::Buf>(&mut self, mut src: T)
203     where
204         Self: Sized,
205     {
206         if self.remaining_mut() < src.remaining() {
207             panic_advance(src.remaining(), self.remaining_mut());
208         }
209 
210         while src.has_remaining() {
211             let s = src.chunk();
212             let d = self.chunk_mut();
213             let cnt = usize::min(s.len(), d.len());
214 
215             d[..cnt].copy_from_slice(&s[..cnt]);
216 
217             // SAFETY: We just initialized `cnt` bytes in `self`.
218             unsafe { self.advance_mut(cnt) };
219             src.advance(cnt);
220         }
221     }
222 
223     /// Transfer bytes into `self` from `src` and advance the cursor by the
224     /// number of bytes written.
225     ///
226     /// `self` must have enough remaining capacity to contain all of `src`.
227     ///
228     /// ```
229     /// use bytes::BufMut;
230     ///
231     /// let mut dst = [0; 6];
232     ///
233     /// {
234     ///     let mut buf = &mut dst[..];
235     ///     buf.put_slice(b"hello");
236     ///
237     ///     assert_eq!(1, buf.remaining_mut());
238     /// }
239     ///
240     /// assert_eq!(b"hello\0", &dst);
241     /// ```
242     #[inline]
put_slice(&mut self, mut src: &[u8])243     fn put_slice(&mut self, mut src: &[u8]) {
244         if self.remaining_mut() < src.len() {
245             panic_advance(src.len(), self.remaining_mut());
246         }
247 
248         while !src.is_empty() {
249             let dst = self.chunk_mut();
250             let cnt = usize::min(src.len(), dst.len());
251 
252             dst[..cnt].copy_from_slice(&src[..cnt]);
253             src = &src[cnt..];
254 
255             // SAFETY: We just initialized `cnt` bytes in `self`.
256             unsafe { self.advance_mut(cnt) };
257         }
258     }
259 
260     /// Put `cnt` bytes `val` into `self`.
261     ///
262     /// Logically equivalent to calling `self.put_u8(val)` `cnt` times, but may work faster.
263     ///
264     /// `self` must have at least `cnt` remaining capacity.
265     ///
266     /// ```
267     /// use bytes::BufMut;
268     ///
269     /// let mut dst = [0; 6];
270     ///
271     /// {
272     ///     let mut buf = &mut dst[..];
273     ///     buf.put_bytes(b'a', 4);
274     ///
275     ///     assert_eq!(2, buf.remaining_mut());
276     /// }
277     ///
278     /// assert_eq!(b"aaaa\0\0", &dst);
279     /// ```
280     ///
281     /// # Panics
282     ///
283     /// This function panics if there is not enough remaining capacity in
284     /// `self`.
285     #[inline]
put_bytes(&mut self, val: u8, mut cnt: usize)286     fn put_bytes(&mut self, val: u8, mut cnt: usize) {
287         if self.remaining_mut() < cnt {
288             panic_advance(cnt, self.remaining_mut());
289         }
290 
291         while cnt > 0 {
292             let dst = self.chunk_mut();
293             let dst_len = usize::min(dst.len(), cnt);
294             // SAFETY: The pointer is valid for `dst_len <= dst.len()` bytes.
295             unsafe { core::ptr::write_bytes(dst.as_mut_ptr(), val, dst_len) };
296             // SAFETY: We just initialized `dst_len` bytes in `self`.
297             unsafe { self.advance_mut(dst_len) };
298             cnt -= dst_len;
299         }
300     }
301 
302     /// Writes an unsigned 8 bit integer to `self`.
303     ///
304     /// The current position is advanced by 1.
305     ///
306     /// # Examples
307     ///
308     /// ```
309     /// use bytes::BufMut;
310     ///
311     /// let mut buf = vec![];
312     /// buf.put_u8(0x01);
313     /// assert_eq!(buf, b"\x01");
314     /// ```
315     ///
316     /// # Panics
317     ///
318     /// This function panics if there is not enough remaining capacity in
319     /// `self`.
320     #[inline]
put_u8(&mut self, n: u8)321     fn put_u8(&mut self, n: u8) {
322         let src = [n];
323         self.put_slice(&src);
324     }
325 
326     /// Writes a signed 8 bit integer to `self`.
327     ///
328     /// The current position is advanced by 1.
329     ///
330     /// # Examples
331     ///
332     /// ```
333     /// use bytes::BufMut;
334     ///
335     /// let mut buf = vec![];
336     /// buf.put_i8(0x01);
337     /// assert_eq!(buf, b"\x01");
338     /// ```
339     ///
340     /// # Panics
341     ///
342     /// This function panics if there is not enough remaining capacity in
343     /// `self`.
344     #[inline]
put_i8(&mut self, n: i8)345     fn put_i8(&mut self, n: i8) {
346         let src = [n as u8];
347         self.put_slice(&src)
348     }
349 
350     /// Writes an unsigned 16 bit integer to `self` in big-endian byte order.
351     ///
352     /// The current position is advanced by 2.
353     ///
354     /// # Examples
355     ///
356     /// ```
357     /// use bytes::BufMut;
358     ///
359     /// let mut buf = vec![];
360     /// buf.put_u16(0x0809);
361     /// assert_eq!(buf, b"\x08\x09");
362     /// ```
363     ///
364     /// # Panics
365     ///
366     /// This function panics if there is not enough remaining capacity in
367     /// `self`.
368     #[inline]
put_u16(&mut self, n: u16)369     fn put_u16(&mut self, n: u16) {
370         self.put_slice(&n.to_be_bytes())
371     }
372 
373     /// Writes an unsigned 16 bit integer to `self` in little-endian byte order.
374     ///
375     /// The current position is advanced by 2.
376     ///
377     /// # Examples
378     ///
379     /// ```
380     /// use bytes::BufMut;
381     ///
382     /// let mut buf = vec![];
383     /// buf.put_u16_le(0x0809);
384     /// assert_eq!(buf, b"\x09\x08");
385     /// ```
386     ///
387     /// # Panics
388     ///
389     /// This function panics if there is not enough remaining capacity in
390     /// `self`.
391     #[inline]
put_u16_le(&mut self, n: u16)392     fn put_u16_le(&mut self, n: u16) {
393         self.put_slice(&n.to_le_bytes())
394     }
395 
396     /// Writes an unsigned 16 bit integer to `self` in native-endian byte order.
397     ///
398     /// The current position is advanced by 2.
399     ///
400     /// # Examples
401     ///
402     /// ```
403     /// use bytes::BufMut;
404     ///
405     /// let mut buf = vec![];
406     /// buf.put_u16_ne(0x0809);
407     /// if cfg!(target_endian = "big") {
408     ///     assert_eq!(buf, b"\x08\x09");
409     /// } else {
410     ///     assert_eq!(buf, b"\x09\x08");
411     /// }
412     /// ```
413     ///
414     /// # Panics
415     ///
416     /// This function panics if there is not enough remaining capacity in
417     /// `self`.
418     #[inline]
put_u16_ne(&mut self, n: u16)419     fn put_u16_ne(&mut self, n: u16) {
420         self.put_slice(&n.to_ne_bytes())
421     }
422 
423     /// Writes a signed 16 bit integer to `self` in big-endian byte order.
424     ///
425     /// The current position is advanced by 2.
426     ///
427     /// # Examples
428     ///
429     /// ```
430     /// use bytes::BufMut;
431     ///
432     /// let mut buf = vec![];
433     /// buf.put_i16(0x0809);
434     /// assert_eq!(buf, b"\x08\x09");
435     /// ```
436     ///
437     /// # Panics
438     ///
439     /// This function panics if there is not enough remaining capacity in
440     /// `self`.
441     #[inline]
put_i16(&mut self, n: i16)442     fn put_i16(&mut self, n: i16) {
443         self.put_slice(&n.to_be_bytes())
444     }
445 
446     /// Writes a signed 16 bit integer to `self` in little-endian byte order.
447     ///
448     /// The current position is advanced by 2.
449     ///
450     /// # Examples
451     ///
452     /// ```
453     /// use bytes::BufMut;
454     ///
455     /// let mut buf = vec![];
456     /// buf.put_i16_le(0x0809);
457     /// assert_eq!(buf, b"\x09\x08");
458     /// ```
459     ///
460     /// # Panics
461     ///
462     /// This function panics if there is not enough remaining capacity in
463     /// `self`.
464     #[inline]
put_i16_le(&mut self, n: i16)465     fn put_i16_le(&mut self, n: i16) {
466         self.put_slice(&n.to_le_bytes())
467     }
468 
469     /// Writes a signed 16 bit integer to `self` in native-endian byte order.
470     ///
471     /// The current position is advanced by 2.
472     ///
473     /// # Examples
474     ///
475     /// ```
476     /// use bytes::BufMut;
477     ///
478     /// let mut buf = vec![];
479     /// buf.put_i16_ne(0x0809);
480     /// if cfg!(target_endian = "big") {
481     ///     assert_eq!(buf, b"\x08\x09");
482     /// } else {
483     ///     assert_eq!(buf, b"\x09\x08");
484     /// }
485     /// ```
486     ///
487     /// # Panics
488     ///
489     /// This function panics if there is not enough remaining capacity in
490     /// `self`.
491     #[inline]
put_i16_ne(&mut self, n: i16)492     fn put_i16_ne(&mut self, n: i16) {
493         self.put_slice(&n.to_ne_bytes())
494     }
495 
496     /// Writes an unsigned 32 bit integer to `self` in big-endian byte order.
497     ///
498     /// The current position is advanced by 4.
499     ///
500     /// # Examples
501     ///
502     /// ```
503     /// use bytes::BufMut;
504     ///
505     /// let mut buf = vec![];
506     /// buf.put_u32(0x0809A0A1);
507     /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
508     /// ```
509     ///
510     /// # Panics
511     ///
512     /// This function panics if there is not enough remaining capacity in
513     /// `self`.
514     #[inline]
put_u32(&mut self, n: u32)515     fn put_u32(&mut self, n: u32) {
516         self.put_slice(&n.to_be_bytes())
517     }
518 
519     /// Writes an unsigned 32 bit integer to `self` in little-endian byte order.
520     ///
521     /// The current position is advanced by 4.
522     ///
523     /// # Examples
524     ///
525     /// ```
526     /// use bytes::BufMut;
527     ///
528     /// let mut buf = vec![];
529     /// buf.put_u32_le(0x0809A0A1);
530     /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
531     /// ```
532     ///
533     /// # Panics
534     ///
535     /// This function panics if there is not enough remaining capacity in
536     /// `self`.
537     #[inline]
put_u32_le(&mut self, n: u32)538     fn put_u32_le(&mut self, n: u32) {
539         self.put_slice(&n.to_le_bytes())
540     }
541 
542     /// Writes an unsigned 32 bit integer to `self` in native-endian byte order.
543     ///
544     /// The current position is advanced by 4.
545     ///
546     /// # Examples
547     ///
548     /// ```
549     /// use bytes::BufMut;
550     ///
551     /// let mut buf = vec![];
552     /// buf.put_u32_ne(0x0809A0A1);
553     /// if cfg!(target_endian = "big") {
554     ///     assert_eq!(buf, b"\x08\x09\xA0\xA1");
555     /// } else {
556     ///     assert_eq!(buf, b"\xA1\xA0\x09\x08");
557     /// }
558     /// ```
559     ///
560     /// # Panics
561     ///
562     /// This function panics if there is not enough remaining capacity in
563     /// `self`.
564     #[inline]
put_u32_ne(&mut self, n: u32)565     fn put_u32_ne(&mut self, n: u32) {
566         self.put_slice(&n.to_ne_bytes())
567     }
568 
569     /// Writes a signed 32 bit integer to `self` in big-endian byte order.
570     ///
571     /// The current position is advanced by 4.
572     ///
573     /// # Examples
574     ///
575     /// ```
576     /// use bytes::BufMut;
577     ///
578     /// let mut buf = vec![];
579     /// buf.put_i32(0x0809A0A1);
580     /// assert_eq!(buf, b"\x08\x09\xA0\xA1");
581     /// ```
582     ///
583     /// # Panics
584     ///
585     /// This function panics if there is not enough remaining capacity in
586     /// `self`.
587     #[inline]
put_i32(&mut self, n: i32)588     fn put_i32(&mut self, n: i32) {
589         self.put_slice(&n.to_be_bytes())
590     }
591 
592     /// Writes a signed 32 bit integer to `self` in little-endian byte order.
593     ///
594     /// The current position is advanced by 4.
595     ///
596     /// # Examples
597     ///
598     /// ```
599     /// use bytes::BufMut;
600     ///
601     /// let mut buf = vec![];
602     /// buf.put_i32_le(0x0809A0A1);
603     /// assert_eq!(buf, b"\xA1\xA0\x09\x08");
604     /// ```
605     ///
606     /// # Panics
607     ///
608     /// This function panics if there is not enough remaining capacity in
609     /// `self`.
610     #[inline]
put_i32_le(&mut self, n: i32)611     fn put_i32_le(&mut self, n: i32) {
612         self.put_slice(&n.to_le_bytes())
613     }
614 
615     /// Writes a signed 32 bit integer to `self` in native-endian byte order.
616     ///
617     /// The current position is advanced by 4.
618     ///
619     /// # Examples
620     ///
621     /// ```
622     /// use bytes::BufMut;
623     ///
624     /// let mut buf = vec![];
625     /// buf.put_i32_ne(0x0809A0A1);
626     /// if cfg!(target_endian = "big") {
627     ///     assert_eq!(buf, b"\x08\x09\xA0\xA1");
628     /// } else {
629     ///     assert_eq!(buf, b"\xA1\xA0\x09\x08");
630     /// }
631     /// ```
632     ///
633     /// # Panics
634     ///
635     /// This function panics if there is not enough remaining capacity in
636     /// `self`.
637     #[inline]
put_i32_ne(&mut self, n: i32)638     fn put_i32_ne(&mut self, n: i32) {
639         self.put_slice(&n.to_ne_bytes())
640     }
641 
642     /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order.
643     ///
644     /// The current position is advanced by 8.
645     ///
646     /// # Examples
647     ///
648     /// ```
649     /// use bytes::BufMut;
650     ///
651     /// let mut buf = vec![];
652     /// buf.put_u64(0x0102030405060708);
653     /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
654     /// ```
655     ///
656     /// # Panics
657     ///
658     /// This function panics if there is not enough remaining capacity in
659     /// `self`.
660     #[inline]
put_u64(&mut self, n: u64)661     fn put_u64(&mut self, n: u64) {
662         self.put_slice(&n.to_be_bytes())
663     }
664 
665     /// Writes an unsigned 64 bit integer to `self` in little-endian byte order.
666     ///
667     /// The current position is advanced by 8.
668     ///
669     /// # Examples
670     ///
671     /// ```
672     /// use bytes::BufMut;
673     ///
674     /// let mut buf = vec![];
675     /// buf.put_u64_le(0x0102030405060708);
676     /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
677     /// ```
678     ///
679     /// # Panics
680     ///
681     /// This function panics if there is not enough remaining capacity in
682     /// `self`.
683     #[inline]
put_u64_le(&mut self, n: u64)684     fn put_u64_le(&mut self, n: u64) {
685         self.put_slice(&n.to_le_bytes())
686     }
687 
688     /// Writes an unsigned 64 bit integer to `self` in native-endian byte order.
689     ///
690     /// The current position is advanced by 8.
691     ///
692     /// # Examples
693     ///
694     /// ```
695     /// use bytes::BufMut;
696     ///
697     /// let mut buf = vec![];
698     /// buf.put_u64_ne(0x0102030405060708);
699     /// if cfg!(target_endian = "big") {
700     ///     assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
701     /// } else {
702     ///     assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
703     /// }
704     /// ```
705     ///
706     /// # Panics
707     ///
708     /// This function panics if there is not enough remaining capacity in
709     /// `self`.
710     #[inline]
put_u64_ne(&mut self, n: u64)711     fn put_u64_ne(&mut self, n: u64) {
712         self.put_slice(&n.to_ne_bytes())
713     }
714 
715     /// Writes a signed 64 bit integer to `self` in the big-endian byte order.
716     ///
717     /// The current position is advanced by 8.
718     ///
719     /// # Examples
720     ///
721     /// ```
722     /// use bytes::BufMut;
723     ///
724     /// let mut buf = vec![];
725     /// buf.put_i64(0x0102030405060708);
726     /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
727     /// ```
728     ///
729     /// # Panics
730     ///
731     /// This function panics if there is not enough remaining capacity in
732     /// `self`.
733     #[inline]
put_i64(&mut self, n: i64)734     fn put_i64(&mut self, n: i64) {
735         self.put_slice(&n.to_be_bytes())
736     }
737 
738     /// Writes a signed 64 bit integer to `self` in little-endian byte order.
739     ///
740     /// The current position is advanced by 8.
741     ///
742     /// # Examples
743     ///
744     /// ```
745     /// use bytes::BufMut;
746     ///
747     /// let mut buf = vec![];
748     /// buf.put_i64_le(0x0102030405060708);
749     /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
750     /// ```
751     ///
752     /// # Panics
753     ///
754     /// This function panics if there is not enough remaining capacity in
755     /// `self`.
756     #[inline]
put_i64_le(&mut self, n: i64)757     fn put_i64_le(&mut self, n: i64) {
758         self.put_slice(&n.to_le_bytes())
759     }
760 
761     /// Writes a signed 64 bit integer to `self` in native-endian byte order.
762     ///
763     /// The current position is advanced by 8.
764     ///
765     /// # Examples
766     ///
767     /// ```
768     /// use bytes::BufMut;
769     ///
770     /// let mut buf = vec![];
771     /// buf.put_i64_ne(0x0102030405060708);
772     /// if cfg!(target_endian = "big") {
773     ///     assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
774     /// } else {
775     ///     assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
776     /// }
777     /// ```
778     ///
779     /// # Panics
780     ///
781     /// This function panics if there is not enough remaining capacity in
782     /// `self`.
783     #[inline]
put_i64_ne(&mut self, n: i64)784     fn put_i64_ne(&mut self, n: i64) {
785         self.put_slice(&n.to_ne_bytes())
786     }
787 
788     /// Writes an unsigned 128 bit integer to `self` in the big-endian byte order.
789     ///
790     /// The current position is advanced by 16.
791     ///
792     /// # Examples
793     ///
794     /// ```
795     /// use bytes::BufMut;
796     ///
797     /// let mut buf = vec![];
798     /// buf.put_u128(0x01020304050607080910111213141516);
799     /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
800     /// ```
801     ///
802     /// # Panics
803     ///
804     /// This function panics if there is not enough remaining capacity in
805     /// `self`.
806     #[inline]
put_u128(&mut self, n: u128)807     fn put_u128(&mut self, n: u128) {
808         self.put_slice(&n.to_be_bytes())
809     }
810 
811     /// Writes an unsigned 128 bit integer to `self` in little-endian byte order.
812     ///
813     /// The current position is advanced by 16.
814     ///
815     /// # Examples
816     ///
817     /// ```
818     /// use bytes::BufMut;
819     ///
820     /// let mut buf = vec![];
821     /// buf.put_u128_le(0x01020304050607080910111213141516);
822     /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
823     /// ```
824     ///
825     /// # Panics
826     ///
827     /// This function panics if there is not enough remaining capacity in
828     /// `self`.
829     #[inline]
put_u128_le(&mut self, n: u128)830     fn put_u128_le(&mut self, n: u128) {
831         self.put_slice(&n.to_le_bytes())
832     }
833 
834     /// Writes an unsigned 128 bit integer to `self` in native-endian byte order.
835     ///
836     /// The current position is advanced by 16.
837     ///
838     /// # Examples
839     ///
840     /// ```
841     /// use bytes::BufMut;
842     ///
843     /// let mut buf = vec![];
844     /// buf.put_u128_ne(0x01020304050607080910111213141516);
845     /// if cfg!(target_endian = "big") {
846     ///     assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
847     /// } else {
848     ///     assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
849     /// }
850     /// ```
851     ///
852     /// # Panics
853     ///
854     /// This function panics if there is not enough remaining capacity in
855     /// `self`.
856     #[inline]
put_u128_ne(&mut self, n: u128)857     fn put_u128_ne(&mut self, n: u128) {
858         self.put_slice(&n.to_ne_bytes())
859     }
860 
861     /// Writes a signed 128 bit integer to `self` in the big-endian byte order.
862     ///
863     /// The current position is advanced by 16.
864     ///
865     /// # Examples
866     ///
867     /// ```
868     /// use bytes::BufMut;
869     ///
870     /// let mut buf = vec![];
871     /// buf.put_i128(0x01020304050607080910111213141516);
872     /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
873     /// ```
874     ///
875     /// # Panics
876     ///
877     /// This function panics if there is not enough remaining capacity in
878     /// `self`.
879     #[inline]
put_i128(&mut self, n: i128)880     fn put_i128(&mut self, n: i128) {
881         self.put_slice(&n.to_be_bytes())
882     }
883 
884     /// Writes a signed 128 bit integer to `self` in little-endian byte order.
885     ///
886     /// The current position is advanced by 16.
887     ///
888     /// # Examples
889     ///
890     /// ```
891     /// use bytes::BufMut;
892     ///
893     /// let mut buf = vec![];
894     /// buf.put_i128_le(0x01020304050607080910111213141516);
895     /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
896     /// ```
897     ///
898     /// # Panics
899     ///
900     /// This function panics if there is not enough remaining capacity in
901     /// `self`.
902     #[inline]
put_i128_le(&mut self, n: i128)903     fn put_i128_le(&mut self, n: i128) {
904         self.put_slice(&n.to_le_bytes())
905     }
906 
907     /// Writes a signed 128 bit integer to `self` in native-endian byte order.
908     ///
909     /// The current position is advanced by 16.
910     ///
911     /// # Examples
912     ///
913     /// ```
914     /// use bytes::BufMut;
915     ///
916     /// let mut buf = vec![];
917     /// buf.put_i128_ne(0x01020304050607080910111213141516);
918     /// if cfg!(target_endian = "big") {
919     ///     assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
920     /// } else {
921     ///     assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
922     /// }
923     /// ```
924     ///
925     /// # Panics
926     ///
927     /// This function panics if there is not enough remaining capacity in
928     /// `self`.
929     #[inline]
put_i128_ne(&mut self, n: i128)930     fn put_i128_ne(&mut self, n: i128) {
931         self.put_slice(&n.to_ne_bytes())
932     }
933 
934     /// Writes an unsigned n-byte integer to `self` in big-endian byte order.
935     ///
936     /// The current position is advanced by `nbytes`.
937     ///
938     /// # Examples
939     ///
940     /// ```
941     /// use bytes::BufMut;
942     ///
943     /// let mut buf = vec![];
944     /// buf.put_uint(0x010203, 3);
945     /// assert_eq!(buf, b"\x01\x02\x03");
946     /// ```
947     ///
948     /// # Panics
949     ///
950     /// This function panics if there is not enough remaining capacity in
951     /// `self` or if `nbytes` is greater than 8.
952     #[inline]
put_uint(&mut self, n: u64, nbytes: usize)953     fn put_uint(&mut self, n: u64, nbytes: usize) {
954         let start = match mem::size_of_val(&n).checked_sub(nbytes) {
955             Some(start) => start,
956             None => panic_does_not_fit(nbytes, mem::size_of_val(&n)),
957         };
958 
959         self.put_slice(&n.to_be_bytes()[start..]);
960     }
961 
962     /// Writes an unsigned n-byte integer to `self` in the little-endian byte order.
963     ///
964     /// The current position is advanced by `nbytes`.
965     ///
966     /// # Examples
967     ///
968     /// ```
969     /// use bytes::BufMut;
970     ///
971     /// let mut buf = vec![];
972     /// buf.put_uint_le(0x010203, 3);
973     /// assert_eq!(buf, b"\x03\x02\x01");
974     /// ```
975     ///
976     /// # Panics
977     ///
978     /// This function panics if there is not enough remaining capacity in
979     /// `self` or if `nbytes` is greater than 8.
980     #[inline]
put_uint_le(&mut self, n: u64, nbytes: usize)981     fn put_uint_le(&mut self, n: u64, nbytes: usize) {
982         let slice = n.to_le_bytes();
983         let slice = match slice.get(..nbytes) {
984             Some(slice) => slice,
985             None => panic_does_not_fit(nbytes, slice.len()),
986         };
987 
988         self.put_slice(slice);
989     }
990 
991     /// Writes an unsigned n-byte integer to `self` in the native-endian byte order.
992     ///
993     /// The current position is advanced by `nbytes`.
994     ///
995     /// # Examples
996     ///
997     /// ```
998     /// use bytes::BufMut;
999     ///
1000     /// let mut buf = vec![];
1001     /// buf.put_uint_ne(0x010203, 3);
1002     /// if cfg!(target_endian = "big") {
1003     ///     assert_eq!(buf, b"\x01\x02\x03");
1004     /// } else {
1005     ///     assert_eq!(buf, b"\x03\x02\x01");
1006     /// }
1007     /// ```
1008     ///
1009     /// # Panics
1010     ///
1011     /// This function panics if there is not enough remaining capacity in
1012     /// `self` or if `nbytes` is greater than 8.
1013     #[inline]
put_uint_ne(&mut self, n: u64, nbytes: usize)1014     fn put_uint_ne(&mut self, n: u64, nbytes: usize) {
1015         if cfg!(target_endian = "big") {
1016             self.put_uint(n, nbytes)
1017         } else {
1018             self.put_uint_le(n, nbytes)
1019         }
1020     }
1021 
1022     /// Writes low `nbytes` of a signed integer to `self` in big-endian byte order.
1023     ///
1024     /// The current position is advanced by `nbytes`.
1025     ///
1026     /// # Examples
1027     ///
1028     /// ```
1029     /// use bytes::BufMut;
1030     ///
1031     /// let mut buf = vec![];
1032     /// buf.put_int(0x0504010203, 3);
1033     /// assert_eq!(buf, b"\x01\x02\x03");
1034     /// ```
1035     ///
1036     /// # Panics
1037     ///
1038     /// This function panics if there is not enough remaining capacity in
1039     /// `self` or if `nbytes` is greater than 8.
1040     #[inline]
put_int(&mut self, n: i64, nbytes: usize)1041     fn put_int(&mut self, n: i64, nbytes: usize) {
1042         let start = match mem::size_of_val(&n).checked_sub(nbytes) {
1043             Some(start) => start,
1044             None => panic_does_not_fit(nbytes, mem::size_of_val(&n)),
1045         };
1046 
1047         self.put_slice(&n.to_be_bytes()[start..]);
1048     }
1049 
1050     /// Writes low `nbytes` of a signed integer to `self` in little-endian byte order.
1051     ///
1052     /// The current position is advanced by `nbytes`.
1053     ///
1054     /// # Examples
1055     ///
1056     /// ```
1057     /// use bytes::BufMut;
1058     ///
1059     /// let mut buf = vec![];
1060     /// buf.put_int_le(0x0504010203, 3);
1061     /// assert_eq!(buf, b"\x03\x02\x01");
1062     /// ```
1063     ///
1064     /// # Panics
1065     ///
1066     /// This function panics if there is not enough remaining capacity in
1067     /// `self` or if `nbytes` is greater than 8.
1068     #[inline]
put_int_le(&mut self, n: i64, nbytes: usize)1069     fn put_int_le(&mut self, n: i64, nbytes: usize) {
1070         let slice = n.to_le_bytes();
1071         let slice = match slice.get(..nbytes) {
1072             Some(slice) => slice,
1073             None => panic_does_not_fit(nbytes, slice.len()),
1074         };
1075 
1076         self.put_slice(slice);
1077     }
1078 
1079     /// Writes low `nbytes` of a signed integer to `self` in native-endian byte order.
1080     ///
1081     /// The current position is advanced by `nbytes`.
1082     ///
1083     /// # Examples
1084     ///
1085     /// ```
1086     /// use bytes::BufMut;
1087     ///
1088     /// let mut buf = vec![];
1089     /// buf.put_int_ne(0x010203, 3);
1090     /// if cfg!(target_endian = "big") {
1091     ///     assert_eq!(buf, b"\x01\x02\x03");
1092     /// } else {
1093     ///     assert_eq!(buf, b"\x03\x02\x01");
1094     /// }
1095     /// ```
1096     ///
1097     /// # Panics
1098     ///
1099     /// This function panics if there is not enough remaining capacity in
1100     /// `self` or if `nbytes` is greater than 8.
1101     #[inline]
put_int_ne(&mut self, n: i64, nbytes: usize)1102     fn put_int_ne(&mut self, n: i64, nbytes: usize) {
1103         if cfg!(target_endian = "big") {
1104             self.put_int(n, nbytes)
1105         } else {
1106             self.put_int_le(n, nbytes)
1107         }
1108     }
1109 
1110     /// Writes an IEEE754 single-precision (4 bytes) floating point number to
1111     /// `self` in big-endian byte order.
1112     ///
1113     /// The current position is advanced by 4.
1114     ///
1115     /// # Examples
1116     ///
1117     /// ```
1118     /// use bytes::BufMut;
1119     ///
1120     /// let mut buf = vec![];
1121     /// buf.put_f32(1.2f32);
1122     /// assert_eq!(buf, b"\x3F\x99\x99\x9A");
1123     /// ```
1124     ///
1125     /// # Panics
1126     ///
1127     /// This function panics if there is not enough remaining capacity in
1128     /// `self`.
1129     #[inline]
put_f32(&mut self, n: f32)1130     fn put_f32(&mut self, n: f32) {
1131         self.put_u32(n.to_bits());
1132     }
1133 
1134     /// Writes an IEEE754 single-precision (4 bytes) floating point number to
1135     /// `self` in little-endian byte order.
1136     ///
1137     /// The current position is advanced by 4.
1138     ///
1139     /// # Examples
1140     ///
1141     /// ```
1142     /// use bytes::BufMut;
1143     ///
1144     /// let mut buf = vec![];
1145     /// buf.put_f32_le(1.2f32);
1146     /// assert_eq!(buf, b"\x9A\x99\x99\x3F");
1147     /// ```
1148     ///
1149     /// # Panics
1150     ///
1151     /// This function panics if there is not enough remaining capacity in
1152     /// `self`.
1153     #[inline]
put_f32_le(&mut self, n: f32)1154     fn put_f32_le(&mut self, n: f32) {
1155         self.put_u32_le(n.to_bits());
1156     }
1157 
1158     /// Writes an IEEE754 single-precision (4 bytes) floating point number to
1159     /// `self` in native-endian byte order.
1160     ///
1161     /// The current position is advanced by 4.
1162     ///
1163     /// # Examples
1164     ///
1165     /// ```
1166     /// use bytes::BufMut;
1167     ///
1168     /// let mut buf = vec![];
1169     /// buf.put_f32_ne(1.2f32);
1170     /// if cfg!(target_endian = "big") {
1171     ///     assert_eq!(buf, b"\x3F\x99\x99\x9A");
1172     /// } else {
1173     ///     assert_eq!(buf, b"\x9A\x99\x99\x3F");
1174     /// }
1175     /// ```
1176     ///
1177     /// # Panics
1178     ///
1179     /// This function panics if there is not enough remaining capacity in
1180     /// `self`.
1181     #[inline]
put_f32_ne(&mut self, n: f32)1182     fn put_f32_ne(&mut self, n: f32) {
1183         self.put_u32_ne(n.to_bits());
1184     }
1185 
1186     /// Writes an IEEE754 double-precision (8 bytes) floating point number to
1187     /// `self` in big-endian byte order.
1188     ///
1189     /// The current position is advanced by 8.
1190     ///
1191     /// # Examples
1192     ///
1193     /// ```
1194     /// use bytes::BufMut;
1195     ///
1196     /// let mut buf = vec![];
1197     /// buf.put_f64(1.2f64);
1198     /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
1199     /// ```
1200     ///
1201     /// # Panics
1202     ///
1203     /// This function panics if there is not enough remaining capacity in
1204     /// `self`.
1205     #[inline]
put_f64(&mut self, n: f64)1206     fn put_f64(&mut self, n: f64) {
1207         self.put_u64(n.to_bits());
1208     }
1209 
1210     /// Writes an IEEE754 double-precision (8 bytes) floating point number to
1211     /// `self` in little-endian byte order.
1212     ///
1213     /// The current position is advanced by 8.
1214     ///
1215     /// # Examples
1216     ///
1217     /// ```
1218     /// use bytes::BufMut;
1219     ///
1220     /// let mut buf = vec![];
1221     /// buf.put_f64_le(1.2f64);
1222     /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
1223     /// ```
1224     ///
1225     /// # Panics
1226     ///
1227     /// This function panics if there is not enough remaining capacity in
1228     /// `self`.
1229     #[inline]
put_f64_le(&mut self, n: f64)1230     fn put_f64_le(&mut self, n: f64) {
1231         self.put_u64_le(n.to_bits());
1232     }
1233 
1234     /// Writes an IEEE754 double-precision (8 bytes) floating point number to
1235     /// `self` in native-endian byte order.
1236     ///
1237     /// The current position is advanced by 8.
1238     ///
1239     /// # Examples
1240     ///
1241     /// ```
1242     /// use bytes::BufMut;
1243     ///
1244     /// let mut buf = vec![];
1245     /// buf.put_f64_ne(1.2f64);
1246     /// if cfg!(target_endian = "big") {
1247     ///     assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
1248     /// } else {
1249     ///     assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
1250     /// }
1251     /// ```
1252     ///
1253     /// # Panics
1254     ///
1255     /// This function panics if there is not enough remaining capacity in
1256     /// `self`.
1257     #[inline]
put_f64_ne(&mut self, n: f64)1258     fn put_f64_ne(&mut self, n: f64) {
1259         self.put_u64_ne(n.to_bits());
1260     }
1261 
1262     /// Creates an adaptor which can write at most `limit` bytes to `self`.
1263     ///
1264     /// # Examples
1265     ///
1266     /// ```
1267     /// use bytes::BufMut;
1268     ///
1269     /// let arr = &mut [0u8; 128][..];
1270     /// assert_eq!(arr.remaining_mut(), 128);
1271     ///
1272     /// let dst = arr.limit(10);
1273     /// assert_eq!(dst.remaining_mut(), 10);
1274     /// ```
1275     #[inline]
limit(self, limit: usize) -> Limit<Self> where Self: Sized,1276     fn limit(self, limit: usize) -> Limit<Self>
1277     where
1278         Self: Sized,
1279     {
1280         limit::new(self, limit)
1281     }
1282 
1283     /// Creates an adaptor which implements the `Write` trait for `self`.
1284     ///
1285     /// This function returns a new value which implements `Write` by adapting
1286     /// the `Write` trait functions to the `BufMut` trait functions. Given that
1287     /// `BufMut` operations are infallible, none of the `Write` functions will
1288     /// return with `Err`.
1289     ///
1290     /// # Examples
1291     ///
1292     /// ```
1293     /// use bytes::BufMut;
1294     /// use std::io::Write;
1295     ///
1296     /// let mut buf = vec![].writer();
1297     ///
1298     /// let num = buf.write(&b"hello world"[..]).unwrap();
1299     /// assert_eq!(11, num);
1300     ///
1301     /// let buf = buf.into_inner();
1302     ///
1303     /// assert_eq!(*buf, b"hello world"[..]);
1304     /// ```
1305     #[cfg(feature = "std")]
1306     #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1307     #[inline]
writer(self) -> Writer<Self> where Self: Sized,1308     fn writer(self) -> Writer<Self>
1309     where
1310         Self: Sized,
1311     {
1312         writer::new(self)
1313     }
1314 
1315     /// Creates an adapter which will chain this buffer with another.
1316     ///
1317     /// The returned `BufMut` instance will first write to all bytes from
1318     /// `self`. Afterwards, it will write to `next`.
1319     ///
1320     /// # Examples
1321     ///
1322     /// ```
1323     /// use bytes::BufMut;
1324     ///
1325     /// let mut a = [0u8; 5];
1326     /// let mut b = [0u8; 6];
1327     ///
1328     /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]);
1329     ///
1330     /// chain.put_slice(b"hello world");
1331     ///
1332     /// assert_eq!(&a[..], b"hello");
1333     /// assert_eq!(&b[..], b" world");
1334     /// ```
1335     #[inline]
chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U> where Self: Sized,1336     fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
1337     where
1338         Self: Sized,
1339     {
1340         Chain::new(self, next)
1341     }
1342 }
1343 
1344 macro_rules! deref_forward_bufmut {
1345     () => {
1346         #[inline]
1347         fn remaining_mut(&self) -> usize {
1348             (**self).remaining_mut()
1349         }
1350 
1351         #[inline]
1352         fn chunk_mut(&mut self) -> &mut UninitSlice {
1353             (**self).chunk_mut()
1354         }
1355 
1356         #[inline]
1357         unsafe fn advance_mut(&mut self, cnt: usize) {
1358             (**self).advance_mut(cnt)
1359         }
1360 
1361         #[inline]
1362         fn put_slice(&mut self, src: &[u8]) {
1363             (**self).put_slice(src)
1364         }
1365 
1366         #[inline]
1367         fn put_u8(&mut self, n: u8) {
1368             (**self).put_u8(n)
1369         }
1370 
1371         #[inline]
1372         fn put_i8(&mut self, n: i8) {
1373             (**self).put_i8(n)
1374         }
1375 
1376         #[inline]
1377         fn put_u16(&mut self, n: u16) {
1378             (**self).put_u16(n)
1379         }
1380 
1381         #[inline]
1382         fn put_u16_le(&mut self, n: u16) {
1383             (**self).put_u16_le(n)
1384         }
1385 
1386         #[inline]
1387         fn put_u16_ne(&mut self, n: u16) {
1388             (**self).put_u16_ne(n)
1389         }
1390 
1391         #[inline]
1392         fn put_i16(&mut self, n: i16) {
1393             (**self).put_i16(n)
1394         }
1395 
1396         #[inline]
1397         fn put_i16_le(&mut self, n: i16) {
1398             (**self).put_i16_le(n)
1399         }
1400 
1401         #[inline]
1402         fn put_i16_ne(&mut self, n: i16) {
1403             (**self).put_i16_ne(n)
1404         }
1405 
1406         #[inline]
1407         fn put_u32(&mut self, n: u32) {
1408             (**self).put_u32(n)
1409         }
1410 
1411         #[inline]
1412         fn put_u32_le(&mut self, n: u32) {
1413             (**self).put_u32_le(n)
1414         }
1415 
1416         #[inline]
1417         fn put_u32_ne(&mut self, n: u32) {
1418             (**self).put_u32_ne(n)
1419         }
1420 
1421         #[inline]
1422         fn put_i32(&mut self, n: i32) {
1423             (**self).put_i32(n)
1424         }
1425 
1426         #[inline]
1427         fn put_i32_le(&mut self, n: i32) {
1428             (**self).put_i32_le(n)
1429         }
1430 
1431         #[inline]
1432         fn put_i32_ne(&mut self, n: i32) {
1433             (**self).put_i32_ne(n)
1434         }
1435 
1436         #[inline]
1437         fn put_u64(&mut self, n: u64) {
1438             (**self).put_u64(n)
1439         }
1440 
1441         #[inline]
1442         fn put_u64_le(&mut self, n: u64) {
1443             (**self).put_u64_le(n)
1444         }
1445 
1446         #[inline]
1447         fn put_u64_ne(&mut self, n: u64) {
1448             (**self).put_u64_ne(n)
1449         }
1450 
1451         #[inline]
1452         fn put_i64(&mut self, n: i64) {
1453             (**self).put_i64(n)
1454         }
1455 
1456         #[inline]
1457         fn put_i64_le(&mut self, n: i64) {
1458             (**self).put_i64_le(n)
1459         }
1460 
1461         #[inline]
1462         fn put_i64_ne(&mut self, n: i64) {
1463             (**self).put_i64_ne(n)
1464         }
1465     };
1466 }
1467 
1468 unsafe impl<T: BufMut + ?Sized> BufMut for &mut T {
1469     deref_forward_bufmut!();
1470 }
1471 
1472 unsafe impl<T: BufMut + ?Sized> BufMut for Box<T> {
1473     deref_forward_bufmut!();
1474 }
1475 
1476 unsafe impl BufMut for &mut [u8] {
1477     #[inline]
remaining_mut(&self) -> usize1478     fn remaining_mut(&self) -> usize {
1479         self.len()
1480     }
1481 
1482     #[inline]
chunk_mut(&mut self) -> &mut UninitSlice1483     fn chunk_mut(&mut self) -> &mut UninitSlice {
1484         UninitSlice::new(self)
1485     }
1486 
1487     #[inline]
advance_mut(&mut self, cnt: usize)1488     unsafe fn advance_mut(&mut self, cnt: usize) {
1489         if self.len() < cnt {
1490             panic_advance(cnt, self.len());
1491         }
1492 
1493         // Lifetime dance taken from `impl Write for &mut [u8]`.
1494         let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
1495         *self = b;
1496     }
1497 
1498     #[inline]
put_slice(&mut self, src: &[u8])1499     fn put_slice(&mut self, src: &[u8]) {
1500         if self.len() < src.len() {
1501             panic_advance(src.len(), self.len());
1502         }
1503 
1504         self[..src.len()].copy_from_slice(src);
1505         // SAFETY: We just initialized `src.len()` bytes.
1506         unsafe { self.advance_mut(src.len()) };
1507     }
1508 
1509     #[inline]
put_bytes(&mut self, val: u8, cnt: usize)1510     fn put_bytes(&mut self, val: u8, cnt: usize) {
1511         if self.len() < cnt {
1512             panic_advance(cnt, self.len());
1513         }
1514 
1515         // SAFETY: We just checked that the pointer is valid for `cnt` bytes.
1516         unsafe {
1517             ptr::write_bytes(self.as_mut_ptr(), val, cnt);
1518             self.advance_mut(cnt);
1519         }
1520     }
1521 }
1522 
1523 unsafe impl BufMut for &mut [core::mem::MaybeUninit<u8>] {
1524     #[inline]
remaining_mut(&self) -> usize1525     fn remaining_mut(&self) -> usize {
1526         self.len()
1527     }
1528 
1529     #[inline]
chunk_mut(&mut self) -> &mut UninitSlice1530     fn chunk_mut(&mut self) -> &mut UninitSlice {
1531         UninitSlice::uninit(self)
1532     }
1533 
1534     #[inline]
advance_mut(&mut self, cnt: usize)1535     unsafe fn advance_mut(&mut self, cnt: usize) {
1536         if self.len() < cnt {
1537             panic_advance(cnt, self.len());
1538         }
1539 
1540         // Lifetime dance taken from `impl Write for &mut [u8]`.
1541         let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
1542         *self = b;
1543     }
1544 
1545     #[inline]
put_slice(&mut self, src: &[u8])1546     fn put_slice(&mut self, src: &[u8]) {
1547         if self.len() < src.len() {
1548             panic_advance(src.len(), self.len());
1549         }
1550 
1551         // SAFETY: We just checked that the pointer is valid for `src.len()` bytes.
1552         unsafe {
1553             ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr().cast(), src.len());
1554             self.advance_mut(src.len());
1555         }
1556     }
1557 
1558     #[inline]
put_bytes(&mut self, val: u8, cnt: usize)1559     fn put_bytes(&mut self, val: u8, cnt: usize) {
1560         if self.len() < cnt {
1561             panic_advance(cnt, self.len());
1562         }
1563 
1564         // SAFETY: We just checked that the pointer is valid for `cnt` bytes.
1565         unsafe {
1566             ptr::write_bytes(self.as_mut_ptr() as *mut u8, val, cnt);
1567             self.advance_mut(cnt);
1568         }
1569     }
1570 }
1571 
1572 unsafe impl BufMut for Vec<u8> {
1573     #[inline]
remaining_mut(&self) -> usize1574     fn remaining_mut(&self) -> usize {
1575         // A vector can never have more than isize::MAX bytes
1576         core::isize::MAX as usize - self.len()
1577     }
1578 
1579     #[inline]
advance_mut(&mut self, cnt: usize)1580     unsafe fn advance_mut(&mut self, cnt: usize) {
1581         let len = self.len();
1582         let remaining = self.capacity() - len;
1583 
1584         if remaining < cnt {
1585             panic_advance(cnt, remaining);
1586         }
1587 
1588         // Addition will not overflow since the sum is at most the capacity.
1589         self.set_len(len + cnt);
1590     }
1591 
1592     #[inline]
chunk_mut(&mut self) -> &mut UninitSlice1593     fn chunk_mut(&mut self) -> &mut UninitSlice {
1594         if self.capacity() == self.len() {
1595             self.reserve(64); // Grow the vec
1596         }
1597 
1598         let cap = self.capacity();
1599         let len = self.len();
1600 
1601         let ptr = self.as_mut_ptr();
1602         // SAFETY: Since `ptr` is valid for `cap` bytes, `ptr.add(len)` must be
1603         // valid for `cap - len` bytes. The subtraction will not underflow since
1604         // `len <= cap`.
1605         unsafe { UninitSlice::from_raw_parts_mut(ptr.add(len), cap - len) }
1606     }
1607 
1608     // Specialize these methods so they can skip checking `remaining_mut`
1609     // and `advance_mut`.
1610     #[inline]
put<T: super::Buf>(&mut self, mut src: T) where Self: Sized,1611     fn put<T: super::Buf>(&mut self, mut src: T)
1612     where
1613         Self: Sized,
1614     {
1615         // In case the src isn't contiguous, reserve upfront.
1616         self.reserve(src.remaining());
1617 
1618         while src.has_remaining() {
1619             let s = src.chunk();
1620             let l = s.len();
1621             self.extend_from_slice(s);
1622             src.advance(l);
1623         }
1624     }
1625 
1626     #[inline]
put_slice(&mut self, src: &[u8])1627     fn put_slice(&mut self, src: &[u8]) {
1628         self.extend_from_slice(src);
1629     }
1630 
1631     #[inline]
put_bytes(&mut self, val: u8, cnt: usize)1632     fn put_bytes(&mut self, val: u8, cnt: usize) {
1633         // If the addition overflows, then the `resize` will fail.
1634         let new_len = self.len().saturating_add(cnt);
1635         self.resize(new_len, val);
1636     }
1637 }
1638 
1639 // The existence of this function makes the compiler catch if the BufMut
1640 // trait is "object-safe" or not.
_assert_trait_object(_b: &dyn BufMut)1641 fn _assert_trait_object(_b: &dyn BufMut) {}
1642