Skip to main content

bytes/buf/
buf_impl.rs

1#[cfg(feature = "std")]
2use crate::buf::{reader, Reader};
3use crate::buf::{take, Chain, Take};
4#[cfg(feature = "std")]
5use crate::{min_u64_usize, saturating_sub_usize_u64};
6use crate::{panic_advance, panic_does_not_fit, TryGetError};
7
8#[cfg(feature = "std")]
9use std::io::IoSlice;
10
11use alloc::boxed::Box;
12
13macro_rules! buf_try_get_impl {
14    ($this:ident, $typ:tt::$conv:tt) => {{
15        const SIZE: usize = core::mem::size_of::<$typ>();
16
17        if $this.remaining() < SIZE {
18            return Err(TryGetError {
19                requested: SIZE,
20                available: $this.remaining(),
21            });
22        }
23
24        // try to convert directly from the bytes
25        // this Option<ret> trick is to avoid keeping a borrow on self
26        // when advance() is called (mut borrow) and to call bytes() only once
27        let ret = $this
28            .chunk()
29            .get(..SIZE)
30            .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
31
32        if let Some(ret) = ret {
33            // if the direct conversion was possible, advance and return
34            $this.advance(SIZE);
35            return Ok(ret);
36        } else {
37            // if not we copy the bytes in a temp buffer then convert
38            let mut buf = [0; SIZE];
39            $this.copy_to_slice(&mut buf); // (do the advance)
40            return Ok($typ::$conv(buf));
41        }
42    }};
43    (le => $this:ident, $typ:tt, $len_to_read:expr) => {{
44        const SIZE: usize = core::mem::size_of::<$typ>();
45
46        // The same trick as above does not improve the best case speed.
47        // It seems to be linked to the way the method is optimised by the compiler
48        let mut buf = [0; SIZE];
49
50        let subslice = match buf.get_mut(..$len_to_read) {
51            Some(subslice) => subslice,
52            None => panic_does_not_fit(SIZE, $len_to_read),
53        };
54
55        $this.try_copy_to_slice(subslice)?;
56        return Ok($typ::from_le_bytes(buf));
57    }};
58    (be => $this:ident, $typ:tt, $len_to_read:expr) => {{
59        const SIZE: usize = core::mem::size_of::<$typ>();
60
61        let slice_at = match SIZE.checked_sub($len_to_read) {
62            Some(slice_at) => slice_at,
63            None => panic_does_not_fit(SIZE, $len_to_read),
64        };
65
66        let mut buf = [0; SIZE];
67        $this.try_copy_to_slice(&mut buf[slice_at..])?;
68        return Ok($typ::from_be_bytes(buf));
69    }};
70}
71
72macro_rules! buf_get_impl {
73    ($this:ident, $typ:tt::$conv:tt) => {{
74        return (|| buf_try_get_impl!($this, $typ::$conv))()
75            .unwrap_or_else(|error| panic_advance(&error));
76    }};
77    (le => $this:ident, $typ:tt, $len_to_read:expr) => {{
78        return (|| buf_try_get_impl!(le => $this, $typ, $len_to_read))()
79            .unwrap_or_else(|error| panic_advance(&error));
80    }};
81    (be => $this:ident, $typ:tt, $len_to_read:expr) => {{
82        return (|| buf_try_get_impl!(be => $this, $typ, $len_to_read))()
83            .unwrap_or_else(|error| panic_advance(&error));
84    }};
85}
86
87// https://en.wikipedia.org/wiki/Sign_extension
88fn sign_extend(val: u64, nbytes: usize) -> i64 {
89    debug_assert!(nbytes <= 8, "nbytes must be in [0, 8]");
90    if nbytes == 0 {
91        // avoid `val << 64` panic
92        0
93    } else {
94        let shift = (8 - nbytes) * 8;
95        (val << shift) as i64 >> shift
96    }
97}
98
99/// Read bytes from a buffer.
100///
101/// A buffer stores bytes in memory such that read operations are infallible.
102/// The underlying storage may or may not be in contiguous memory. A `Buf` value
103/// is a cursor into the buffer. Reading from `Buf` advances the cursor
104/// position. It can be thought of as an efficient `Iterator` for collections of
105/// bytes.
106///
107/// The simplest `Buf` is a `&[u8]`.
108///
109/// ```
110/// use bytes::Buf;
111///
112/// let mut buf = &b"hello world"[..];
113///
114/// assert_eq!(b'h', buf.get_u8());
115/// assert_eq!(b'e', buf.get_u8());
116/// assert_eq!(b'l', buf.get_u8());
117///
118/// let mut rest = [0; 8];
119/// buf.copy_to_slice(&mut rest);
120///
121/// assert_eq!(&rest[..], &b"lo world"[..]);
122/// ```
123pub trait Buf {
124    /// Returns the number of bytes between the current position and the end of
125    /// the buffer.
126    ///
127    /// This value is greater than or equal to the length of the slice returned
128    /// by `chunk()`.
129    ///
130    /// # Examples
131    ///
132    /// ```
133    /// use bytes::Buf;
134    ///
135    /// let mut buf = &b"hello world"[..];
136    ///
137    /// assert_eq!(buf.remaining(), 11);
138    ///
139    /// buf.get_u8();
140    ///
141    /// assert_eq!(buf.remaining(), 10);
142    /// ```
143    ///
144    /// # Implementer notes
145    ///
146    /// Implementations of `remaining` should ensure that the return value does
147    /// not change unless a call is made to `advance` or any other function that
148    /// is documented to change the `Buf`'s current position.
149    fn remaining(&self) -> usize;
150
151    /// Returns a slice starting at the current position and of length between 0
152    /// and `Buf::remaining()`. Note that this *can* return a shorter slice (this
153    /// allows non-continuous internal representation).
154    ///
155    /// This is a lower level function. Most operations are done with other
156    /// functions.
157    ///
158    /// # Examples
159    ///
160    /// ```
161    /// use bytes::Buf;
162    ///
163    /// let mut buf = &b"hello world"[..];
164    ///
165    /// assert_eq!(buf.chunk(), &b"hello world"[..]);
166    ///
167    /// buf.advance(6);
168    ///
169    /// assert_eq!(buf.chunk(), &b"world"[..]);
170    /// ```
171    ///
172    /// # Implementer notes
173    ///
174    /// This function should never panic. `chunk()` should return an empty
175    /// slice **if and only if** `remaining()` returns 0. In other words,
176    /// `chunk()` returning an empty slice implies that `remaining()` will
177    /// return 0 and `remaining()` returning 0 implies that `chunk()` will
178    /// return an empty slice.
179    // The `chunk` method was previously called `bytes`. This alias makes the rename
180    // more easily discoverable.
181    #[cfg_attr(docsrs, doc(alias = "bytes"))]
182    fn chunk(&self) -> &[u8];
183
184    /// Fills `dst` with potentially multiple slices starting at `self`'s
185    /// current position.
186    ///
187    /// If the `Buf` is backed by disjoint slices of bytes, `chunk_vectored` enables
188    /// fetching more than one slice at once. `dst` is a slice of `IoSlice`
189    /// references, enabling the slice to be directly used with [`writev`]
190    /// without any further conversion. The sum of the lengths of all the
191    /// buffers written to `dst` will be less than or equal to `Buf::remaining()`.
192    ///
193    /// The entries in `dst` will be overwritten, but the data **contained** by
194    /// the slices **will not** be modified. The return value is the number of
195    /// slices written to `dst`. If `Buf::remaining()` is non-zero, then this
196    /// writes at least one non-empty slice to `dst`.
197    ///
198    /// This is a lower level function. Most operations are done with other
199    /// functions.
200    ///
201    /// # Implementer notes
202    ///
203    /// This function should never panic. Once the end of the buffer is reached,
204    /// i.e., `Buf::remaining` returns 0, calls to `chunk_vectored` must return 0
205    /// without mutating `dst`.
206    ///
207    /// Implementations should also take care to properly handle being called
208    /// with `dst` being a zero length slice.
209    ///
210    /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
211    #[cfg(feature = "std")]
212    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
213    fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
214        if dst.is_empty() {
215            return 0;
216        }
217
218        if self.has_remaining() {
219            dst[0] = IoSlice::new(self.chunk());
220            1
221        } else {
222            0
223        }
224    }
225
226    /// Advance the internal cursor of the Buf
227    ///
228    /// The next call to `chunk()` will return a slice starting `cnt` bytes
229    /// further into the underlying buffer.
230    ///
231    /// # Examples
232    ///
233    /// ```
234    /// use bytes::Buf;
235    ///
236    /// let mut buf = &b"hello world"[..];
237    ///
238    /// assert_eq!(buf.chunk(), &b"hello world"[..]);
239    ///
240    /// buf.advance(6);
241    ///
242    /// assert_eq!(buf.chunk(), &b"world"[..]);
243    /// ```
244    ///
245    /// # Panics
246    ///
247    /// This function **may** panic if `cnt > self.remaining()`.
248    ///
249    /// # Implementer notes
250    ///
251    /// It is recommended for implementations of `advance` to panic if `cnt >
252    /// self.remaining()`. If the implementation does not panic, the call must
253    /// behave as if `cnt == self.remaining()`.
254    ///
255    /// A call with `cnt == 0` should never panic and be a no-op.
256    fn advance(&mut self, cnt: usize);
257
258    /// Returns true if there are any more bytes to consume
259    ///
260    /// This is equivalent to `self.remaining() != 0`.
261    ///
262    /// # Examples
263    ///
264    /// ```
265    /// use bytes::Buf;
266    ///
267    /// let mut buf = &b"a"[..];
268    ///
269    /// assert!(buf.has_remaining());
270    ///
271    /// buf.get_u8();
272    ///
273    /// assert!(!buf.has_remaining());
274    /// ```
275    fn has_remaining(&self) -> bool {
276        self.remaining() > 0
277    }
278
279    /// Copies bytes from `self` into `dst`.
280    ///
281    /// The cursor is advanced by the number of bytes copied. `self` must have
282    /// enough remaining bytes to fill `dst`.
283    ///
284    /// # Examples
285    ///
286    /// ```
287    /// use bytes::Buf;
288    ///
289    /// let mut buf = &b"hello world"[..];
290    /// let mut dst = [0; 5];
291    ///
292    /// buf.copy_to_slice(&mut dst);
293    /// assert_eq!(&b"hello"[..], &dst);
294    /// assert_eq!(6, buf.remaining());
295    /// ```
296    ///
297    /// # Panics
298    ///
299    /// This function panics if `self.remaining() < dst.len()`.
300    fn copy_to_slice(&mut self, dst: &mut [u8]) {
301        self.try_copy_to_slice(dst)
302            .unwrap_or_else(|error| panic_advance(&error));
303    }
304
305    /// Gets an unsigned 8 bit integer from `self`.
306    ///
307    /// The current position is advanced by 1.
308    ///
309    /// # Examples
310    ///
311    /// ```
312    /// use bytes::Buf;
313    ///
314    /// let mut buf = &b"\x08 hello"[..];
315    /// assert_eq!(8, buf.get_u8());
316    /// ```
317    ///
318    /// # Panics
319    ///
320    /// This function panics if there is no more remaining data in `self`.
321    fn get_u8(&mut self) -> u8 {
322        if self.remaining() < 1 {
323            panic_advance(&TryGetError {
324                requested: 1,
325                available: 0,
326            })
327        }
328        let ret = self.chunk()[0];
329        self.advance(1);
330        ret
331    }
332
333    /// Gets a signed 8 bit integer from `self`.
334    ///
335    /// The current position is advanced by 1.
336    ///
337    /// # Examples
338    ///
339    /// ```
340    /// use bytes::Buf;
341    ///
342    /// let mut buf = &b"\x08 hello"[..];
343    /// assert_eq!(8, buf.get_i8());
344    /// ```
345    ///
346    /// # Panics
347    ///
348    /// This function panics if there is no more remaining data in `self`.
349    fn get_i8(&mut self) -> i8 {
350        if self.remaining() < 1 {
351            panic_advance(&TryGetError {
352                requested: 1,
353                available: 0,
354            });
355        }
356        let ret = self.chunk()[0] as i8;
357        self.advance(1);
358        ret
359    }
360
361    /// Gets an unsigned 16 bit integer from `self` in big-endian byte order.
362    ///
363    /// The current position is advanced by 2.
364    ///
365    /// # Examples
366    ///
367    /// ```
368    /// use bytes::Buf;
369    ///
370    /// let mut buf = &b"\x08\x09 hello"[..];
371    /// assert_eq!(0x0809, buf.get_u16());
372    /// ```
373    ///
374    /// # Panics
375    ///
376    /// This function panics if there is not enough remaining data in `self`.
377    fn get_u16(&mut self) -> u16 {
378        buf_get_impl!(self, u16::from_be_bytes);
379    }
380
381    /// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
382    ///
383    /// The current position is advanced by 2.
384    ///
385    /// # Examples
386    ///
387    /// ```
388    /// use bytes::Buf;
389    ///
390    /// let mut buf = &b"\x09\x08 hello"[..];
391    /// assert_eq!(0x0809, buf.get_u16_le());
392    /// ```
393    ///
394    /// # Panics
395    ///
396    /// This function panics if there is not enough remaining data in `self`.
397    fn get_u16_le(&mut self) -> u16 {
398        buf_get_impl!(self, u16::from_le_bytes);
399    }
400
401    /// Gets an unsigned 16 bit integer from `self` in native-endian byte order.
402    ///
403    /// The current position is advanced by 2.
404    ///
405    /// # Examples
406    ///
407    /// ```
408    /// use bytes::Buf;
409    ///
410    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
411    ///     true => b"\x08\x09 hello",
412    ///     false => b"\x09\x08 hello",
413    /// };
414    /// assert_eq!(0x0809, buf.get_u16_ne());
415    /// ```
416    ///
417    /// # Panics
418    ///
419    /// This function panics if there is not enough remaining data in `self`.
420    fn get_u16_ne(&mut self) -> u16 {
421        buf_get_impl!(self, u16::from_ne_bytes);
422    }
423
424    /// Gets a signed 16 bit integer from `self` in big-endian byte order.
425    ///
426    /// The current position is advanced by 2.
427    ///
428    /// # Examples
429    ///
430    /// ```
431    /// use bytes::Buf;
432    ///
433    /// let mut buf = &b"\x08\x09 hello"[..];
434    /// assert_eq!(0x0809, buf.get_i16());
435    /// ```
436    ///
437    /// # Panics
438    ///
439    /// This function panics if there is not enough remaining data in `self`.
440    fn get_i16(&mut self) -> i16 {
441        buf_get_impl!(self, i16::from_be_bytes);
442    }
443
444    /// Gets a signed 16 bit integer from `self` in little-endian byte order.
445    ///
446    /// The current position is advanced by 2.
447    ///
448    /// # Examples
449    ///
450    /// ```
451    /// use bytes::Buf;
452    ///
453    /// let mut buf = &b"\x09\x08 hello"[..];
454    /// assert_eq!(0x0809, buf.get_i16_le());
455    /// ```
456    ///
457    /// # Panics
458    ///
459    /// This function panics if there is not enough remaining data in `self`.
460    fn get_i16_le(&mut self) -> i16 {
461        buf_get_impl!(self, i16::from_le_bytes);
462    }
463
464    /// Gets a signed 16 bit integer from `self` in native-endian byte order.
465    ///
466    /// The current position is advanced by 2.
467    ///
468    /// # Examples
469    ///
470    /// ```
471    /// use bytes::Buf;
472    ///
473    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
474    ///     true => b"\x08\x09 hello",
475    ///     false => b"\x09\x08 hello",
476    /// };
477    /// assert_eq!(0x0809, buf.get_i16_ne());
478    /// ```
479    ///
480    /// # Panics
481    ///
482    /// This function panics if there is not enough remaining data in `self`.
483    fn get_i16_ne(&mut self) -> i16 {
484        buf_get_impl!(self, i16::from_ne_bytes);
485    }
486
487    /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
488    ///
489    /// The current position is advanced by 4.
490    ///
491    /// # Examples
492    ///
493    /// ```
494    /// use bytes::Buf;
495    ///
496    /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
497    /// assert_eq!(0x0809A0A1, buf.get_u32());
498    /// ```
499    ///
500    /// # Panics
501    ///
502    /// This function panics if there is not enough remaining data in `self`.
503    fn get_u32(&mut self) -> u32 {
504        buf_get_impl!(self, u32::from_be_bytes);
505    }
506
507    /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
508    ///
509    /// The current position is advanced by 4.
510    ///
511    /// # Examples
512    ///
513    /// ```
514    /// use bytes::Buf;
515    ///
516    /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
517    /// assert_eq!(0x0809A0A1, buf.get_u32_le());
518    /// ```
519    ///
520    /// # Panics
521    ///
522    /// This function panics if there is not enough remaining data in `self`.
523    fn get_u32_le(&mut self) -> u32 {
524        buf_get_impl!(self, u32::from_le_bytes);
525    }
526
527    /// Gets an unsigned 32 bit integer from `self` in native-endian byte order.
528    ///
529    /// The current position is advanced by 4.
530    ///
531    /// # Examples
532    ///
533    /// ```
534    /// use bytes::Buf;
535    ///
536    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
537    ///     true => b"\x08\x09\xA0\xA1 hello",
538    ///     false => b"\xA1\xA0\x09\x08 hello",
539    /// };
540    /// assert_eq!(0x0809A0A1, buf.get_u32_ne());
541    /// ```
542    ///
543    /// # Panics
544    ///
545    /// This function panics if there is not enough remaining data in `self`.
546    fn get_u32_ne(&mut self) -> u32 {
547        buf_get_impl!(self, u32::from_ne_bytes);
548    }
549
550    /// Gets a signed 32 bit integer from `self` in big-endian byte order.
551    ///
552    /// The current position is advanced by 4.
553    ///
554    /// # Examples
555    ///
556    /// ```
557    /// use bytes::Buf;
558    ///
559    /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
560    /// assert_eq!(0x0809A0A1, buf.get_i32());
561    /// ```
562    ///
563    /// # Panics
564    ///
565    /// This function panics if there is not enough remaining data in `self`.
566    fn get_i32(&mut self) -> i32 {
567        buf_get_impl!(self, i32::from_be_bytes);
568    }
569
570    /// Gets a signed 32 bit integer from `self` in little-endian byte order.
571    ///
572    /// The current position is advanced by 4.
573    ///
574    /// # Examples
575    ///
576    /// ```
577    /// use bytes::Buf;
578    ///
579    /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
580    /// assert_eq!(0x0809A0A1, buf.get_i32_le());
581    /// ```
582    ///
583    /// # Panics
584    ///
585    /// This function panics if there is not enough remaining data in `self`.
586    fn get_i32_le(&mut self) -> i32 {
587        buf_get_impl!(self, i32::from_le_bytes);
588    }
589
590    /// Gets a signed 32 bit integer from `self` in native-endian byte order.
591    ///
592    /// The current position is advanced by 4.
593    ///
594    /// # Examples
595    ///
596    /// ```
597    /// use bytes::Buf;
598    ///
599    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
600    ///     true => b"\x08\x09\xA0\xA1 hello",
601    ///     false => b"\xA1\xA0\x09\x08 hello",
602    /// };
603    /// assert_eq!(0x0809A0A1, buf.get_i32_ne());
604    /// ```
605    ///
606    /// # Panics
607    ///
608    /// This function panics if there is not enough remaining data in `self`.
609    fn get_i32_ne(&mut self) -> i32 {
610        buf_get_impl!(self, i32::from_ne_bytes);
611    }
612
613    /// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
614    ///
615    /// The current position is advanced by 8.
616    ///
617    /// # Examples
618    ///
619    /// ```
620    /// use bytes::Buf;
621    ///
622    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
623    /// assert_eq!(0x0102030405060708, buf.get_u64());
624    /// ```
625    ///
626    /// # Panics
627    ///
628    /// This function panics if there is not enough remaining data in `self`.
629    fn get_u64(&mut self) -> u64 {
630        buf_get_impl!(self, u64::from_be_bytes);
631    }
632
633    /// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
634    ///
635    /// The current position is advanced by 8.
636    ///
637    /// # Examples
638    ///
639    /// ```
640    /// use bytes::Buf;
641    ///
642    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
643    /// assert_eq!(0x0102030405060708, buf.get_u64_le());
644    /// ```
645    ///
646    /// # Panics
647    ///
648    /// This function panics if there is not enough remaining data in `self`.
649    fn get_u64_le(&mut self) -> u64 {
650        buf_get_impl!(self, u64::from_le_bytes);
651    }
652
653    /// Gets an unsigned 64 bit integer from `self` in native-endian byte order.
654    ///
655    /// The current position is advanced by 8.
656    ///
657    /// # Examples
658    ///
659    /// ```
660    /// use bytes::Buf;
661    ///
662    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
663    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
664    ///     false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
665    /// };
666    /// assert_eq!(0x0102030405060708, buf.get_u64_ne());
667    /// ```
668    ///
669    /// # Panics
670    ///
671    /// This function panics if there is not enough remaining data in `self`.
672    fn get_u64_ne(&mut self) -> u64 {
673        buf_get_impl!(self, u64::from_ne_bytes);
674    }
675
676    /// Gets a signed 64 bit integer from `self` in big-endian byte order.
677    ///
678    /// The current position is advanced by 8.
679    ///
680    /// # Examples
681    ///
682    /// ```
683    /// use bytes::Buf;
684    ///
685    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
686    /// assert_eq!(0x0102030405060708, buf.get_i64());
687    /// ```
688    ///
689    /// # Panics
690    ///
691    /// This function panics if there is not enough remaining data in `self`.
692    fn get_i64(&mut self) -> i64 {
693        buf_get_impl!(self, i64::from_be_bytes);
694    }
695
696    /// Gets a signed 64 bit integer from `self` in little-endian byte order.
697    ///
698    /// The current position is advanced by 8.
699    ///
700    /// # Examples
701    ///
702    /// ```
703    /// use bytes::Buf;
704    ///
705    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
706    /// assert_eq!(0x0102030405060708, buf.get_i64_le());
707    /// ```
708    ///
709    /// # Panics
710    ///
711    /// This function panics if there is not enough remaining data in `self`.
712    fn get_i64_le(&mut self) -> i64 {
713        buf_get_impl!(self, i64::from_le_bytes);
714    }
715
716    /// Gets a signed 64 bit integer from `self` in native-endian byte order.
717    ///
718    /// The current position is advanced by 8.
719    ///
720    /// # Examples
721    ///
722    /// ```
723    /// use bytes::Buf;
724    ///
725    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
726    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
727    ///     false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
728    /// };
729    /// assert_eq!(0x0102030405060708, buf.get_i64_ne());
730    /// ```
731    ///
732    /// # Panics
733    ///
734    /// This function panics if there is not enough remaining data in `self`.
735    fn get_i64_ne(&mut self) -> i64 {
736        buf_get_impl!(self, i64::from_ne_bytes);
737    }
738
739    /// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
740    ///
741    /// The current position is advanced by 16.
742    ///
743    /// # Examples
744    ///
745    /// ```
746    /// use bytes::Buf;
747    ///
748    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
749    /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128());
750    /// ```
751    ///
752    /// # Panics
753    ///
754    /// This function panics if there is not enough remaining data in `self`.
755    fn get_u128(&mut self) -> u128 {
756        buf_get_impl!(self, u128::from_be_bytes);
757    }
758
759    /// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
760    ///
761    /// The current position is advanced by 16.
762    ///
763    /// # Examples
764    ///
765    /// ```
766    /// use bytes::Buf;
767    ///
768    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
769    /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le());
770    /// ```
771    ///
772    /// # Panics
773    ///
774    /// This function panics if there is not enough remaining data in `self`.
775    fn get_u128_le(&mut self) -> u128 {
776        buf_get_impl!(self, u128::from_le_bytes);
777    }
778
779    /// Gets an unsigned 128 bit integer from `self` in native-endian byte order.
780    ///
781    /// The current position is advanced by 16.
782    ///
783    /// # Examples
784    ///
785    /// ```
786    /// use bytes::Buf;
787    ///
788    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
789    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
790    ///     false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
791    /// };
792    /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_ne());
793    /// ```
794    ///
795    /// # Panics
796    ///
797    /// This function panics if there is not enough remaining data in `self`.
798    fn get_u128_ne(&mut self) -> u128 {
799        buf_get_impl!(self, u128::from_ne_bytes);
800    }
801
802    /// Gets a signed 128 bit integer from `self` in big-endian byte order.
803    ///
804    /// The current position is advanced by 16.
805    ///
806    /// # Examples
807    ///
808    /// ```
809    /// use bytes::Buf;
810    ///
811    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
812    /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128());
813    /// ```
814    ///
815    /// # Panics
816    ///
817    /// This function panics if there is not enough remaining data in `self`.
818    fn get_i128(&mut self) -> i128 {
819        buf_get_impl!(self, i128::from_be_bytes);
820    }
821
822    /// Gets a signed 128 bit integer from `self` in little-endian byte order.
823    ///
824    /// The current position is advanced by 16.
825    ///
826    /// # Examples
827    ///
828    /// ```
829    /// use bytes::Buf;
830    ///
831    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
832    /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le());
833    /// ```
834    ///
835    /// # Panics
836    ///
837    /// This function panics if there is not enough remaining data in `self`.
838    fn get_i128_le(&mut self) -> i128 {
839        buf_get_impl!(self, i128::from_le_bytes);
840    }
841
842    /// Gets a signed 128 bit integer from `self` in native-endian byte order.
843    ///
844    /// The current position is advanced by 16.
845    ///
846    /// # Examples
847    ///
848    /// ```
849    /// use bytes::Buf;
850    ///
851    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
852    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
853    ///     false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
854    /// };
855    /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_ne());
856    /// ```
857    ///
858    /// # Panics
859    ///
860    /// This function panics if there is not enough remaining data in `self`.
861    fn get_i128_ne(&mut self) -> i128 {
862        buf_get_impl!(self, i128::from_ne_bytes);
863    }
864
865    /// Gets an unsigned n-byte integer from `self` in big-endian byte order.
866    ///
867    /// The current position is advanced by `nbytes`.
868    ///
869    /// # Examples
870    ///
871    /// ```
872    /// use bytes::Buf;
873    ///
874    /// let mut buf = &b"\x01\x02\x03 hello"[..];
875    /// assert_eq!(0x010203, buf.get_uint(3));
876    /// ```
877    ///
878    /// # Panics
879    ///
880    /// This function panics if there is not enough remaining data in `self`, or
881    /// if `nbytes` is greater than 8.
882    fn get_uint(&mut self, nbytes: usize) -> u64 {
883        buf_get_impl!(be => self, u64, nbytes);
884    }
885
886    /// Gets an unsigned n-byte integer from `self` in little-endian byte order.
887    ///
888    /// The current position is advanced by `nbytes`.
889    ///
890    /// # Examples
891    ///
892    /// ```
893    /// use bytes::Buf;
894    ///
895    /// let mut buf = &b"\x03\x02\x01 hello"[..];
896    /// assert_eq!(0x010203, buf.get_uint_le(3));
897    /// ```
898    ///
899    /// # Panics
900    ///
901    /// This function panics if there is not enough remaining data in `self`, or
902    /// if `nbytes` is greater than 8.
903    fn get_uint_le(&mut self, nbytes: usize) -> u64 {
904        buf_get_impl!(le => self, u64, nbytes);
905    }
906
907    /// Gets an unsigned n-byte integer from `self` in native-endian byte order.
908    ///
909    /// The current position is advanced by `nbytes`.
910    ///
911    /// # Examples
912    ///
913    /// ```
914    /// use bytes::Buf;
915    ///
916    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
917    ///     true => b"\x01\x02\x03 hello",
918    ///     false => b"\x03\x02\x01 hello",
919    /// };
920    /// assert_eq!(0x010203, buf.get_uint_ne(3));
921    /// ```
922    ///
923    /// # Panics
924    ///
925    /// This function panics if there is not enough remaining data in `self`, or
926    /// if `nbytes` is greater than 8.
927    fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
928        if cfg!(target_endian = "big") {
929            self.get_uint(nbytes)
930        } else {
931            self.get_uint_le(nbytes)
932        }
933    }
934
935    /// Gets a signed n-byte integer from `self` in big-endian byte order.
936    ///
937    /// The current position is advanced by `nbytes`.
938    ///
939    /// # Examples
940    ///
941    /// ```
942    /// use bytes::Buf;
943    ///
944    /// let mut buf = &b"\x01\x02\x03 hello"[..];
945    /// assert_eq!(0x010203, buf.get_int(3));
946    /// ```
947    ///
948    /// # Panics
949    ///
950    /// This function panics if there is not enough remaining data in `self`, or
951    /// if `nbytes` is greater than 8.
952    fn get_int(&mut self, nbytes: usize) -> i64 {
953        sign_extend(self.get_uint(nbytes), nbytes)
954    }
955
956    /// Gets a signed n-byte integer from `self` in little-endian byte order.
957    ///
958    /// The current position is advanced by `nbytes`.
959    ///
960    /// # Examples
961    ///
962    /// ```
963    /// use bytes::Buf;
964    ///
965    /// let mut buf = &b"\x03\x02\x01 hello"[..];
966    /// assert_eq!(0x010203, buf.get_int_le(3));
967    /// ```
968    ///
969    /// # Panics
970    ///
971    /// This function panics if there is not enough remaining data in `self`, or
972    /// if `nbytes` is greater than 8.
973    fn get_int_le(&mut self, nbytes: usize) -> i64 {
974        sign_extend(self.get_uint_le(nbytes), nbytes)
975    }
976
977    /// Gets a signed n-byte integer from `self` in native-endian byte order.
978    ///
979    /// The current position is advanced by `nbytes`.
980    ///
981    /// # Examples
982    ///
983    /// ```
984    /// use bytes::Buf;
985    ///
986    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
987    ///     true => b"\x01\x02\x03 hello",
988    ///     false => b"\x03\x02\x01 hello",
989    /// };
990    /// assert_eq!(0x010203, buf.get_int_ne(3));
991    /// ```
992    ///
993    /// # Panics
994    ///
995    /// This function panics if there is not enough remaining data in `self`, or
996    /// if `nbytes` is greater than 8.
997    fn get_int_ne(&mut self, nbytes: usize) -> i64 {
998        if cfg!(target_endian = "big") {
999            self.get_int(nbytes)
1000        } else {
1001            self.get_int_le(nbytes)
1002        }
1003    }
1004
1005    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1006    /// `self` in big-endian byte order.
1007    ///
1008    /// The current position is advanced by 4.
1009    ///
1010    /// # Examples
1011    ///
1012    /// ```
1013    /// use bytes::Buf;
1014    ///
1015    /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..];
1016    /// assert_eq!(1.2f32, buf.get_f32());
1017    /// ```
1018    ///
1019    /// # Panics
1020    ///
1021    /// This function panics if there is not enough remaining data in `self`.
1022    fn get_f32(&mut self) -> f32 {
1023        f32::from_bits(self.get_u32())
1024    }
1025
1026    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1027    /// `self` in little-endian byte order.
1028    ///
1029    /// The current position is advanced by 4.
1030    ///
1031    /// # Examples
1032    ///
1033    /// ```
1034    /// use bytes::Buf;
1035    ///
1036    /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..];
1037    /// assert_eq!(1.2f32, buf.get_f32_le());
1038    /// ```
1039    ///
1040    /// # Panics
1041    ///
1042    /// This function panics if there is not enough remaining data in `self`.
1043    fn get_f32_le(&mut self) -> f32 {
1044        f32::from_bits(self.get_u32_le())
1045    }
1046
1047    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1048    /// `self` in native-endian byte order.
1049    ///
1050    /// The current position is advanced by 4.
1051    ///
1052    /// # Examples
1053    ///
1054    /// ```
1055    /// use bytes::Buf;
1056    ///
1057    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1058    ///     true => b"\x3F\x99\x99\x9A hello",
1059    ///     false => b"\x9A\x99\x99\x3F hello",
1060    /// };
1061    /// assert_eq!(1.2f32, buf.get_f32_ne());
1062    /// ```
1063    ///
1064    /// # Panics
1065    ///
1066    /// This function panics if there is not enough remaining data in `self`.
1067    fn get_f32_ne(&mut self) -> f32 {
1068        f32::from_bits(self.get_u32_ne())
1069    }
1070
1071    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1072    /// `self` in big-endian byte order.
1073    ///
1074    /// The current position is advanced by 8.
1075    ///
1076    /// # Examples
1077    ///
1078    /// ```
1079    /// use bytes::Buf;
1080    ///
1081    /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..];
1082    /// assert_eq!(1.2f64, buf.get_f64());
1083    /// ```
1084    ///
1085    /// # Panics
1086    ///
1087    /// This function panics if there is not enough remaining data in `self`.
1088    fn get_f64(&mut self) -> f64 {
1089        f64::from_bits(self.get_u64())
1090    }
1091
1092    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1093    /// `self` in little-endian byte order.
1094    ///
1095    /// The current position is advanced by 8.
1096    ///
1097    /// # Examples
1098    ///
1099    /// ```
1100    /// use bytes::Buf;
1101    ///
1102    /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..];
1103    /// assert_eq!(1.2f64, buf.get_f64_le());
1104    /// ```
1105    ///
1106    /// # Panics
1107    ///
1108    /// This function panics if there is not enough remaining data in `self`.
1109    fn get_f64_le(&mut self) -> f64 {
1110        f64::from_bits(self.get_u64_le())
1111    }
1112
1113    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1114    /// `self` in native-endian byte order.
1115    ///
1116    /// The current position is advanced by 8.
1117    ///
1118    /// # Examples
1119    ///
1120    /// ```
1121    /// use bytes::Buf;
1122    ///
1123    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1124    ///     true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello",
1125    ///     false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello",
1126    /// };
1127    /// assert_eq!(1.2f64, buf.get_f64_ne());
1128    /// ```
1129    ///
1130    /// # Panics
1131    ///
1132    /// This function panics if there is not enough remaining data in `self`.
1133    fn get_f64_ne(&mut self) -> f64 {
1134        f64::from_bits(self.get_u64_ne())
1135    }
1136
1137    /// Copies bytes from `self` into `dst`.
1138    ///
1139    /// The cursor is advanced by the number of bytes copied. `self` must have
1140    /// enough remaining bytes to fill `dst`.
1141    ///
1142    /// Returns `Err(TryGetError)` when there are not enough
1143    /// remaining bytes to read the value.
1144    ///
1145    /// # Examples
1146    ///
1147    /// ```
1148    /// use bytes::Buf;
1149    ///
1150    /// let mut buf = &b"hello world"[..];
1151    /// let mut dst = [0; 5];
1152    ///
1153    /// assert_eq!(Ok(()), buf.try_copy_to_slice(&mut dst));
1154    /// assert_eq!(&b"hello"[..], &dst);
1155    /// assert_eq!(6, buf.remaining());
1156    /// ```
1157    ///
1158    /// ```
1159    /// use bytes::{Buf, TryGetError};
1160    ///
1161    /// let mut buf = &b"hello world"[..];
1162    /// let mut dst = [0; 12];
1163    ///
1164    /// assert_eq!(Err(TryGetError{requested: 12, available: 11}), buf.try_copy_to_slice(&mut dst));
1165    /// assert_eq!(11, buf.remaining());
1166    /// ```
1167    fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> {
1168        if self.remaining() < dst.len() {
1169            return Err(TryGetError {
1170                requested: dst.len(),
1171                available: self.remaining(),
1172            });
1173        }
1174
1175        while !dst.is_empty() {
1176            let src = self.chunk();
1177            let cnt = usize::min(src.len(), dst.len());
1178
1179            dst[..cnt].copy_from_slice(&src[..cnt]);
1180            dst = &mut dst[cnt..];
1181
1182            self.advance(cnt);
1183        }
1184        Ok(())
1185    }
1186
1187    /// Gets an unsigned 8 bit integer from `self`.
1188    ///
1189    /// The current position is advanced by 1.
1190    ///
1191    /// Returns `Err(TryGetError)` when there are not enough
1192    /// remaining bytes to read the value.
1193    ///
1194    /// # Examples
1195    ///
1196    /// ```
1197    /// use bytes::Buf;
1198    ///
1199    /// let mut buf = &b"\x08 hello"[..];
1200    /// assert_eq!(Ok(0x08_u8), buf.try_get_u8());
1201    /// assert_eq!(6, buf.remaining());
1202    /// ```
1203    ///
1204    /// ```
1205    /// use bytes::{Buf, TryGetError};
1206    ///
1207    /// let mut buf = &b""[..];
1208    /// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_u8());
1209    /// ```
1210    fn try_get_u8(&mut self) -> Result<u8, TryGetError> {
1211        if self.remaining() < 1 {
1212            return Err(TryGetError {
1213                requested: 1,
1214                available: self.remaining(),
1215            });
1216        }
1217        let ret = self.chunk()[0];
1218        self.advance(1);
1219        Ok(ret)
1220    }
1221
1222    /// Gets a signed 8 bit integer from `self`.
1223    ///
1224    /// The current position is advanced by 1.
1225    ///
1226    /// Returns `Err(TryGetError)` when there are not enough
1227    /// remaining bytes to read the value.
1228    ///
1229    /// # Examples
1230    ///
1231    /// ```
1232    /// use bytes::Buf;
1233    ///
1234    /// let mut buf = &b"\x08 hello"[..];
1235    /// assert_eq!(Ok(0x08_i8), buf.try_get_i8());
1236    /// assert_eq!(6, buf.remaining());
1237    /// ```
1238    ///
1239    /// ```
1240    /// use bytes::{Buf, TryGetError};
1241    ///
1242    /// let mut buf = &b""[..];
1243    /// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_i8());
1244    /// ```
1245    fn try_get_i8(&mut self) -> Result<i8, TryGetError> {
1246        if self.remaining() < 1 {
1247            return Err(TryGetError {
1248                requested: 1,
1249                available: self.remaining(),
1250            });
1251        }
1252        let ret = self.chunk()[0] as i8;
1253        self.advance(1);
1254        Ok(ret)
1255    }
1256
1257    /// Gets an unsigned 16 bit integer from `self` in big-endian byte order.
1258    ///
1259    /// The current position is advanced by 2.
1260    ///
1261    /// Returns `Err(TryGetError)` when there are not enough
1262    /// remaining bytes to read the value.
1263    ///
1264    /// # Examples
1265    ///
1266    /// ```
1267    /// use bytes::Buf;
1268    ///
1269    /// let mut buf = &b"\x08\x09 hello"[..];
1270    /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16());
1271    /// assert_eq!(6, buf.remaining());
1272    /// ```
1273    ///
1274    /// ```
1275    /// use bytes::{Buf, TryGetError};
1276    ///
1277    /// let mut buf = &b"\x08"[..];
1278    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16());
1279    /// assert_eq!(1, buf.remaining());
1280    /// ```
1281    fn try_get_u16(&mut self) -> Result<u16, TryGetError> {
1282        buf_try_get_impl!(self, u16::from_be_bytes)
1283    }
1284
1285    /// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
1286    ///
1287    /// The current position is advanced by 2.
1288    ///
1289    /// Returns `Err(TryGetError)` when there are not enough
1290    /// remaining bytes to read the value.
1291    ///
1292    /// # Examples
1293    ///
1294    /// ```
1295    /// use bytes::Buf;
1296    ///
1297    /// let mut buf = &b"\x09\x08 hello"[..];
1298    /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_le());
1299    /// assert_eq!(6, buf.remaining());
1300    /// ```
1301    ///
1302    /// ```
1303    /// use bytes::{Buf, TryGetError};
1304    ///
1305    /// let mut buf = &b"\x08"[..];
1306    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_le());
1307    /// assert_eq!(1, buf.remaining());
1308    /// ```
1309    fn try_get_u16_le(&mut self) -> Result<u16, TryGetError> {
1310        buf_try_get_impl!(self, u16::from_le_bytes)
1311    }
1312
1313    /// Gets an unsigned 16 bit integer from `self` in native-endian byte order.
1314    ///
1315    /// The current position is advanced by 2.
1316    ///
1317    /// Returns `Err(TryGetError)` when there are not enough
1318    /// remaining bytes to read the value.
1319    ///
1320    /// # Examples
1321    ///
1322    /// ```
1323    /// use bytes::Buf;
1324    ///
1325    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1326    ///     true => b"\x08\x09 hello",
1327    ///     false => b"\x09\x08 hello",
1328    /// };
1329    /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_ne());
1330    /// assert_eq!(6, buf.remaining());
1331    /// ```
1332    ///
1333    /// ```
1334    /// use bytes::{Buf, TryGetError};
1335    ///
1336    /// let mut buf = &b"\x08"[..];
1337    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_ne());
1338    /// assert_eq!(1, buf.remaining());
1339    /// ```
1340    fn try_get_u16_ne(&mut self) -> Result<u16, TryGetError> {
1341        buf_try_get_impl!(self, u16::from_ne_bytes)
1342    }
1343
1344    /// Gets a signed 16 bit integer from `self` in big-endian byte order.
1345    ///
1346    /// The current position is advanced by 2.
1347    ///
1348    /// Returns `Err(TryGetError)` when there are not enough
1349    /// remaining bytes to read the value.
1350    ///
1351    /// # Examples
1352    ///
1353    /// ```
1354    /// use bytes::Buf;
1355    ///
1356    /// let mut buf = &b"\x08\x09 hello"[..];
1357    /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16());
1358    /// assert_eq!(6, buf.remaining());
1359    /// ```
1360    ///
1361    /// ```
1362    /// use bytes::{Buf, TryGetError};
1363    ///
1364    /// let mut buf = &b"\x08"[..];
1365    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16());
1366    /// assert_eq!(1, buf.remaining());
1367    /// ```
1368    fn try_get_i16(&mut self) -> Result<i16, TryGetError> {
1369        buf_try_get_impl!(self, i16::from_be_bytes)
1370    }
1371
1372    /// Gets an signed 16 bit integer from `self` in little-endian byte order.
1373    ///
1374    /// The current position is advanced by 2.
1375    ///
1376    /// Returns `Err(TryGetError)` when there are not enough
1377    /// remaining bytes to read the value.
1378    ///
1379    /// # Examples
1380    ///
1381    /// ```
1382    /// use bytes::Buf;
1383    ///
1384    /// let mut buf = &b"\x09\x08 hello"[..];
1385    /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_le());
1386    /// assert_eq!(6, buf.remaining());
1387    /// ```
1388    ///
1389    /// ```
1390    /// use bytes::{Buf, TryGetError};
1391    ///
1392    /// let mut buf = &b"\x08"[..];
1393    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_le());
1394    /// assert_eq!(1, buf.remaining());
1395    /// ```
1396    fn try_get_i16_le(&mut self) -> Result<i16, TryGetError> {
1397        buf_try_get_impl!(self, i16::from_le_bytes)
1398    }
1399
1400    /// Gets a signed 16 bit integer from `self` in native-endian byte order.
1401    ///
1402    /// The current position is advanced by 2.
1403    ///
1404    /// Returns `Err(TryGetError)` when there are not enough
1405    /// remaining bytes to read the value.
1406    ///
1407    /// # Examples
1408    ///
1409    /// ```
1410    /// use bytes::Buf;
1411    ///
1412    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1413    ///     true => b"\x08\x09 hello",
1414    ///     false => b"\x09\x08 hello",
1415    /// };
1416    /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_ne());
1417    /// assert_eq!(6, buf.remaining());
1418    /// ```
1419    ///
1420    /// ```
1421    /// use bytes::{Buf, TryGetError};
1422    ///
1423    /// let mut buf = &b"\x08"[..];
1424    /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_ne());
1425    /// assert_eq!(1, buf.remaining());
1426    /// ```
1427    fn try_get_i16_ne(&mut self) -> Result<i16, TryGetError> {
1428        buf_try_get_impl!(self, i16::from_ne_bytes)
1429    }
1430
1431    /// Gets an unsigned 32 bit integer from `self` in big-endian byte order.
1432    ///
1433    /// The current position is advanced by 4.
1434    ///
1435    /// Returns `Err(TryGetError)` when there are not enough
1436    /// remaining bytes to read the value.
1437    ///
1438    /// # Examples
1439    ///
1440    /// ```
1441    /// use bytes::Buf;
1442    ///
1443    /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
1444    /// assert_eq!(Ok(0x0809A0A1), buf.try_get_u32());
1445    /// assert_eq!(6, buf.remaining());
1446    /// ```
1447    ///
1448    /// ```
1449    /// use bytes::{Buf, TryGetError};
1450    ///
1451    /// let mut buf = &b"\x01\x02\x03"[..];
1452    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32());
1453    /// assert_eq!(3, buf.remaining());
1454    /// ```
1455    fn try_get_u32(&mut self) -> Result<u32, TryGetError> {
1456        buf_try_get_impl!(self, u32::from_be_bytes)
1457    }
1458
1459    /// Gets an unsigned 32 bit integer from `self` in little-endian byte order.
1460    ///
1461    /// The current position is advanced by 4.
1462    ///
1463    /// Returns `Err(TryGetError)` when there are not enough
1464    /// remaining bytes to read the value.
1465    ///
1466    /// # Examples
1467    ///
1468    /// ```
1469    /// use bytes::Buf;
1470    ///
1471    /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
1472    /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_le());
1473    /// assert_eq!(6, buf.remaining());
1474    /// ```
1475    ///
1476    /// ```
1477    /// use bytes::{Buf, TryGetError};
1478    ///
1479    /// let mut buf = &b"\x08\x09\xA0"[..];
1480    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_le());
1481    /// assert_eq!(3, buf.remaining());
1482    /// ```
1483    fn try_get_u32_le(&mut self) -> Result<u32, TryGetError> {
1484        buf_try_get_impl!(self, u32::from_le_bytes)
1485    }
1486
1487    /// Gets an unsigned 32 bit integer from `self` in native-endian byte order.
1488    ///
1489    /// The current position is advanced by 4.
1490    ///
1491    /// Returns `Err(TryGetError)` when there are not enough
1492    /// remaining bytes to read the value.
1493    ///
1494    /// # Examples
1495    ///
1496    /// ```
1497    /// use bytes::Buf;
1498    ///
1499    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1500    ///     true => b"\x08\x09\xA0\xA1 hello",
1501    ///     false => b"\xA1\xA0\x09\x08 hello",
1502    /// };
1503    /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_ne());
1504    /// assert_eq!(6, buf.remaining());
1505    /// ```
1506    ///
1507    /// ```
1508    /// use bytes::{Buf, TryGetError};
1509    ///
1510    /// let mut buf = &b"\x08\x09\xA0"[..];
1511    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_ne());
1512    /// assert_eq!(3, buf.remaining());
1513    /// ```
1514    fn try_get_u32_ne(&mut self) -> Result<u32, TryGetError> {
1515        buf_try_get_impl!(self, u32::from_ne_bytes)
1516    }
1517
1518    /// Gets a signed 32 bit integer from `self` in big-endian byte order.
1519    ///
1520    /// The current position is advanced by 4.
1521    ///
1522    /// Returns `Err(TryGetError)` when there are not enough
1523    /// remaining bytes to read the value.
1524    ///
1525    /// # Examples
1526    ///
1527    /// ```
1528    /// use bytes::Buf;
1529    ///
1530    /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
1531    /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32());
1532    /// assert_eq!(6, buf.remaining());
1533    /// ```
1534    ///
1535    /// ```
1536    /// use bytes::{Buf, TryGetError};
1537    ///
1538    /// let mut buf = &b"\x01\x02\x03"[..];
1539    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32());
1540    /// assert_eq!(3, buf.remaining());
1541    /// ```
1542    fn try_get_i32(&mut self) -> Result<i32, TryGetError> {
1543        buf_try_get_impl!(self, i32::from_be_bytes)
1544    }
1545
1546    /// Gets a signed 32 bit integer from `self` in little-endian byte order.
1547    ///
1548    /// The current position is advanced by 4.
1549    ///
1550    /// Returns `Err(TryGetError)` when there are not enough
1551    /// remaining bytes to read the value.
1552    ///
1553    /// # Examples
1554    ///
1555    /// ```
1556    /// use bytes::Buf;
1557    ///
1558    /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
1559    /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_le());
1560    /// assert_eq!(6, buf.remaining());
1561    /// ```
1562    ///
1563    /// ```
1564    /// use bytes::{Buf, TryGetError};
1565    ///
1566    /// let mut buf = &b"\x08\x09\xA0"[..];
1567    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_le());
1568    /// assert_eq!(3, buf.remaining());
1569    /// ```
1570    fn try_get_i32_le(&mut self) -> Result<i32, TryGetError> {
1571        buf_try_get_impl!(self, i32::from_le_bytes)
1572    }
1573
1574    /// Gets a signed 32 bit integer from `self` in native-endian byte order.
1575    ///
1576    /// The current position is advanced by 4.
1577    ///
1578    /// Returns `Err(TryGetError)` when there are not enough
1579    /// remaining bytes to read the value.
1580    ///
1581    /// # Examples
1582    ///
1583    /// ```
1584    /// use bytes::Buf;
1585    ///
1586    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1587    ///     true => b"\x08\x09\xA0\xA1 hello",
1588    ///     false => b"\xA1\xA0\x09\x08 hello",
1589    /// };
1590    /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_ne());
1591    /// assert_eq!(6, buf.remaining());
1592    /// ```
1593    ///
1594    /// ```
1595    /// use bytes::{Buf, TryGetError};
1596    ///
1597    /// let mut buf = &b"\x08\x09\xA0"[..];
1598    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_ne());
1599    /// assert_eq!(3, buf.remaining());
1600    /// ```
1601    fn try_get_i32_ne(&mut self) -> Result<i32, TryGetError> {
1602        buf_try_get_impl!(self, i32::from_ne_bytes)
1603    }
1604
1605    /// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
1606    ///
1607    /// The current position is advanced by 8.
1608    ///
1609    /// Returns `Err(TryGetError)` when there are not enough
1610    /// remaining bytes to read the value.
1611    ///
1612    /// # Examples
1613    ///
1614    /// ```
1615    /// use bytes::Buf;
1616    ///
1617    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
1618    /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64());
1619    /// assert_eq!(6, buf.remaining());
1620    /// ```
1621    ///
1622    /// ```
1623    /// use bytes::{Buf, TryGetError};
1624    ///
1625    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..];
1626    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64());
1627    /// assert_eq!(7, buf.remaining());
1628    /// ```
1629    fn try_get_u64(&mut self) -> Result<u64, TryGetError> {
1630        buf_try_get_impl!(self, u64::from_be_bytes)
1631    }
1632
1633    /// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
1634    ///
1635    /// The current position is advanced by 8.
1636    ///
1637    /// Returns `Err(TryGetError)` when there are not enough
1638    /// remaining bytes to read the value.
1639    ///
1640    /// # Examples
1641    ///
1642    /// ```
1643    /// use bytes::Buf;
1644    ///
1645    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
1646    /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_le());
1647    /// assert_eq!(6, buf.remaining());
1648    /// ```
1649    ///
1650    /// ```
1651    /// use bytes::{Buf, TryGetError};
1652    ///
1653    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..];
1654    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_le());
1655    /// assert_eq!(7, buf.remaining());
1656    /// ```
1657    fn try_get_u64_le(&mut self) -> Result<u64, TryGetError> {
1658        buf_try_get_impl!(self, u64::from_le_bytes)
1659    }
1660
1661    /// Gets an unsigned 64 bit integer from `self` in native-endian byte order.
1662    ///
1663    /// The current position is advanced by 8.
1664    ///
1665    /// Returns `Err(TryGetError)` when there are not enough
1666    /// remaining bytes to read the value.
1667    ///
1668    /// # Examples
1669    ///
1670    /// ```
1671    /// use bytes::Buf;
1672    ///
1673    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1674    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
1675    ///     false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
1676    /// };
1677    /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_ne());
1678    /// assert_eq!(6, buf.remaining());
1679    /// ```
1680    ///
1681    /// ```
1682    /// use bytes::{Buf, TryGetError};
1683    ///
1684    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..];
1685    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_ne());
1686    /// assert_eq!(7, buf.remaining());
1687    /// ```
1688    fn try_get_u64_ne(&mut self) -> Result<u64, TryGetError> {
1689        buf_try_get_impl!(self, u64::from_ne_bytes)
1690    }
1691
1692    /// Gets a signed 64 bit integer from `self` in big-endian byte order.
1693    ///
1694    /// The current position is advanced by 8.
1695    ///
1696    /// Returns `Err(TryGetError)` when there are not enough
1697    /// remaining bytes to read the value.
1698    ///
1699    /// # Examples
1700    ///
1701    /// ```
1702    /// use bytes::Buf;
1703    ///
1704    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
1705    /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64());
1706    /// assert_eq!(6, buf.remaining());
1707    /// ```
1708    ///
1709    /// ```
1710    /// use bytes::{Buf, TryGetError};
1711    ///
1712    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..];
1713    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64());
1714    /// assert_eq!(7, buf.remaining());
1715    /// ```
1716    fn try_get_i64(&mut self) -> Result<i64, TryGetError> {
1717        buf_try_get_impl!(self, i64::from_be_bytes)
1718    }
1719
1720    /// Gets a signed 64 bit integer from `self` in little-endian byte order.
1721    ///
1722    /// The current position is advanced by 8.
1723    ///
1724    /// Returns `Err(TryGetError)` when there are not enough
1725    /// remaining bytes to read the value.
1726    ///
1727    /// # Examples
1728    ///
1729    /// ```
1730    /// use bytes::Buf;
1731    ///
1732    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
1733    /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_le());
1734    /// assert_eq!(6, buf.remaining());
1735    /// ```
1736    ///
1737    /// ```
1738    /// use bytes::{Buf, TryGetError};
1739    ///
1740    /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..];
1741    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_le());
1742    /// assert_eq!(7, buf.remaining());
1743    /// ```
1744    fn try_get_i64_le(&mut self) -> Result<i64, TryGetError> {
1745        buf_try_get_impl!(self, i64::from_le_bytes)
1746    }
1747
1748    /// Gets a signed 64 bit integer from `self` in native-endian byte order.
1749    ///
1750    /// The current position is advanced by 8.
1751    ///
1752    /// Returns `Err(TryGetError)` when there are not enough
1753    /// remaining bytes to read the value.
1754    ///
1755    /// # Examples
1756    ///
1757    /// ```
1758    /// use bytes::Buf;
1759    ///
1760    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1761    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
1762    ///     false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
1763    /// };
1764    /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_ne());
1765    /// assert_eq!(6, buf.remaining());
1766    /// ```
1767    ///
1768    /// ```
1769    /// use bytes::{Buf, TryGetError};
1770    ///
1771    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..];
1772    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_ne());
1773    /// assert_eq!(7, buf.remaining());
1774    /// ```
1775    fn try_get_i64_ne(&mut self) -> Result<i64, TryGetError> {
1776        buf_try_get_impl!(self, i64::from_ne_bytes)
1777    }
1778
1779    /// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
1780    ///
1781    /// The current position is advanced by 16.
1782    ///
1783    /// Returns `Err(TryGetError)` when there are not enough
1784    /// remaining bytes to read the value.
1785    ///
1786    /// # Examples
1787    ///
1788    /// ```
1789    /// use bytes::Buf;
1790    ///
1791    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
1792    /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128());
1793    /// assert_eq!(6, buf.remaining());
1794    /// ```
1795    ///
1796    /// ```
1797    /// use bytes::{Buf, TryGetError};
1798    ///
1799    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..];
1800    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128());
1801    /// assert_eq!(15, buf.remaining());
1802    /// ```
1803    fn try_get_u128(&mut self) -> Result<u128, TryGetError> {
1804        buf_try_get_impl!(self, u128::from_be_bytes)
1805    }
1806
1807    /// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
1808    ///
1809    /// The current position is advanced by 16.
1810    ///
1811    /// Returns `Err(TryGetError)` when there are not enough
1812    /// remaining bytes to read the value.
1813    ///
1814    /// # Examples
1815    ///
1816    /// ```
1817    /// use bytes::Buf;
1818    ///
1819    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
1820    /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_le());
1821    /// assert_eq!(6, buf.remaining());
1822    /// ```
1823    ///
1824    /// ```
1825    /// use bytes::{Buf, TryGetError};
1826    ///
1827    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..];
1828    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_le());
1829    /// assert_eq!(15, buf.remaining());
1830    /// ```
1831    fn try_get_u128_le(&mut self) -> Result<u128, TryGetError> {
1832        buf_try_get_impl!(self, u128::from_le_bytes)
1833    }
1834
1835    /// Gets an unsigned 128 bit integer from `self` in native-endian byte order.
1836    ///
1837    /// The current position is advanced by 16.
1838    ///
1839    /// Returns `Err(TryGetError)` when there are not enough
1840    /// remaining bytes to read the value.
1841    ///
1842    /// # Examples
1843    ///
1844    /// ```
1845    /// use bytes::Buf;
1846    ///
1847    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1848    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
1849    ///     false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
1850    /// };
1851    /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_ne());
1852    /// assert_eq!(6, buf.remaining());
1853    /// ```
1854    ///
1855    /// ```
1856    /// use bytes::{Buf, TryGetError};
1857    ///
1858    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..];
1859    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_ne());
1860    /// assert_eq!(15, buf.remaining());
1861    /// ```
1862    fn try_get_u128_ne(&mut self) -> Result<u128, TryGetError> {
1863        buf_try_get_impl!(self, u128::from_ne_bytes)
1864    }
1865
1866    /// Gets a signed 128 bit integer from `self` in big-endian byte order.
1867    ///
1868    /// The current position is advanced by 16.
1869    ///
1870    /// Returns `Err(TryGetError)` when there are not enough
1871    /// remaining bytes to read the value.
1872    ///
1873    /// # Examples
1874    ///
1875    /// ```
1876    /// use bytes::Buf;
1877    ///
1878    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
1879    /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128());
1880    /// assert_eq!(6, buf.remaining());
1881    /// ```
1882    ///
1883    /// ```
1884    /// use bytes::{Buf, TryGetError};
1885    ///
1886    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..];
1887    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128());
1888    /// assert_eq!(15, buf.remaining());
1889    /// ```
1890    fn try_get_i128(&mut self) -> Result<i128, TryGetError> {
1891        buf_try_get_impl!(self, i128::from_be_bytes)
1892    }
1893
1894    /// Gets a signed 128 bit integer from `self` in little-endian byte order.
1895    ///
1896    /// The current position is advanced by 16.
1897    ///
1898    /// Returns `Err(TryGetError)` when there are not enough
1899    /// remaining bytes to read the value.
1900    ///
1901    /// # Examples
1902    ///
1903    /// ```
1904    /// use bytes::Buf;
1905    ///
1906    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
1907    /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_le());
1908    /// assert_eq!(6, buf.remaining());
1909    /// ```
1910    ///
1911    /// ```
1912    /// use bytes::{Buf, TryGetError};
1913    ///
1914    /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..];
1915    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_le());
1916    /// assert_eq!(15, buf.remaining());
1917    /// ```
1918    fn try_get_i128_le(&mut self) -> Result<i128, TryGetError> {
1919        buf_try_get_impl!(self, i128::from_le_bytes)
1920    }
1921
1922    /// Gets a signed 128 bit integer from `self` in native-endian byte order.
1923    ///
1924    /// The current position is advanced by 16.
1925    ///
1926    /// Returns `Err(TryGetError)` when there are not enough
1927    /// remaining bytes to read the value.
1928    ///
1929    /// # Examples
1930    ///
1931    /// ```
1932    /// use bytes::Buf;
1933    ///
1934    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1935    ///     true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
1936    ///     false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
1937    /// };
1938    /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_ne());
1939    /// assert_eq!(6, buf.remaining());
1940    /// ```
1941    ///
1942    /// ```
1943    /// use bytes::{Buf, TryGetError};
1944    ///
1945    /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..];
1946    /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_ne());
1947    /// assert_eq!(15, buf.remaining());
1948    /// ```
1949    fn try_get_i128_ne(&mut self) -> Result<i128, TryGetError> {
1950        buf_try_get_impl!(self, i128::from_ne_bytes)
1951    }
1952
1953    /// Gets an unsigned n-byte integer from `self` in big-endian byte order.
1954    ///
1955    /// The current position is advanced by `nbytes`.
1956    ///
1957    /// Returns `Err(TryGetError)` when there are not enough
1958    /// remaining bytes to read the value.
1959    ///
1960    /// # Examples
1961    ///
1962    /// ```
1963    /// use bytes::Buf;
1964    ///
1965    /// let mut buf = &b"\x01\x02\x03 hello"[..];
1966    /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint(3));
1967    /// assert_eq!(6, buf.remaining());
1968    /// ```
1969    ///
1970    /// ```
1971    /// use bytes::{Buf, TryGetError};
1972    ///
1973    /// let mut buf = &b"\x01\x02\x03"[..];
1974    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint(4));
1975    /// assert_eq!(3, buf.remaining());
1976    /// ```
1977    ///
1978    /// # Panics
1979    ///
1980    /// This function panics if `nbytes` > 8.
1981    fn try_get_uint(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
1982        buf_try_get_impl!(be => self, u64, nbytes);
1983    }
1984
1985    /// Gets an unsigned n-byte integer from `self` in little-endian byte order.
1986    ///
1987    /// The current position is advanced by `nbytes`.
1988    ///
1989    /// Returns `Err(TryGetError)` when there are not enough
1990    /// remaining bytes to read the value.
1991    ///
1992    /// # Examples
1993    ///
1994    /// ```
1995    /// use bytes::Buf;
1996    ///
1997    /// let mut buf = &b"\x03\x02\x01 hello"[..];
1998    /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_le(3));
1999    /// assert_eq!(6, buf.remaining());
2000    /// ```
2001    ///
2002    /// ```
2003    /// use bytes::{Buf, TryGetError};
2004    ///
2005    /// let mut buf = &b"\x01\x02\x03"[..];
2006    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_le(4));
2007    /// assert_eq!(3, buf.remaining());
2008    /// ```
2009    ///
2010    /// # Panics
2011    ///
2012    /// This function panics if `nbytes` > 8.
2013    fn try_get_uint_le(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
2014        buf_try_get_impl!(le => self, u64, nbytes);
2015    }
2016
2017    /// Gets an unsigned n-byte integer from `self` in native-endian byte order.
2018    ///
2019    /// The current position is advanced by `nbytes`.
2020    ///
2021    /// Returns `Err(TryGetError)` when there are not enough
2022    /// remaining bytes to read the value.
2023    ///
2024    /// # Examples
2025    ///
2026    /// ```
2027    /// use bytes::Buf;
2028    ///
2029    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2030    ///     true => b"\x01\x02\x03 hello",
2031    ///     false => b"\x03\x02\x01 hello",
2032    /// };
2033    /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_ne(3));
2034    /// assert_eq!(6, buf.remaining());
2035    /// ```
2036    ///
2037    /// ```
2038    /// use bytes::{Buf, TryGetError};
2039    ///
2040    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2041    ///     true => b"\x01\x02\x03",
2042    ///     false => b"\x03\x02\x01",
2043    /// };
2044    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_ne(4));
2045    /// assert_eq!(3, buf.remaining());
2046    /// ```
2047    ///
2048    /// # Panics
2049    ///
2050    /// This function panics if `nbytes` is greater than 8.
2051    fn try_get_uint_ne(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
2052        if cfg!(target_endian = "big") {
2053            self.try_get_uint(nbytes)
2054        } else {
2055            self.try_get_uint_le(nbytes)
2056        }
2057    }
2058
2059    /// Gets a signed n-byte integer from `self` in big-endian byte order.
2060    ///
2061    /// The current position is advanced by `nbytes`.
2062    ///
2063    /// Returns `Err(TryGetError)` when there are not enough
2064    /// remaining bytes to read the value.
2065    ///
2066    /// # Examples
2067    ///
2068    /// ```
2069    /// use bytes::Buf;
2070    ///
2071    /// let mut buf = &b"\x01\x02\x03 hello"[..];
2072    /// assert_eq!(Ok(0x010203_i64), buf.try_get_int(3));
2073    /// assert_eq!(6, buf.remaining());
2074    /// ```
2075    ///
2076    /// ```
2077    /// use bytes::{Buf, TryGetError};
2078    ///
2079    /// let mut buf = &b"\x01\x02\x03"[..];
2080    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int(4));
2081    /// assert_eq!(3, buf.remaining());
2082    /// ```
2083    ///
2084    /// # Panics
2085    ///
2086    /// This function panics if `nbytes` is greater than 8.
2087    fn try_get_int(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2088        Ok(sign_extend(self.try_get_uint(nbytes)?, nbytes))
2089    }
2090
2091    /// Gets a signed n-byte integer from `self` in little-endian byte order.
2092    ///
2093    /// The current position is advanced by `nbytes`.
2094    ///
2095    /// Returns `Err(TryGetError)` when there are not enough
2096    /// remaining bytes to read the value.
2097    ///
2098    /// # Examples
2099    ///
2100    /// ```
2101    /// use bytes::Buf;
2102    ///
2103    /// let mut buf = &b"\x03\x02\x01 hello"[..];
2104    /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_le(3));
2105    /// assert_eq!(6, buf.remaining());
2106    /// ```
2107    ///
2108    /// ```
2109    /// use bytes::{Buf, TryGetError};
2110    ///
2111    /// let mut buf = &b"\x01\x02\x03"[..];
2112    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_le(4));
2113    /// assert_eq!(3, buf.remaining());
2114    /// ```
2115    ///
2116    /// # Panics
2117    ///
2118    /// This function panics if `nbytes` is greater than 8.
2119    fn try_get_int_le(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2120        Ok(sign_extend(self.try_get_uint_le(nbytes)?, nbytes))
2121    }
2122
2123    /// Gets a signed n-byte integer from `self` in native-endian byte order.
2124    ///
2125    /// The current position is advanced by `nbytes`.
2126    ///
2127    /// Returns `Err(TryGetError)` when there are not enough
2128    /// remaining bytes to read the value.
2129    ///
2130    /// # Examples
2131    ///
2132    /// ```
2133    /// use bytes::Buf;
2134    ///
2135    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2136    ///     true => b"\x01\x02\x03 hello",
2137    ///     false => b"\x03\x02\x01 hello",
2138    /// };
2139    /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_ne(3));
2140    /// assert_eq!(6, buf.remaining());
2141    /// ```
2142    ///
2143    /// ```
2144    /// use bytes::{Buf, TryGetError};
2145    ///
2146    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2147    ///     true => b"\x01\x02\x03",
2148    ///     false => b"\x03\x02\x01",
2149    /// };
2150    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_ne(4));
2151    /// assert_eq!(3, buf.remaining());
2152    /// ```
2153    ///
2154    /// # Panics
2155    ///
2156    /// This function panics if `nbytes` is greater than 8.
2157    fn try_get_int_ne(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2158        if cfg!(target_endian = "big") {
2159            self.try_get_int(nbytes)
2160        } else {
2161            self.try_get_int_le(nbytes)
2162        }
2163    }
2164
2165    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
2166    /// `self` in big-endian byte order.
2167    ///
2168    /// The current position is advanced by 4.
2169    ///
2170    /// Returns `Err(TryGetError)` when there are not enough
2171    /// remaining bytes to read the value.
2172    ///
2173    /// # Examples
2174    ///
2175    /// ```
2176    /// use bytes::Buf;
2177    ///
2178    /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..];
2179    /// assert_eq!(1.2f32, buf.get_f32());
2180    /// assert_eq!(6, buf.remaining());
2181    /// ```
2182    ///
2183    /// ```
2184    /// use bytes::{Buf, TryGetError};
2185    ///
2186    /// let mut buf = &b"\x3F\x99\x99"[..];
2187    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32());
2188    /// assert_eq!(3, buf.remaining());
2189    /// ```
2190    fn try_get_f32(&mut self) -> Result<f32, TryGetError> {
2191        Ok(f32::from_bits(self.try_get_u32()?))
2192    }
2193
2194    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
2195    /// `self` in little-endian byte order.
2196    ///
2197    /// The current position is advanced by 4.
2198    ///
2199    /// Returns `Err(TryGetError)` when there are not enough
2200    /// remaining bytes to read the value.
2201    ///
2202    /// # Examples
2203    ///
2204    /// ```
2205    /// use bytes::Buf;
2206    ///
2207    /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..];
2208    /// assert_eq!(1.2f32, buf.get_f32_le());
2209    /// assert_eq!(6, buf.remaining());
2210    /// ```
2211    ///
2212    /// ```
2213    /// use bytes::{Buf, TryGetError};
2214    ///
2215    /// let mut buf = &b"\x3F\x99\x99"[..];
2216    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_le());
2217    /// assert_eq!(3, buf.remaining());
2218    /// ```
2219    fn try_get_f32_le(&mut self) -> Result<f32, TryGetError> {
2220        Ok(f32::from_bits(self.try_get_u32_le()?))
2221    }
2222
2223    /// Gets an IEEE754 single-precision (4 bytes) floating point number from
2224    /// `self` in native-endian byte order.
2225    ///
2226    /// The current position is advanced by 4.
2227    ///
2228    /// Returns `Err(TryGetError)` when there are not enough
2229    /// remaining bytes to read the value.
2230    ///
2231    /// # Examples
2232    ///
2233    /// ```
2234    /// use bytes::Buf;
2235    ///
2236    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2237    ///     true => b"\x3F\x99\x99\x9A hello",
2238    ///     false => b"\x9A\x99\x99\x3F hello",
2239    /// };
2240    /// assert_eq!(1.2f32, buf.get_f32_ne());
2241    /// assert_eq!(6, buf.remaining());
2242    /// ```
2243    ///
2244    /// ```
2245    /// use bytes::{Buf, TryGetError};
2246    ///
2247    /// let mut buf = &b"\x3F\x99\x99"[..];
2248    /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_ne());
2249    /// assert_eq!(3, buf.remaining());
2250    /// ```
2251    fn try_get_f32_ne(&mut self) -> Result<f32, TryGetError> {
2252        Ok(f32::from_bits(self.try_get_u32_ne()?))
2253    }
2254
2255    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
2256    /// `self` in big-endian byte order.
2257    ///
2258    /// The current position is advanced by 8.
2259    ///
2260    /// Returns `Err(TryGetError)` when there are not enough
2261    /// remaining bytes to read the value.
2262    ///
2263    /// # Examples
2264    ///
2265    /// ```
2266    /// use bytes::Buf;
2267    ///
2268    /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..];
2269    /// assert_eq!(1.2f64, buf.get_f64());
2270    /// assert_eq!(6, buf.remaining());
2271    /// ```
2272    ///
2273    /// ```
2274    /// use bytes::{Buf, TryGetError};
2275    ///
2276    /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..];
2277    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64());
2278    /// assert_eq!(7, buf.remaining());
2279    /// ```
2280    fn try_get_f64(&mut self) -> Result<f64, TryGetError> {
2281        Ok(f64::from_bits(self.try_get_u64()?))
2282    }
2283
2284    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
2285    /// `self` in little-endian byte order.
2286    ///
2287    /// The current position is advanced by 8.
2288    ///
2289    /// Returns `Err(TryGetError)` when there are not enough
2290    /// remaining bytes to read the value.
2291    ///
2292    /// # Examples
2293    ///
2294    /// ```
2295    /// use bytes::Buf;
2296    ///
2297    /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..];
2298    /// assert_eq!(1.2f64, buf.get_f64_le());
2299    /// assert_eq!(6, buf.remaining());
2300    /// ```
2301    ///
2302    /// ```
2303    /// use bytes::{Buf, TryGetError};
2304    ///
2305    /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..];
2306    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_le());
2307    /// assert_eq!(7, buf.remaining());
2308    /// ```
2309    fn try_get_f64_le(&mut self) -> Result<f64, TryGetError> {
2310        Ok(f64::from_bits(self.try_get_u64_le()?))
2311    }
2312
2313    /// Gets an IEEE754 double-precision (8 bytes) floating point number from
2314    /// `self` in native-endian byte order.
2315    ///
2316    /// The current position is advanced by 8.
2317    ///
2318    /// Returns `Err(TryGetError)` when there are not enough
2319    /// remaining bytes to read the value.
2320    ///
2321    /// # Examples
2322    ///
2323    /// ```
2324    /// use bytes::Buf;
2325    ///
2326    /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
2327    ///     true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello",
2328    ///     false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello",
2329    /// };
2330    /// assert_eq!(1.2f64, buf.get_f64_ne());
2331    /// assert_eq!(6, buf.remaining());
2332    /// ```
2333    ///
2334    /// ```
2335    /// use bytes::{Buf, TryGetError};
2336    ///
2337    /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..];
2338    /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_ne());
2339    /// assert_eq!(7, buf.remaining());
2340    /// ```
2341    fn try_get_f64_ne(&mut self) -> Result<f64, TryGetError> {
2342        Ok(f64::from_bits(self.try_get_u64_ne()?))
2343    }
2344
2345    /// Consumes `len` bytes inside self and returns new instance of `Bytes`
2346    /// with this data.
2347    ///
2348    /// This function may be optimized by the underlying type to avoid actual
2349    /// copies. For example, `Bytes` implementation will do a shallow copy
2350    /// (ref-count increment).
2351    ///
2352    /// # Examples
2353    ///
2354    /// ```
2355    /// use bytes::Buf;
2356    ///
2357    /// let bytes = (&b"hello world"[..]).copy_to_bytes(5);
2358    /// assert_eq!(&bytes[..], &b"hello"[..]);
2359    /// ```
2360    ///
2361    /// # Panics
2362    ///
2363    /// This function panics if `len > self.remaining()`.
2364    fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
2365        use super::BufMut;
2366
2367        if self.remaining() < len {
2368            panic_advance(&TryGetError {
2369                requested: len,
2370                available: self.remaining(),
2371            });
2372        }
2373
2374        let mut ret = crate::BytesMut::with_capacity(len);
2375        ret.put(self.take(len));
2376        ret.freeze()
2377    }
2378
2379    /// Creates an adaptor which will read at most `limit` bytes from `self`.
2380    ///
2381    /// This function returns a new instance of `Buf` which will read at most
2382    /// `limit` bytes.
2383    ///
2384    /// # Examples
2385    ///
2386    /// ```
2387    /// use bytes::{Buf, BufMut};
2388    ///
2389    /// let mut buf = b"hello world"[..].take(5);
2390    /// let mut dst = vec![];
2391    ///
2392    /// dst.put(&mut buf);
2393    /// assert_eq!(dst, b"hello");
2394    ///
2395    /// let mut buf = buf.into_inner();
2396    /// dst.clear();
2397    /// dst.put(&mut buf);
2398    /// assert_eq!(dst, b" world");
2399    /// ```
2400    fn take(self, limit: usize) -> Take<Self>
2401    where
2402        Self: Sized,
2403    {
2404        take::new(self, limit)
2405    }
2406
2407    /// Creates an adaptor which will chain this buffer with another.
2408    ///
2409    /// The returned `Buf` instance will first consume all bytes from `self`.
2410    /// Afterwards the output is equivalent to the output of next.
2411    ///
2412    /// # Examples
2413    ///
2414    /// ```
2415    /// use bytes::Buf;
2416    ///
2417    /// let mut chain = b"hello "[..].chain(&b"world"[..]);
2418    ///
2419    /// let full = chain.copy_to_bytes(11);
2420    /// assert_eq!(full.chunk(), b"hello world");
2421    /// ```
2422    fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
2423    where
2424        Self: Sized,
2425    {
2426        Chain::new(self, next)
2427    }
2428
2429    /// Creates an adaptor which implements the `Read` trait for `self`.
2430    ///
2431    /// This function returns a new value which implements `Read` by adapting
2432    /// the `Read` trait functions to the `Buf` trait functions. Given that
2433    /// `Buf` operations are infallible, none of the `Read` functions will
2434    /// return with `Err`.
2435    ///
2436    /// # Examples
2437    ///
2438    /// ```
2439    /// use bytes::{Bytes, Buf};
2440    /// use std::io::Read;
2441    ///
2442    /// let buf = Bytes::from("hello world");
2443    ///
2444    /// let mut reader = buf.reader();
2445    /// let mut dst = [0; 1024];
2446    ///
2447    /// let num = reader.read(&mut dst).unwrap();
2448    ///
2449    /// assert_eq!(11, num);
2450    /// assert_eq!(&dst[..11], &b"hello world"[..]);
2451    /// ```
2452    #[cfg(feature = "std")]
2453    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
2454    fn reader(self) -> Reader<Self>
2455    where
2456        Self: Sized,
2457    {
2458        reader::new(self)
2459    }
2460}
2461
2462macro_rules! deref_forward_buf {
2463    () => {
2464        #[inline]
2465        fn remaining(&self) -> usize {
2466            (**self).remaining()
2467        }
2468
2469        #[inline]
2470        fn chunk(&self) -> &[u8] {
2471            (**self).chunk()
2472        }
2473
2474        #[cfg(feature = "std")]
2475        #[inline]
2476        fn chunks_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
2477            (**self).chunks_vectored(dst)
2478        }
2479
2480        #[inline]
2481        fn advance(&mut self, cnt: usize) {
2482            (**self).advance(cnt)
2483        }
2484
2485        #[inline]
2486        fn has_remaining(&self) -> bool {
2487            (**self).has_remaining()
2488        }
2489
2490        #[inline]
2491        fn copy_to_slice(&mut self, dst: &mut [u8]) {
2492            (**self).copy_to_slice(dst)
2493        }
2494
2495        #[inline]
2496        fn get_u8(&mut self) -> u8 {
2497            (**self).get_u8()
2498        }
2499
2500        #[inline]
2501        fn get_i8(&mut self) -> i8 {
2502            (**self).get_i8()
2503        }
2504
2505        #[inline]
2506        fn get_u16(&mut self) -> u16 {
2507            (**self).get_u16()
2508        }
2509
2510        #[inline]
2511        fn get_u16_le(&mut self) -> u16 {
2512            (**self).get_u16_le()
2513        }
2514
2515        #[inline]
2516        fn get_u16_ne(&mut self) -> u16 {
2517            (**self).get_u16_ne()
2518        }
2519
2520        #[inline]
2521        fn get_i16(&mut self) -> i16 {
2522            (**self).get_i16()
2523        }
2524
2525        #[inline]
2526        fn get_i16_le(&mut self) -> i16 {
2527            (**self).get_i16_le()
2528        }
2529
2530        #[inline]
2531        fn get_i16_ne(&mut self) -> i16 {
2532            (**self).get_i16_ne()
2533        }
2534
2535        #[inline]
2536        fn get_u32(&mut self) -> u32 {
2537            (**self).get_u32()
2538        }
2539
2540        #[inline]
2541        fn get_u32_le(&mut self) -> u32 {
2542            (**self).get_u32_le()
2543        }
2544
2545        #[inline]
2546        fn get_u32_ne(&mut self) -> u32 {
2547            (**self).get_u32_ne()
2548        }
2549
2550        #[inline]
2551        fn get_i32(&mut self) -> i32 {
2552            (**self).get_i32()
2553        }
2554
2555        #[inline]
2556        fn get_i32_le(&mut self) -> i32 {
2557            (**self).get_i32_le()
2558        }
2559
2560        #[inline]
2561        fn get_i32_ne(&mut self) -> i32 {
2562            (**self).get_i32_ne()
2563        }
2564
2565        #[inline]
2566        fn get_u64(&mut self) -> u64 {
2567            (**self).get_u64()
2568        }
2569
2570        #[inline]
2571        fn get_u64_le(&mut self) -> u64 {
2572            (**self).get_u64_le()
2573        }
2574
2575        #[inline]
2576        fn get_u64_ne(&mut self) -> u64 {
2577            (**self).get_u64_ne()
2578        }
2579
2580        #[inline]
2581        fn get_i64(&mut self) -> i64 {
2582            (**self).get_i64()
2583        }
2584
2585        #[inline]
2586        fn get_i64_le(&mut self) -> i64 {
2587            (**self).get_i64_le()
2588        }
2589
2590        #[inline]
2591        fn get_i64_ne(&mut self) -> i64 {
2592            (**self).get_i64_ne()
2593        }
2594
2595        #[inline]
2596        fn get_u128(&mut self) -> u128 {
2597            (**self).get_u128()
2598        }
2599
2600        #[inline]
2601        fn get_u128_le(&mut self) -> u128 {
2602            (**self).get_u128_le()
2603        }
2604
2605        #[inline]
2606        fn get_u128_ne(&mut self) -> u128 {
2607            (**self).get_u128_ne()
2608        }
2609
2610        #[inline]
2611        fn get_i128(&mut self) -> i128 {
2612            (**self).get_i128()
2613        }
2614
2615        #[inline]
2616        fn get_i128_le(&mut self) -> i128 {
2617            (**self).get_i128_le()
2618        }
2619
2620        #[inline]
2621        fn get_i128_ne(&mut self) -> i128 {
2622            (**self).get_i128_ne()
2623        }
2624
2625        #[inline]
2626        fn get_uint(&mut self, nbytes: usize) -> u64 {
2627            (**self).get_uint(nbytes)
2628        }
2629
2630        #[inline]
2631        fn get_uint_le(&mut self, nbytes: usize) -> u64 {
2632            (**self).get_uint_le(nbytes)
2633        }
2634
2635        #[inline]
2636        fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
2637            (**self).get_uint_ne(nbytes)
2638        }
2639
2640        #[inline]
2641        fn get_int(&mut self, nbytes: usize) -> i64 {
2642            (**self).get_int(nbytes)
2643        }
2644
2645        #[inline]
2646        fn get_int_le(&mut self, nbytes: usize) -> i64 {
2647            (**self).get_int_le(nbytes)
2648        }
2649
2650        #[inline]
2651        fn get_int_ne(&mut self, nbytes: usize) -> i64 {
2652            (**self).get_int_ne(nbytes)
2653        }
2654
2655        #[inline]
2656        fn get_f32(&mut self) -> f32 {
2657            (**self).get_f32()
2658        }
2659
2660        #[inline]
2661        fn get_f32_le(&mut self) -> f32 {
2662            (**self).get_f32_le()
2663        }
2664
2665        #[inline]
2666        fn get_f32_ne(&mut self) -> f32 {
2667            (**self).get_f32_ne()
2668        }
2669
2670        #[inline]
2671        fn get_f64(&mut self) -> f64 {
2672            (**self).get_f64()
2673        }
2674
2675        #[inline]
2676        fn get_f64_le(&mut self) -> f64 {
2677            (**self).get_f64_le()
2678        }
2679
2680        #[inline]
2681        fn get_f64_ne(&mut self) -> f64 {
2682            (**self).get_f64_ne()
2683        }
2684
2685        #[inline]
2686        fn try_copy_to_slice(&mut self, dst: &mut [u8]) -> Result<(), TryGetError> {
2687            (**self).try_copy_to_slice(dst)
2688        }
2689
2690        #[inline]
2691        fn try_get_u8(&mut self) -> Result<u8, TryGetError> {
2692            (**self).try_get_u8()
2693        }
2694
2695        #[inline]
2696        fn try_get_i8(&mut self) -> Result<i8, TryGetError> {
2697            (**self).try_get_i8()
2698        }
2699
2700        #[inline]
2701        fn try_get_u16(&mut self) -> Result<u16, TryGetError> {
2702            (**self).try_get_u16()
2703        }
2704
2705        #[inline]
2706        fn try_get_u16_le(&mut self) -> Result<u16, TryGetError> {
2707            (**self).try_get_u16_le()
2708        }
2709
2710        #[inline]
2711        fn try_get_u16_ne(&mut self) -> Result<u16, TryGetError> {
2712            (**self).try_get_u16_ne()
2713        }
2714
2715        #[inline]
2716        fn try_get_i16(&mut self) -> Result<i16, TryGetError> {
2717            (**self).try_get_i16()
2718        }
2719
2720        #[inline]
2721        fn try_get_i16_le(&mut self) -> Result<i16, TryGetError> {
2722            (**self).try_get_i16_le()
2723        }
2724
2725        #[inline]
2726        fn try_get_i16_ne(&mut self) -> Result<i16, TryGetError> {
2727            (**self).try_get_i16_ne()
2728        }
2729
2730        #[inline]
2731        fn try_get_u32(&mut self) -> Result<u32, TryGetError> {
2732            (**self).try_get_u32()
2733        }
2734
2735        #[inline]
2736        fn try_get_u32_le(&mut self) -> Result<u32, TryGetError> {
2737            (**self).try_get_u32_le()
2738        }
2739
2740        #[inline]
2741        fn try_get_u32_ne(&mut self) -> Result<u32, TryGetError> {
2742            (**self).try_get_u32_ne()
2743        }
2744
2745        #[inline]
2746        fn try_get_i32(&mut self) -> Result<i32, TryGetError> {
2747            (**self).try_get_i32()
2748        }
2749
2750        #[inline]
2751        fn try_get_i32_le(&mut self) -> Result<i32, TryGetError> {
2752            (**self).try_get_i32_le()
2753        }
2754
2755        #[inline]
2756        fn try_get_i32_ne(&mut self) -> Result<i32, TryGetError> {
2757            (**self).try_get_i32_ne()
2758        }
2759
2760        #[inline]
2761        fn try_get_u64(&mut self) -> Result<u64, TryGetError> {
2762            (**self).try_get_u64()
2763        }
2764
2765        #[inline]
2766        fn try_get_u64_le(&mut self) -> Result<u64, TryGetError> {
2767            (**self).try_get_u64_le()
2768        }
2769
2770        #[inline]
2771        fn try_get_u64_ne(&mut self) -> Result<u64, TryGetError> {
2772            (**self).try_get_u64_ne()
2773        }
2774
2775        #[inline]
2776        fn try_get_i64(&mut self) -> Result<i64, TryGetError> {
2777            (**self).try_get_i64()
2778        }
2779
2780        #[inline]
2781        fn try_get_i64_le(&mut self) -> Result<i64, TryGetError> {
2782            (**self).try_get_i64_le()
2783        }
2784
2785        #[inline]
2786        fn try_get_i64_ne(&mut self) -> Result<i64, TryGetError> {
2787            (**self).try_get_i64_ne()
2788        }
2789
2790        #[inline]
2791        fn try_get_u128(&mut self) -> Result<u128, TryGetError> {
2792            (**self).try_get_u128()
2793        }
2794
2795        #[inline]
2796        fn try_get_u128_le(&mut self) -> Result<u128, TryGetError> {
2797            (**self).try_get_u128_le()
2798        }
2799
2800        #[inline]
2801        fn try_get_u128_ne(&mut self) -> Result<u128, TryGetError> {
2802            (**self).try_get_u128_ne()
2803        }
2804
2805        #[inline]
2806        fn try_get_i128(&mut self) -> Result<i128, TryGetError> {
2807            (**self).try_get_i128()
2808        }
2809
2810        #[inline]
2811        fn try_get_i128_le(&mut self) -> Result<i128, TryGetError> {
2812            (**self).try_get_i128_le()
2813        }
2814
2815        #[inline]
2816        fn try_get_i128_ne(&mut self) -> Result<i128, TryGetError> {
2817            (**self).try_get_i128_ne()
2818        }
2819
2820        #[inline]
2821        fn try_get_uint(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
2822            (**self).try_get_uint(nbytes)
2823        }
2824
2825        #[inline]
2826        fn try_get_uint_le(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
2827            (**self).try_get_uint_le(nbytes)
2828        }
2829
2830        #[inline]
2831        fn try_get_uint_ne(&mut self, nbytes: usize) -> Result<u64, TryGetError> {
2832            (**self).try_get_uint_ne(nbytes)
2833        }
2834
2835        #[inline]
2836        fn try_get_int(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2837            (**self).try_get_int(nbytes)
2838        }
2839
2840        #[inline]
2841        fn try_get_int_le(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2842            (**self).try_get_int_le(nbytes)
2843        }
2844
2845        #[inline]
2846        fn try_get_int_ne(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
2847            (**self).try_get_int_ne(nbytes)
2848        }
2849
2850        #[inline]
2851        fn try_get_f32(&mut self) -> Result<f32, TryGetError> {
2852            (**self).try_get_f32()
2853        }
2854
2855        #[inline]
2856        fn try_get_f32_le(&mut self) -> Result<f32, TryGetError> {
2857            (**self).try_get_f32_le()
2858        }
2859
2860        #[inline]
2861        fn try_get_f32_ne(&mut self) -> Result<f32, TryGetError> {
2862            (**self).try_get_f32_ne()
2863        }
2864
2865        #[inline]
2866        fn try_get_f64(&mut self) -> Result<f64, TryGetError> {
2867            (**self).try_get_f64()
2868        }
2869
2870        #[inline]
2871        fn try_get_f64_le(&mut self) -> Result<f64, TryGetError> {
2872            (**self).try_get_f64_le()
2873        }
2874
2875        #[inline]
2876        fn try_get_f64_ne(&mut self) -> Result<f64, TryGetError> {
2877            (**self).try_get_f64_ne()
2878        }
2879
2880        #[inline]
2881        fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
2882            (**self).copy_to_bytes(len)
2883        }
2884    };
2885}
2886
2887impl<T: Buf + ?Sized> Buf for &mut T {
2888    deref_forward_buf!();
2889}
2890
2891impl<T: Buf + ?Sized> Buf for Box<T> {
2892    deref_forward_buf!();
2893}
2894
2895impl Buf for &[u8] {
2896    #[inline]
2897    fn remaining(&self) -> usize {
2898        self.len()
2899    }
2900
2901    #[inline]
2902    fn chunk(&self) -> &[u8] {
2903        self
2904    }
2905
2906    #[inline]
2907    fn advance(&mut self, cnt: usize) {
2908        if self.len() < cnt {
2909            panic_advance(&TryGetError {
2910                requested: cnt,
2911                available: self.len(),
2912            });
2913        }
2914
2915        *self = &self[cnt..];
2916    }
2917
2918    #[inline]
2919    fn copy_to_slice(&mut self, dst: &mut [u8]) {
2920        if self.len() < dst.len() {
2921            panic_advance(&TryGetError {
2922                requested: dst.len(),
2923                available: self.len(),
2924            });
2925        }
2926
2927        dst.copy_from_slice(&self[..dst.len()]);
2928        self.advance(dst.len());
2929    }
2930}
2931
2932#[cfg(feature = "std")]
2933impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
2934    #[inline]
2935    fn remaining(&self) -> usize {
2936        saturating_sub_usize_u64(self.get_ref().as_ref().len(), self.position())
2937    }
2938
2939    #[inline]
2940    fn chunk(&self) -> &[u8] {
2941        let slice = self.get_ref().as_ref();
2942        let pos = min_u64_usize(self.position(), slice.len());
2943        &slice[pos..]
2944    }
2945
2946    #[inline]
2947    fn advance(&mut self, cnt: usize) {
2948        let len = self.get_ref().as_ref().len();
2949        let pos = self.position();
2950
2951        // We intentionally allow `cnt == 0` here even if `pos > len`.
2952        let max_cnt = saturating_sub_usize_u64(len, pos);
2953        if cnt > max_cnt {
2954            panic_advance(&TryGetError {
2955                requested: cnt,
2956                available: max_cnt,
2957            });
2958        }
2959
2960        // This will not overflow because either `cnt == 0` or the sum is not
2961        // greater than `len`.
2962        self.set_position(pos + cnt as u64);
2963    }
2964}
2965
2966// The existence of this function makes the compiler catch if the Buf
2967// trait is "object-safe" or not.
2968fn _assert_trait_object(_b: &dyn Buf) {}
2969
2970#[cfg(test)]
2971mod tests {
2972    use super::sign_extend;
2973
2974    #[test]
2975    fn sign_extend_zero_bytes() {
2976        assert_eq!(sign_extend(u64::MAX, 0), 0);
2977    }
2978
2979    #[test]
2980    #[cfg(debug_assertions)]
2981    #[should_panic(expected = "nbytes must be in [0, 8]")]
2982    fn sign_extend_more_than_eight_bytes_panics_in_debug() {
2983        sign_extend(0, 9);
2984    }
2985}