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