1 use core::{cmp, mem, ptr};
2
3 #[cfg(feature = "std")]
4 use std::io::IoSlice;
5
6 use alloc::boxed::Box;
7
8 macro_rules! buf_get_impl {
9 ($this:ident, $typ:tt::$conv:tt) => {{
10 const SIZE: usize = mem::size_of::<$typ>();
11 // try to convert directly from the bytes
12 // this Option<ret> trick is to avoid keeping a borrow on self
13 // when advance() is called (mut borrow) and to call bytes() only once
14 let ret = $this
15 .bytes()
16 .get(..SIZE)
17 .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
18
19 if let Some(ret) = ret {
20 // if the direct conversion was possible, advance and return
21 $this.advance(SIZE);
22 return ret;
23 } else {
24 // if not we copy the bytes in a temp buffer then convert
25 let mut buf = [0; SIZE];
26 $this.copy_to_slice(&mut buf); // (do the advance)
27 return $typ::$conv(buf);
28 }
29 }};
30 (le => $this:ident, $typ:tt, $len_to_read:expr) => {{
31 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
32
33 // The same trick as above does not improve the best case speed.
34 // It seems to be linked to the way the method is optimised by the compiler
35 let mut buf = [0; (mem::size_of::<$typ>())];
36 $this.copy_to_slice(&mut buf[..($len_to_read)]);
37 return $typ::from_le_bytes(buf);
38 }};
39 (be => $this:ident, $typ:tt, $len_to_read:expr) => {{
40 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
41
42 let mut buf = [0; (mem::size_of::<$typ>())];
43 $this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
44 return $typ::from_be_bytes(buf);
45 }};
46 }
47
48 /// Read bytes from a buffer.
49 ///
50 /// A buffer stores bytes in memory such that read operations are infallible.
51 /// The underlying storage may or may not be in contiguous memory. A `Buf` value
52 /// is a cursor into the buffer. Reading from `Buf` advances the cursor
53 /// position. It can be thought of as an efficient `Iterator` for collections of
54 /// bytes.
55 ///
56 /// The simplest `Buf` is a `&[u8]`.
57 ///
58 /// ```
59 /// use bytes::Buf;
60 ///
61 /// let mut buf = &b"hello world"[..];
62 ///
63 /// assert_eq!(b'h', buf.get_u8());
64 /// assert_eq!(b'e', buf.get_u8());
65 /// assert_eq!(b'l', buf.get_u8());
66 ///
67 /// let mut rest = [0; 8];
68 /// buf.copy_to_slice(&mut rest);
69 ///
70 /// assert_eq!(&rest[..], &b"lo world"[..]);
71 /// ```
72 pub trait Buf {
73 /// Returns the number of bytes between the current position and the end of
74 /// the buffer.
75 ///
76 /// This value is greater than or equal to the length of the slice returned
77 /// by `bytes`.
78 ///
79 /// # Examples
80 ///
81 /// ```
82 /// use bytes::Buf;
83 ///
84 /// let mut buf = &b"hello world"[..];
85 ///
86 /// assert_eq!(buf.remaining(), 11);
87 ///
88 /// buf.get_u8();
89 ///
90 /// assert_eq!(buf.remaining(), 10);
91 /// ```
92 ///
93 /// # Implementer notes
94 ///
95 /// Implementations of `remaining` should ensure that the return value does
96 /// not change unless a call is made to `advance` or any other function that
97 /// is documented to change the `Buf`'s current position.
remaining(&self) -> usize98 fn remaining(&self) -> usize;
99
100 /// Returns a slice starting at the current position and of length between 0
101 /// and `Buf::remaining()`. Note that this *can* return shorter slice (this allows
102 /// non-continuous internal representation).
103 ///
104 /// This is a lower level function. Most operations are done with other
105 /// functions.
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// use bytes::Buf;
111 ///
112 /// let mut buf = &b"hello world"[..];
113 ///
114 /// assert_eq!(buf.bytes(), &b"hello world"[..]);
115 ///
116 /// buf.advance(6);
117 ///
118 /// assert_eq!(buf.bytes(), &b"world"[..]);
119 /// ```
120 ///
121 /// # Implementer notes
122 ///
123 /// This function should never panic. Once the end of the buffer is reached,
124 /// i.e., `Buf::remaining` returns 0, calls to `bytes` should return an
125 /// empty slice.
bytes(&self) -> &[u8]126 fn bytes(&self) -> &[u8];
127
128 /// Fills `dst` with potentially multiple slices starting at `self`'s
129 /// current position.
130 ///
131 /// If the `Buf` is backed by disjoint slices of bytes, `bytes_vectored` enables
132 /// fetching more than one slice at once. `dst` is a slice of `IoSlice`
133 /// references, enabling the slice to be directly used with [`writev`]
134 /// without any further conversion. The sum of the lengths of all the
135 /// buffers in `dst` will be less than or equal to `Buf::remaining()`.
136 ///
137 /// The entries in `dst` will be overwritten, but the data **contained** by
138 /// the slices **will not** be modified. If `bytes_vectored` does not fill every
139 /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices
140 /// in `self.
141 ///
142 /// This is a lower level function. Most operations are done with other
143 /// functions.
144 ///
145 /// # Implementer notes
146 ///
147 /// This function should never panic. Once the end of the buffer is reached,
148 /// i.e., `Buf::remaining` returns 0, calls to `bytes_vectored` must return 0
149 /// without mutating `dst`.
150 ///
151 /// Implementations should also take care to properly handle being called
152 /// with `dst` being a zero length slice.
153 ///
154 /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
155 #[cfg(feature = "std")]
bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize156 fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
157 if dst.is_empty() {
158 return 0;
159 }
160
161 if self.has_remaining() {
162 dst[0] = IoSlice::new(self.bytes());
163 1
164 } else {
165 0
166 }
167 }
168
169 /// Advance the internal cursor of the Buf
170 ///
171 /// The next call to `bytes` will return a slice starting `cnt` bytes
172 /// further into the underlying buffer.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// use bytes::Buf;
178 ///
179 /// let mut buf = &b"hello world"[..];
180 ///
181 /// assert_eq!(buf.bytes(), &b"hello world"[..]);
182 ///
183 /// buf.advance(6);
184 ///
185 /// assert_eq!(buf.bytes(), &b"world"[..]);
186 /// ```
187 ///
188 /// # Panics
189 ///
190 /// This function **may** panic if `cnt > self.remaining()`.
191 ///
192 /// # Implementer notes
193 ///
194 /// It is recommended for implementations of `advance` to panic if `cnt >
195 /// self.remaining()`. If the implementation does not panic, the call must
196 /// behave as if `cnt == self.remaining()`.
197 ///
198 /// A call with `cnt == 0` should never panic and be a no-op.
advance(&mut self, cnt: usize)199 fn advance(&mut self, cnt: usize);
200
201 /// Returns true if there are any more bytes to consume
202 ///
203 /// This is equivalent to `self.remaining() != 0`.
204 ///
205 /// # Examples
206 ///
207 /// ```
208 /// use bytes::Buf;
209 ///
210 /// let mut buf = &b"a"[..];
211 ///
212 /// assert!(buf.has_remaining());
213 ///
214 /// buf.get_u8();
215 ///
216 /// assert!(!buf.has_remaining());
217 /// ```
has_remaining(&self) -> bool218 fn has_remaining(&self) -> bool {
219 self.remaining() > 0
220 }
221
222 /// Copies bytes from `self` into `dst`.
223 ///
224 /// The cursor is advanced by the number of bytes copied. `self` must have
225 /// enough remaining bytes to fill `dst`.
226 ///
227 /// # Examples
228 ///
229 /// ```
230 /// use bytes::Buf;
231 ///
232 /// let mut buf = &b"hello world"[..];
233 /// let mut dst = [0; 5];
234 ///
235 /// buf.copy_to_slice(&mut dst);
236 /// assert_eq!(&b"hello"[..], &dst);
237 /// assert_eq!(6, buf.remaining());
238 /// ```
239 ///
240 /// # Panics
241 ///
242 /// This function panics if `self.remaining() < dst.len()`
copy_to_slice(&mut self, dst: &mut [u8])243 fn copy_to_slice(&mut self, dst: &mut [u8]) {
244 let mut off = 0;
245
246 assert!(self.remaining() >= dst.len());
247
248 while off < dst.len() {
249 let cnt;
250
251 unsafe {
252 let src = self.bytes();
253 cnt = cmp::min(src.len(), dst.len() - off);
254
255 ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
256
257 off += cnt;
258 }
259
260 self.advance(cnt);
261 }
262 }
263
264 /// Gets an unsigned 8 bit integer from `self`.
265 ///
266 /// The current position is advanced by 1.
267 ///
268 /// # Examples
269 ///
270 /// ```
271 /// use bytes::Buf;
272 ///
273 /// let mut buf = &b"\x08 hello"[..];
274 /// assert_eq!(8, buf.get_u8());
275 /// ```
276 ///
277 /// # Panics
278 ///
279 /// This function panics if there is no more remaining data in `self`.
get_u8(&mut self) -> u8280 fn get_u8(&mut self) -> u8 {
281 assert!(self.remaining() >= 1);
282 let ret = self.bytes()[0];
283 self.advance(1);
284 ret
285 }
286
287 /// Gets a signed 8 bit integer from `self`.
288 ///
289 /// The current position is advanced by 1.
290 ///
291 /// # Examples
292 ///
293 /// ```
294 /// use bytes::Buf;
295 ///
296 /// let mut buf = &b"\x08 hello"[..];
297 /// assert_eq!(8, buf.get_i8());
298 /// ```
299 ///
300 /// # Panics
301 ///
302 /// This function panics if there is no more remaining data in `self`.
get_i8(&mut self) -> i8303 fn get_i8(&mut self) -> i8 {
304 assert!(self.remaining() >= 1);
305 let ret = self.bytes()[0] as i8;
306 self.advance(1);
307 ret
308 }
309
310 /// Gets an unsigned 16 bit integer from `self` in big-endian byte order.
311 ///
312 /// The current position is advanced by 2.
313 ///
314 /// # Examples
315 ///
316 /// ```
317 /// use bytes::Buf;
318 ///
319 /// let mut buf = &b"\x08\x09 hello"[..];
320 /// assert_eq!(0x0809, buf.get_u16());
321 /// ```
322 ///
323 /// # Panics
324 ///
325 /// This function panics if there is not enough remaining data in `self`.
get_u16(&mut self) -> u16326 fn get_u16(&mut self) -> u16 {
327 buf_get_impl!(self, u16::from_be_bytes);
328 }
329
330 /// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
331 ///
332 /// The current position is advanced by 2.
333 ///
334 /// # Examples
335 ///
336 /// ```
337 /// use bytes::Buf;
338 ///
339 /// let mut buf = &b"\x09\x08 hello"[..];
340 /// assert_eq!(0x0809, buf.get_u16_le());
341 /// ```
342 ///
343 /// # Panics
344 ///
345 /// This function panics if there is not enough remaining data in `self`.
get_u16_le(&mut self) -> u16346 fn get_u16_le(&mut self) -> u16 {
347 buf_get_impl!(self, u16::from_le_bytes);
348 }
349
350 /// Gets a signed 16 bit integer from `self` in big-endian byte order.
351 ///
352 /// The current position is advanced by 2.
353 ///
354 /// # Examples
355 ///
356 /// ```
357 /// use bytes::Buf;
358 ///
359 /// let mut buf = &b"\x08\x09 hello"[..];
360 /// assert_eq!(0x0809, buf.get_i16());
361 /// ```
362 ///
363 /// # Panics
364 ///
365 /// This function panics if there is not enough remaining data in `self`.
get_i16(&mut self) -> i16366 fn get_i16(&mut self) -> i16 {
367 buf_get_impl!(self, i16::from_be_bytes);
368 }
369
370 /// Gets a signed 16 bit integer from `self` in little-endian byte order.
371 ///
372 /// The current position is advanced by 2.
373 ///
374 /// # Examples
375 ///
376 /// ```
377 /// use bytes::Buf;
378 ///
379 /// let mut buf = &b"\x09\x08 hello"[..];
380 /// assert_eq!(0x0809, buf.get_i16_le());
381 /// ```
382 ///
383 /// # Panics
384 ///
385 /// This function panics if there is not enough remaining data in `self`.
get_i16_le(&mut self) -> i16386 fn get_i16_le(&mut self) -> i16 {
387 buf_get_impl!(self, i16::from_le_bytes);
388 }
389
390 /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
391 ///
392 /// The current position is advanced by 4.
393 ///
394 /// # Examples
395 ///
396 /// ```
397 /// use bytes::Buf;
398 ///
399 /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
400 /// assert_eq!(0x0809A0A1, buf.get_u32());
401 /// ```
402 ///
403 /// # Panics
404 ///
405 /// This function panics if there is not enough remaining data in `self`.
get_u32(&mut self) -> u32406 fn get_u32(&mut self) -> u32 {
407 buf_get_impl!(self, u32::from_be_bytes);
408 }
409
410 /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
411 ///
412 /// The current position is advanced by 4.
413 ///
414 /// # Examples
415 ///
416 /// ```
417 /// use bytes::Buf;
418 ///
419 /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
420 /// assert_eq!(0x0809A0A1, buf.get_u32_le());
421 /// ```
422 ///
423 /// # Panics
424 ///
425 /// This function panics if there is not enough remaining data in `self`.
get_u32_le(&mut self) -> u32426 fn get_u32_le(&mut self) -> u32 {
427 buf_get_impl!(self, u32::from_le_bytes);
428 }
429
430 /// Gets a signed 32 bit integer from `self` in big-endian byte order.
431 ///
432 /// The current position is advanced by 4.
433 ///
434 /// # Examples
435 ///
436 /// ```
437 /// use bytes::Buf;
438 ///
439 /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
440 /// assert_eq!(0x0809A0A1, buf.get_i32());
441 /// ```
442 ///
443 /// # Panics
444 ///
445 /// This function panics if there is not enough remaining data in `self`.
get_i32(&mut self) -> i32446 fn get_i32(&mut self) -> i32 {
447 buf_get_impl!(self, i32::from_be_bytes);
448 }
449
450 /// Gets a signed 32 bit integer from `self` in little-endian byte order.
451 ///
452 /// The current position is advanced by 4.
453 ///
454 /// # Examples
455 ///
456 /// ```
457 /// use bytes::Buf;
458 ///
459 /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
460 /// assert_eq!(0x0809A0A1, buf.get_i32_le());
461 /// ```
462 ///
463 /// # Panics
464 ///
465 /// This function panics if there is not enough remaining data in `self`.
get_i32_le(&mut self) -> i32466 fn get_i32_le(&mut self) -> i32 {
467 buf_get_impl!(self, i32::from_le_bytes);
468 }
469
470 /// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
471 ///
472 /// The current position is advanced by 8.
473 ///
474 /// # Examples
475 ///
476 /// ```
477 /// use bytes::Buf;
478 ///
479 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
480 /// assert_eq!(0x0102030405060708, buf.get_u64());
481 /// ```
482 ///
483 /// # Panics
484 ///
485 /// This function panics if there is not enough remaining data in `self`.
get_u64(&mut self) -> u64486 fn get_u64(&mut self) -> u64 {
487 buf_get_impl!(self, u64::from_be_bytes);
488 }
489
490 /// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
491 ///
492 /// The current position is advanced by 8.
493 ///
494 /// # Examples
495 ///
496 /// ```
497 /// use bytes::Buf;
498 ///
499 /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
500 /// assert_eq!(0x0102030405060708, buf.get_u64_le());
501 /// ```
502 ///
503 /// # Panics
504 ///
505 /// This function panics if there is not enough remaining data in `self`.
get_u64_le(&mut self) -> u64506 fn get_u64_le(&mut self) -> u64 {
507 buf_get_impl!(self, u64::from_le_bytes);
508 }
509
510 /// Gets a signed 64 bit integer from `self` in big-endian byte order.
511 ///
512 /// The current position is advanced by 8.
513 ///
514 /// # Examples
515 ///
516 /// ```
517 /// use bytes::Buf;
518 ///
519 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
520 /// assert_eq!(0x0102030405060708, buf.get_i64());
521 /// ```
522 ///
523 /// # Panics
524 ///
525 /// This function panics if there is not enough remaining data in `self`.
get_i64(&mut self) -> i64526 fn get_i64(&mut self) -> i64 {
527 buf_get_impl!(self, i64::from_be_bytes);
528 }
529
530 /// Gets a signed 64 bit integer from `self` in little-endian byte order.
531 ///
532 /// The current position is advanced by 8.
533 ///
534 /// # Examples
535 ///
536 /// ```
537 /// use bytes::Buf;
538 ///
539 /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
540 /// assert_eq!(0x0102030405060708, buf.get_i64_le());
541 /// ```
542 ///
543 /// # Panics
544 ///
545 /// This function panics if there is not enough remaining data in `self`.
get_i64_le(&mut self) -> i64546 fn get_i64_le(&mut self) -> i64 {
547 buf_get_impl!(self, i64::from_le_bytes);
548 }
549
550 /// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
551 ///
552 /// The current position is advanced by 16.
553 ///
554 /// # Examples
555 ///
556 /// ```
557 /// use bytes::Buf;
558 ///
559 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
560 /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128());
561 /// ```
562 ///
563 /// # Panics
564 ///
565 /// This function panics if there is not enough remaining data in `self`.
get_u128(&mut self) -> u128566 fn get_u128(&mut self) -> u128 {
567 buf_get_impl!(self, u128::from_be_bytes);
568 }
569
570 /// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
571 ///
572 /// The current position is advanced by 16.
573 ///
574 /// # Examples
575 ///
576 /// ```
577 /// use bytes::Buf;
578 ///
579 /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
580 /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le());
581 /// ```
582 ///
583 /// # Panics
584 ///
585 /// This function panics if there is not enough remaining data in `self`.
get_u128_le(&mut self) -> u128586 fn get_u128_le(&mut self) -> u128 {
587 buf_get_impl!(self, u128::from_le_bytes);
588 }
589
590 /// Gets a signed 128 bit integer from `self` in big-endian byte order.
591 ///
592 /// The current position is advanced by 16.
593 ///
594 /// # Examples
595 ///
596 /// ```
597 /// use bytes::Buf;
598 ///
599 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
600 /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128());
601 /// ```
602 ///
603 /// # Panics
604 ///
605 /// This function panics if there is not enough remaining data in `self`.
get_i128(&mut self) -> i128606 fn get_i128(&mut self) -> i128 {
607 buf_get_impl!(self, i128::from_be_bytes);
608 }
609
610 /// Gets a signed 128 bit integer from `self` in little-endian byte order.
611 ///
612 /// The current position is advanced by 16.
613 ///
614 /// # Examples
615 ///
616 /// ```
617 /// use bytes::Buf;
618 ///
619 /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
620 /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le());
621 /// ```
622 ///
623 /// # Panics
624 ///
625 /// This function panics if there is not enough remaining data in `self`.
get_i128_le(&mut self) -> i128626 fn get_i128_le(&mut self) -> i128 {
627 buf_get_impl!(self, i128::from_le_bytes);
628 }
629
630 /// Gets an unsigned n-byte integer from `self` in big-endian byte order.
631 ///
632 /// The current position is advanced by `nbytes`.
633 ///
634 /// # Examples
635 ///
636 /// ```
637 /// use bytes::Buf;
638 ///
639 /// let mut buf = &b"\x01\x02\x03 hello"[..];
640 /// assert_eq!(0x010203, buf.get_uint(3));
641 /// ```
642 ///
643 /// # Panics
644 ///
645 /// This function panics if there is not enough remaining data in `self`.
get_uint(&mut self, nbytes: usize) -> u64646 fn get_uint(&mut self, nbytes: usize) -> u64 {
647 buf_get_impl!(be => self, u64, nbytes);
648 }
649
650 /// Gets an unsigned n-byte integer from `self` in little-endian byte order.
651 ///
652 /// The current position is advanced by `nbytes`.
653 ///
654 /// # Examples
655 ///
656 /// ```
657 /// use bytes::Buf;
658 ///
659 /// let mut buf = &b"\x03\x02\x01 hello"[..];
660 /// assert_eq!(0x010203, buf.get_uint_le(3));
661 /// ```
662 ///
663 /// # Panics
664 ///
665 /// This function panics if there is not enough remaining data in `self`.
get_uint_le(&mut self, nbytes: usize) -> u64666 fn get_uint_le(&mut self, nbytes: usize) -> u64 {
667 buf_get_impl!(le => self, u64, nbytes);
668 }
669
670 /// Gets a signed n-byte integer from `self` in big-endian byte order.
671 ///
672 /// The current position is advanced by `nbytes`.
673 ///
674 /// # Examples
675 ///
676 /// ```
677 /// use bytes::Buf;
678 ///
679 /// let mut buf = &b"\x01\x02\x03 hello"[..];
680 /// assert_eq!(0x010203, buf.get_int(3));
681 /// ```
682 ///
683 /// # Panics
684 ///
685 /// This function panics if there is not enough remaining data in `self`.
get_int(&mut self, nbytes: usize) -> i64686 fn get_int(&mut self, nbytes: usize) -> i64 {
687 buf_get_impl!(be => self, i64, nbytes);
688 }
689
690 /// Gets a signed n-byte integer from `self` in little-endian byte order.
691 ///
692 /// The current position is advanced by `nbytes`.
693 ///
694 /// # Examples
695 ///
696 /// ```
697 /// use bytes::Buf;
698 ///
699 /// let mut buf = &b"\x03\x02\x01 hello"[..];
700 /// assert_eq!(0x010203, buf.get_int_le(3));
701 /// ```
702 ///
703 /// # Panics
704 ///
705 /// This function panics if there is not enough remaining data in `self`.
get_int_le(&mut self, nbytes: usize) -> i64706 fn get_int_le(&mut self, nbytes: usize) -> i64 {
707 buf_get_impl!(le => self, i64, nbytes);
708 }
709
710 /// Gets an IEEE754 single-precision (4 bytes) floating point number from
711 /// `self` in big-endian byte order.
712 ///
713 /// The current position is advanced by 4.
714 ///
715 /// # Examples
716 ///
717 /// ```
718 /// use bytes::Buf;
719 ///
720 /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..];
721 /// assert_eq!(1.2f32, buf.get_f32());
722 /// ```
723 ///
724 /// # Panics
725 ///
726 /// This function panics if there is not enough remaining data in `self`.
get_f32(&mut self) -> f32727 fn get_f32(&mut self) -> f32 {
728 f32::from_bits(Self::get_u32(self))
729 }
730
731 /// Gets an IEEE754 single-precision (4 bytes) floating point number from
732 /// `self` in little-endian byte order.
733 ///
734 /// The current position is advanced by 4.
735 ///
736 /// # Examples
737 ///
738 /// ```
739 /// use bytes::Buf;
740 ///
741 /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..];
742 /// assert_eq!(1.2f32, buf.get_f32_le());
743 /// ```
744 ///
745 /// # Panics
746 ///
747 /// This function panics if there is not enough remaining data in `self`.
get_f32_le(&mut self) -> f32748 fn get_f32_le(&mut self) -> f32 {
749 f32::from_bits(Self::get_u32_le(self))
750 }
751
752 /// Gets an IEEE754 double-precision (8 bytes) floating point number from
753 /// `self` in big-endian byte order.
754 ///
755 /// The current position is advanced by 8.
756 ///
757 /// # Examples
758 ///
759 /// ```
760 /// use bytes::Buf;
761 ///
762 /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..];
763 /// assert_eq!(1.2f64, buf.get_f64());
764 /// ```
765 ///
766 /// # Panics
767 ///
768 /// This function panics if there is not enough remaining data in `self`.
get_f64(&mut self) -> f64769 fn get_f64(&mut self) -> f64 {
770 f64::from_bits(Self::get_u64(self))
771 }
772
773 /// Gets an IEEE754 double-precision (8 bytes) floating point number from
774 /// `self` in little-endian byte order.
775 ///
776 /// The current position is advanced by 8.
777 ///
778 /// # Examples
779 ///
780 /// ```
781 /// use bytes::Buf;
782 ///
783 /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..];
784 /// assert_eq!(1.2f64, buf.get_f64_le());
785 /// ```
786 ///
787 /// # Panics
788 ///
789 /// This function panics if there is not enough remaining data in `self`.
get_f64_le(&mut self) -> f64790 fn get_f64_le(&mut self) -> f64 {
791 f64::from_bits(Self::get_u64_le(self))
792 }
793
794 /// Consumes remaining bytes inside self and returns new instance of `Bytes`
795 ///
796 /// # Examples
797 ///
798 /// ```
799 /// use bytes::Buf;
800 ///
801 /// let bytes = (&b"hello world"[..]).to_bytes();
802 /// assert_eq!(&bytes[..], &b"hello world"[..]);
803 /// ```
to_bytes(&mut self) -> crate::Bytes804 fn to_bytes(&mut self) -> crate::Bytes {
805 use super::BufMut;
806 let mut ret = crate::BytesMut::with_capacity(self.remaining());
807 ret.put(self);
808 ret.freeze()
809 }
810 }
811
812 macro_rules! deref_forward_buf {
813 () => {
814 fn remaining(&self) -> usize {
815 (**self).remaining()
816 }
817
818 fn bytes(&self) -> &[u8] {
819 (**self).bytes()
820 }
821
822 #[cfg(feature = "std")]
823 fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
824 (**self).bytes_vectored(dst)
825 }
826
827 fn advance(&mut self, cnt: usize) {
828 (**self).advance(cnt)
829 }
830
831 fn has_remaining(&self) -> bool {
832 (**self).has_remaining()
833 }
834
835 fn copy_to_slice(&mut self, dst: &mut [u8]) {
836 (**self).copy_to_slice(dst)
837 }
838
839 fn get_u8(&mut self) -> u8 {
840 (**self).get_u8()
841 }
842
843 fn get_i8(&mut self) -> i8 {
844 (**self).get_i8()
845 }
846
847 fn get_u16(&mut self) -> u16 {
848 (**self).get_u16()
849 }
850
851 fn get_u16_le(&mut self) -> u16 {
852 (**self).get_u16_le()
853 }
854
855 fn get_i16(&mut self) -> i16 {
856 (**self).get_i16()
857 }
858
859 fn get_i16_le(&mut self) -> i16 {
860 (**self).get_i16_le()
861 }
862
863 fn get_u32(&mut self) -> u32 {
864 (**self).get_u32()
865 }
866
867 fn get_u32_le(&mut self) -> u32 {
868 (**self).get_u32_le()
869 }
870
871 fn get_i32(&mut self) -> i32 {
872 (**self).get_i32()
873 }
874
875 fn get_i32_le(&mut self) -> i32 {
876 (**self).get_i32_le()
877 }
878
879 fn get_u64(&mut self) -> u64 {
880 (**self).get_u64()
881 }
882
883 fn get_u64_le(&mut self) -> u64 {
884 (**self).get_u64_le()
885 }
886
887 fn get_i64(&mut self) -> i64 {
888 (**self).get_i64()
889 }
890
891 fn get_i64_le(&mut self) -> i64 {
892 (**self).get_i64_le()
893 }
894
895 fn get_uint(&mut self, nbytes: usize) -> u64 {
896 (**self).get_uint(nbytes)
897 }
898
899 fn get_uint_le(&mut self, nbytes: usize) -> u64 {
900 (**self).get_uint_le(nbytes)
901 }
902
903 fn get_int(&mut self, nbytes: usize) -> i64 {
904 (**self).get_int(nbytes)
905 }
906
907 fn get_int_le(&mut self, nbytes: usize) -> i64 {
908 (**self).get_int_le(nbytes)
909 }
910
911 fn to_bytes(&mut self) -> crate::Bytes {
912 (**self).to_bytes()
913 }
914 };
915 }
916
917 impl<T: Buf + ?Sized> Buf for &mut T {
918 deref_forward_buf!();
919 }
920
921 impl<T: Buf + ?Sized> Buf for Box<T> {
922 deref_forward_buf!();
923 }
924
925 impl Buf for &[u8] {
926 #[inline]
remaining(&self) -> usize927 fn remaining(&self) -> usize {
928 self.len()
929 }
930
931 #[inline]
bytes(&self) -> &[u8]932 fn bytes(&self) -> &[u8] {
933 self
934 }
935
936 #[inline]
advance(&mut self, cnt: usize)937 fn advance(&mut self, cnt: usize) {
938 *self = &self[cnt..];
939 }
940 }
941
942 impl Buf for Option<[u8; 1]> {
remaining(&self) -> usize943 fn remaining(&self) -> usize {
944 if self.is_some() {
945 1
946 } else {
947 0
948 }
949 }
950
bytes(&self) -> &[u8]951 fn bytes(&self) -> &[u8] {
952 self.as_ref()
953 .map(AsRef::as_ref)
954 .unwrap_or(Default::default())
955 }
956
advance(&mut self, cnt: usize)957 fn advance(&mut self, cnt: usize) {
958 if cnt == 0 {
959 return;
960 }
961
962 if self.is_none() {
963 panic!("overflow");
964 } else {
965 assert_eq!(1, cnt);
966 *self = None;
967 }
968 }
969 }
970
971 #[cfg(feature = "std")]
972 impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
remaining(&self) -> usize973 fn remaining(&self) -> usize {
974 let len = self.get_ref().as_ref().len();
975 let pos = self.position();
976
977 if pos >= len as u64 {
978 return 0;
979 }
980
981 len - pos as usize
982 }
983
bytes(&self) -> &[u8]984 fn bytes(&self) -> &[u8] {
985 let len = self.get_ref().as_ref().len();
986 let pos = self.position();
987
988 if pos >= len as u64 {
989 return &[];
990 }
991
992 &self.get_ref().as_ref()[pos as usize..]
993 }
994
advance(&mut self, cnt: usize)995 fn advance(&mut self, cnt: usize) {
996 let pos = (self.position() as usize)
997 .checked_add(cnt)
998 .expect("overflow");
999
1000 assert!(pos <= self.get_ref().as_ref().len());
1001 self.set_position(pos as u64);
1002 }
1003 }
1004
1005 // The existence of this function makes the compiler catch if the Buf
1006 // trait is "object-safe" or not.
_assert_trait_object(_b: &dyn Buf)1007 fn _assert_trait_object(_b: &dyn Buf) {}
1008