From c6bf96c9b62e08473dd1bcd74d9a4665d63e9e06 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Sat, 4 Jan 2025 08:24:51 +0100 Subject: [PATCH 01/15] Added fallible try_get methods to Buf --- src/buf/buf_impl.rs | 962 +++++++++++++++++++++++++++++++++++++++++++- src/buf/mod.rs | 2 +- src/lib.rs | 2 +- 3 files changed, 963 insertions(+), 3 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index b09d356bd..0c0467932 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -10,6 +10,23 @@ use std::io::IoSlice; use alloc::boxed::Box; +/// An error which occurred while attempting +/// to get a value from a [`Buf`](trait.Buf.html). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TryGetError { + /// Indicates that there were not enough remaining + /// bytes in the buffer to read a value. + NotEnoughBytes, +} + +impl std::fmt::Display for TryGetError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + TryGetError::NotEnoughBytes => write!(f, "Not enough bytes in buffer to read value"), + } + } +} + macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); @@ -66,6 +83,35 @@ macro_rules! buf_get_impl { }}; } +macro_rules! buf_try_get_impl { + ($this:ident, $typ:tt::$conv:tt) => {{ + const SIZE: usize = core::mem::size_of::<$typ>(); + + if $this.remaining() < SIZE { + return Err(TryGetError::NotEnoughBytes); + } + + // try to convert directly from the bytes + // this Option trick is to avoid keeping a borrow on self + // when advance() is called (mut borrow) and to call bytes() only once + let ret = $this + .chunk() + .get(..SIZE) + .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) }); + + if let Some(ret) = ret { + // if the direct conversion was possible, advance and return + $this.advance(SIZE); + return Ok(ret); + } else { + // if not we copy the bytes in a temp buffer then convert + let mut buf = [0; SIZE]; + $this.copy_to_slice(&mut buf); // (do the advance) + return Ok($typ::$conv(buf)); + } + }}; +} + // https://en.wikipedia.org/wiki/Sign_extension fn sign_extend(val: u64, nbytes: usize) -> i64 { let shift = (8 - nbytes) * 8; @@ -737,7 +783,7 @@ pub trait Buf { buf_get_impl!(self, u128::from_be_bytes); } - /// Gets an unsigned 128 bit integer from `self` in little-endian byte order. + /// Gets a signed 128 bit integer from `self` in little-endian byte order. /// /// The current position is advanced by 16. /// @@ -1113,6 +1159,920 @@ pub trait Buf { f64::from_bits(self.get_u64_ne()) } + /// Copies bytes from `self` into `dst`. + /// + /// The cursor is advanced by the number of bytes copied. `self` must have + /// enough remaining bytes to fill `dst`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"hello world"[..]; + /// let mut dst = [0; 5]; + /// + /// assert_eq!(Ok(()), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(&b"hello"[..], &dst); + /// assert_eq!(6, buf.remaining()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"hello world"[..]; + /// let mut dst = [0; 12]; + /// + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_copy_to_slice(&mut dst)); + /// ``` + fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { + if self.remaining() < dst.len() { + return Err(TryGetError::NotEnoughBytes); + } + + while !dst.is_empty() { + let src = self.chunk(); + let cnt = usize::min(src.len(), dst.len()); + + dst[..cnt].copy_from_slice(&src[..cnt]); + dst = &mut dst[cnt..]; + + self.advance(cnt); + } + Ok(()) + } + + /// Gets an unsigned 8 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 1. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08 hello"[..]; + /// assert_eq!(Ok(0x08_u8), buf.try_get_u8()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b""[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u8()); + /// ``` + fn try_get_u8(&mut self) -> Result { + buf_try_get_impl!(self, u8::from_be_bytes) + } + + /// Gets a signed 8 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 1. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08 hello"[..]; + /// assert_eq!(Ok(0x08_i8), buf.try_get_i8()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b""[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i8()); + /// ``` + fn try_get_i8(&mut self) -> Result { + buf_try_get_impl!(self, i8::from_be_bytes) + } + + /// Gets an unsigned 16 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09 hello"[..]; + /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16()); + /// ``` + fn try_get_u16(&mut self) -> Result { + buf_try_get_impl!(self, u16::from_be_bytes) + } + + /// Gets an unsigned 16 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x09\x08 hello"[..]; + /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16_le()); + /// ``` + fn try_get_u16_le(&mut self) -> Result { + buf_try_get_impl!(self, u16::from_le_bytes) + } + + /// Gets an unsigned 16 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09 hello", + /// false => b"\x09\x08 hello", + /// }; + /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16_ne()); + /// ``` + fn try_get_u16_ne(&mut self) -> Result { + buf_try_get_impl!(self, u16::from_ne_bytes) + } + + /// Gets a signed 16 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09 hello"[..]; + /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16()); + /// ``` + fn try_get_i16(&mut self) -> Result { + buf_try_get_impl!(self, i16::from_be_bytes) + } + + /// Gets an signed 16 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x09\x08 hello"[..]; + /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16_le()); + /// ``` + fn try_get_i16_le(&mut self) -> Result { + buf_try_get_impl!(self, i16::from_le_bytes) + } + + /// Gets a signed 16 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09 hello", + /// false => b"\x09\x08 hello", + /// }; + /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16_ne()); + /// ``` + fn try_get_i16_ne(&mut self) -> Result { + buf_try_get_impl!(self, i16::from_ne_bytes) + } + + /// Gets an unsigned 32 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + /// assert_eq!(Ok(0x0809A0A1), buf.try_get_u32()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32()); + /// ``` + fn try_get_u32(&mut self) -> Result { + buf_try_get_impl!(self, u32::from_be_bytes) + } + + /// Gets an unsigned 32 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x09\xA0"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32_le()); + /// ``` + fn try_get_u32_le(&mut self) -> Result { + buf_try_get_impl!(self, u32::from_le_bytes) + } + + /// Gets an unsigned 32 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09\xA0\xA1 hello", + /// false => b"\xA1\xA0\x09\x08 hello", + /// }; + /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x09\xA0"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32_ne()); + /// ``` + fn try_get_u32_ne(&mut self) -> Result { + buf_try_get_impl!(self, u32::from_ne_bytes) + } + + /// Gets a signed 32 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32()); + /// ``` + fn try_get_i32(&mut self) -> Result { + buf_try_get_impl!(self, i32::from_be_bytes) + } + + /// Gets a signed 32 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x09\xA0"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32_le()); + /// ``` + fn try_get_i32_le(&mut self) -> Result { + buf_try_get_impl!(self, i32::from_le_bytes) + } + + /// Gets a signed 32 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09\xA0\xA1 hello", + /// false => b"\xA1\xA0\x09\x08 hello", + /// }; + /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x09\xA0"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32_ne()); + /// ``` + fn try_get_i32_ne(&mut self) -> Result { + buf_try_get_impl!(self, i32::from_ne_bytes) + } + + /// Gets an unsigned 64 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64()); + /// ``` + fn try_get_u64(&mut self) -> Result { + buf_try_get_impl!(self, u64::from_be_bytes) + } + + /// Gets an unsigned 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64_le()); + /// ``` + fn try_get_u64_le(&mut self) -> Result { + buf_try_get_impl!(self, u64::from_le_bytes) + } + + /// Gets an unsigned 64 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", + /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64_ne()); + /// ``` + fn try_get_u64_ne(&mut self) -> Result { + buf_try_get_impl!(self, u64::from_ne_bytes) + } + + /// Gets a signed 64 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64()); + /// ``` + fn try_get_i64(&mut self) -> Result { + buf_try_get_impl!(self, i64::from_be_bytes) + } + + /// Gets a signed 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64_le()); + /// ``` + fn try_get_i64_le(&mut self) -> Result { + buf_try_get_impl!(self, i64::from_le_bytes) + } + + /// Gets a signed 64 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", + /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64_ne()); + /// ``` + fn try_get_i64_ne(&mut self) -> Result { + buf_try_get_impl!(self, i64::from_ne_bytes) + } + + /// Gets an unsigned 128 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128()); + /// ``` + fn try_get_u128(&mut self) -> Result { + buf_try_get_impl!(self, u128::from_be_bytes) + } + + /// Gets an unsigned 128 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128_le()); + /// ``` + fn try_get_u128_le(&mut self) -> Result { + buf_try_get_impl!(self, u128::from_le_bytes) + } + + /// Gets an unsigned 128 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", + /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128_ne()); + /// ``` + fn try_get_u128_ne(&mut self) -> Result { + buf_try_get_impl!(self, u128::from_ne_bytes) + } + + /// Gets a signed 128 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128()); + /// ``` + fn try_get_i128(&mut self) -> Result { + buf_try_get_impl!(self, i128::from_be_bytes) + } + + /// Gets a signed 128 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128_le()); + /// ``` + fn try_get_i128_le(&mut self) -> Result { + buf_try_get_impl!(self, i128::from_le_bytes) + } + + /// Gets a signed 128 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", + /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128_ne()); + /// ``` + fn try_get_i128_ne(&mut self) -> Result { + buf_try_get_impl!(self, i128::from_ne_bytes) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; + /// assert_eq!(1.2f32, buf.get_f32()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\x99\x99"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32()); + /// ``` + fn try_get_f32(&mut self) -> Result { + Ok(f32::from_bits(self.try_get_u32()?)) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; + /// assert_eq!(1.2f32, buf.get_f32_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\x99\x99"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32_le()); + /// ``` + fn try_get_f32_le(&mut self) -> Result { + Ok(f32::from_bits(self.try_get_u32_le()?)) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x3F\x99\x99\x9A hello", + /// false => b"\x9A\x99\x99\x3F hello", + /// }; + /// assert_eq!(1.2f32, buf.get_f32_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\x99\x99"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32_ne()); + /// ``` + fn try_get_f32_ne(&mut self) -> Result { + Ok(f32::from_bits(self.try_get_u32_ne()?)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; + /// assert_eq!(1.2f64, buf.get_f64()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64()); + /// ``` + fn try_get_f64(&mut self) -> Result { + Ok(f64::from_bits(self.try_get_u64()?)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; + /// assert_eq!(1.2f64, buf.get_f64_le()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64_le()); + /// ``` + fn try_get_f64_le(&mut self) -> Result { + Ok(f64::from_bits(self.try_get_u64_le()?)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello", + /// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello", + /// }; + /// assert_eq!(1.2f64, buf.get_f64_ne()); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64_ne()); + /// ``` + fn try_get_f64_ne(&mut self) -> Result { + Ok(f64::from_bits(self.try_get_u64_ne()?)) + } + /// Consumes `len` bytes inside self and returns new instance of `Bytes` /// with this data. /// diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 1bf0a47e8..9f6f8d8f9 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -27,7 +27,7 @@ mod vec_deque; #[cfg(feature = "std")] mod writer; -pub use self::buf_impl::Buf; +pub use self::buf_impl::{Buf, TryGetError}; pub use self::buf_mut::BufMut; pub use self::chain::Chain; pub use self::iter::IntoIter; diff --git a/src/lib.rs b/src/lib.rs index 7ddd2205b..dd34091c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ extern crate alloc; extern crate std; pub mod buf; -pub use crate::buf::{Buf, BufMut}; +pub use crate::buf::{Buf, BufMut, TryGetError}; mod bytes; mod bytes_mut; From 9f145799a310f376c104341e6b7759c2cb3d1342 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Mon, 6 Jan 2025 20:48:52 +0100 Subject: [PATCH 02/15] Added missing fallible try_get methods to Buf --- src/buf/buf_impl.rs | 203 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 0c0467932..70153eca9 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -110,6 +110,33 @@ macro_rules! buf_try_get_impl { return Ok($typ::$conv(buf)); } }}; + (le => $this:ident, $typ:tt, $len_to_read:expr) => {{ + const SIZE: usize = core::mem::size_of::<$typ>(); + + // The same trick as above does not improve the best case speed. + // It seems to be linked to the way the method is optimised by the compiler + let mut buf = [0; SIZE]; + + let subslice = match buf.get_mut(..$len_to_read) { + Some(subslice) => subslice, + None => panic_does_not_fit(SIZE, $len_to_read), + }; + + $this.try_copy_to_slice(subslice)?; + return Ok($typ::from_le_bytes(buf)); + }}; + (be => $this:ident, $typ:tt, $len_to_read:expr) => {{ + const SIZE: usize = core::mem::size_of::<$typ>(); + + let slice_at = match SIZE.checked_sub($len_to_read) { + Some(slice_at) => slice_at, + None => panic_does_not_fit(SIZE, $len_to_read), + }; + + let mut buf = [0; SIZE]; + $this.try_copy_to_slice(&mut buf[slice_at..])?; + return Ok($typ::from_be_bytes(buf)); + }}; } // https://en.wikipedia.org/wiki/Sign_extension @@ -1905,6 +1932,182 @@ pub trait Buf { buf_try_get_impl!(self, i128::from_ne_bytes) } + /// Gets an unsigned n-byte integer from `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03 hello"[..]; + /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint(4)); + /// ``` + fn try_get_uint(&mut self, nbytes: usize) -> Result { + buf_try_get_impl!(be => self, u64, nbytes); + } + + /// Gets an unsigned n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_le(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint_le(4)); + /// ``` + fn try_get_uint_le(&mut self, nbytes: usize) -> Result { + buf_try_get_impl!(le => self, u64, nbytes); + } + + /// Gets an unsigned n-byte integer from `self` in native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03 hello", + /// false => b"\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_ne(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03", + /// false => b"\x03\x02\x01", + /// }; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint_ne(4)); + /// ``` + fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { + if cfg!(target_endian = "big") { + self.try_get_uint(nbytes) + } else { + self.try_get_uint_le(nbytes) + } + } + + /// Gets a signed n-byte integer from `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03 hello"[..]; + /// assert_eq!(Ok(0x010203_i64), buf.try_get_int(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int(4)); + /// ``` + fn try_get_int(&mut self, nbytes: usize) -> Result { + buf_try_get_impl!(be => self, i64, nbytes); + } + + /// Gets a signed n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x03\x02\x01 hello"[..]; + /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_le(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf = &b"\x01\x02\x03"[..]; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int_le(4)); + /// ``` + fn try_get_int_le(&mut self, nbytes: usize) -> Result { + buf_try_get_impl!(le => self, i64, nbytes); + } + + /// Gets a signed n-byte integer from `self` in native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// remaining bytes to read the value. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03 hello", + /// false => b"\x03\x02\x01 hello", + /// }; + /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_ne(3)); + /// ``` + /// + /// ``` + /// use bytes::{Buf, TryGetError}; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03", + /// false => b"\x03\x02\x01", + /// }; + /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int_ne(4)); + /// ``` + fn try_get_int_ne(&mut self, nbytes: usize) -> Result { + if cfg!(target_endian = "big") { + self.try_get_int(nbytes) + } else { + self.try_get_int_le(nbytes) + } + } + /// Gets an IEEE754 single-precision (4 bytes) floating point number from /// `self` in big-endian byte order. /// From 38296fab911ad54eeb3c6fdfd56a4d34d4fc47f6 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Tue, 7 Jan 2025 06:01:13 +0100 Subject: [PATCH 03/15] Moved the error type to the top of the crate and renamed it. --- src/buf/buf_impl.rs | 256 +++++++++++++++++++++----------------------- src/buf/mod.rs | 2 +- src/lib.rs | 24 ++++- 3 files changed, 144 insertions(+), 138 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 70153eca9..4e333e662 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -4,29 +4,13 @@ use crate::buf::{take, Chain, Take}; #[cfg(feature = "std")] use crate::{min_u64_usize, saturating_sub_usize_u64}; use crate::{panic_advance, panic_does_not_fit}; +use crate::Error; #[cfg(feature = "std")] use std::io::IoSlice; use alloc::boxed::Box; -/// An error which occurred while attempting -/// to get a value from a [`Buf`](trait.Buf.html). -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum TryGetError { - /// Indicates that there were not enough remaining - /// bytes in the buffer to read a value. - NotEnoughBytes, -} - -impl std::fmt::Display for TryGetError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - match self { - TryGetError::NotEnoughBytes => write!(f, "Not enough bytes in buffer to read value"), - } - } -} - macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); @@ -88,7 +72,7 @@ macro_rules! buf_try_get_impl { const SIZE: usize = core::mem::size_of::<$typ>(); if $this.remaining() < SIZE { - return Err(TryGetError::NotEnoughBytes); + return Err(Error::OutOfBytes); } // try to convert directly from the bytes @@ -1208,16 +1192,16 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"hello world"[..]; /// let mut dst = [0; 12]; /// - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_copy_to_slice(&mut dst)); /// ``` - fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { + fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), Error> { if self.remaining() < dst.len() { - return Err(TryGetError::NotEnoughBytes); + return Err(Error::OutOfBytes); } while !dst.is_empty() { @@ -1249,12 +1233,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u8()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u8()); /// ``` - fn try_get_u8(&mut self) -> Result { + fn try_get_u8(&mut self) -> Result { buf_try_get_impl!(self, u8::from_be_bytes) } @@ -1275,12 +1259,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i8()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i8()); /// ``` - fn try_get_i8(&mut self) -> Result { + fn try_get_i8(&mut self) -> Result { buf_try_get_impl!(self, i8::from_be_bytes) } @@ -1301,12 +1285,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16()); /// ``` - fn try_get_u16(&mut self) -> Result { + fn try_get_u16(&mut self) -> Result { buf_try_get_impl!(self, u16::from_be_bytes) } @@ -1327,12 +1311,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_le()); /// ``` - fn try_get_u16_le(&mut self) -> Result { + fn try_get_u16_le(&mut self) -> Result { buf_try_get_impl!(self, u16::from_le_bytes) } @@ -1356,12 +1340,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u16_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_ne()); /// ``` - fn try_get_u16_ne(&mut self) -> Result { + fn try_get_u16_ne(&mut self) -> Result { buf_try_get_impl!(self, u16::from_ne_bytes) } @@ -1382,12 +1366,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16()); /// ``` - fn try_get_i16(&mut self) -> Result { + fn try_get_i16(&mut self) -> Result { buf_try_get_impl!(self, i16::from_be_bytes) } @@ -1408,12 +1392,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_le()); /// ``` - fn try_get_i16_le(&mut self) -> Result { + fn try_get_i16_le(&mut self) -> Result { buf_try_get_impl!(self, i16::from_le_bytes) } @@ -1437,12 +1421,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i16_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_ne()); /// ``` - fn try_get_i16_ne(&mut self) -> Result { + fn try_get_i16_ne(&mut self) -> Result { buf_try_get_impl!(self, i16::from_ne_bytes) } @@ -1463,12 +1447,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32()); /// ``` - fn try_get_u32(&mut self) -> Result { + fn try_get_u32(&mut self) -> Result { buf_try_get_impl!(self, u32::from_be_bytes) } @@ -1489,12 +1473,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_le()); /// ``` - fn try_get_u32_le(&mut self) -> Result { + fn try_get_u32_le(&mut self) -> Result { buf_try_get_impl!(self, u32::from_le_bytes) } @@ -1518,12 +1502,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u32_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_ne()); /// ``` - fn try_get_u32_ne(&mut self) -> Result { + fn try_get_u32_ne(&mut self) -> Result { buf_try_get_impl!(self, u32::from_ne_bytes) } @@ -1544,12 +1528,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32()); /// ``` - fn try_get_i32(&mut self) -> Result { + fn try_get_i32(&mut self) -> Result { buf_try_get_impl!(self, i32::from_be_bytes) } @@ -1570,12 +1554,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_le()); /// ``` - fn try_get_i32_le(&mut self) -> Result { + fn try_get_i32_le(&mut self) -> Result { buf_try_get_impl!(self, i32::from_le_bytes) } @@ -1599,12 +1583,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i32_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_ne()); /// ``` - fn try_get_i32_ne(&mut self) -> Result { + fn try_get_i32_ne(&mut self) -> Result { buf_try_get_impl!(self, i32::from_ne_bytes) } @@ -1625,12 +1609,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64()); /// ``` - fn try_get_u64(&mut self) -> Result { + fn try_get_u64(&mut self) -> Result { buf_try_get_impl!(self, u64::from_be_bytes) } @@ -1651,12 +1635,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_le()); /// ``` - fn try_get_u64_le(&mut self) -> Result { + fn try_get_u64_le(&mut self) -> Result { buf_try_get_impl!(self, u64::from_le_bytes) } @@ -1680,12 +1664,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u64_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_ne()); /// ``` - fn try_get_u64_ne(&mut self) -> Result { + fn try_get_u64_ne(&mut self) -> Result { buf_try_get_impl!(self, u64::from_ne_bytes) } @@ -1706,12 +1690,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64()); /// ``` - fn try_get_i64(&mut self) -> Result { + fn try_get_i64(&mut self) -> Result { buf_try_get_impl!(self, i64::from_be_bytes) } @@ -1732,12 +1716,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_le()); /// ``` - fn try_get_i64_le(&mut self) -> Result { + fn try_get_i64_le(&mut self) -> Result { buf_try_get_impl!(self, i64::from_le_bytes) } @@ -1761,12 +1745,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i64_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_ne()); /// ``` - fn try_get_i64_ne(&mut self) -> Result { + fn try_get_i64_ne(&mut self) -> Result { buf_try_get_impl!(self, i64::from_ne_bytes) } @@ -1787,12 +1771,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128()); /// ``` - fn try_get_u128(&mut self) -> Result { + fn try_get_u128(&mut self) -> Result { buf_try_get_impl!(self, u128::from_be_bytes) } @@ -1813,12 +1797,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_le()); /// ``` - fn try_get_u128_le(&mut self) -> Result { + fn try_get_u128_le(&mut self) -> Result { buf_try_get_impl!(self, u128::from_le_bytes) } @@ -1842,12 +1826,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_u128_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_ne()); /// ``` - fn try_get_u128_ne(&mut self) -> Result { + fn try_get_u128_ne(&mut self) -> Result { buf_try_get_impl!(self, u128::from_ne_bytes) } @@ -1868,12 +1852,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128()); /// ``` - fn try_get_i128(&mut self) -> Result { + fn try_get_i128(&mut self) -> Result { buf_try_get_impl!(self, i128::from_be_bytes) } @@ -1894,12 +1878,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_le()); /// ``` - fn try_get_i128_le(&mut self) -> Result { + fn try_get_i128_le(&mut self) -> Result { buf_try_get_impl!(self, i128::from_le_bytes) } @@ -1923,12 +1907,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_i128_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_ne()); /// ``` - fn try_get_i128_ne(&mut self) -> Result { + fn try_get_i128_ne(&mut self) -> Result { buf_try_get_impl!(self, i128::from_ne_bytes) } @@ -1949,12 +1933,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint(4)); /// ``` - fn try_get_uint(&mut self, nbytes: usize) -> Result { + fn try_get_uint(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, u64, nbytes); } @@ -1975,12 +1959,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint_le(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_le(4)); /// ``` - fn try_get_uint_le(&mut self, nbytes: usize) -> Result { + fn try_get_uint_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, u64, nbytes); } @@ -2004,15 +1988,15 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf: &[u8] = match cfg!(target_endian = "big") { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_uint_ne(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_ne(4)); /// ``` - fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { + fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_uint(nbytes) } else { @@ -2037,12 +2021,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int(4)); /// ``` - fn try_get_int(&mut self, nbytes: usize) -> Result { + fn try_get_int(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, i64, nbytes); } @@ -2063,12 +2047,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int_le(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_le(4)); /// ``` - fn try_get_int_le(&mut self, nbytes: usize) -> Result { + fn try_get_int_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, i64, nbytes); } @@ -2092,15 +2076,15 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf: &[u8] = match cfg!(target_endian = "big") { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_int_ne(4)); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_ne(4)); /// ``` - fn try_get_int_ne(&mut self, nbytes: usize) -> Result { + fn try_get_int_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_int(nbytes) } else { @@ -2126,12 +2110,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32()); /// ``` - fn try_get_f32(&mut self) -> Result { + fn try_get_f32(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32()?)) } @@ -2153,12 +2137,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_le()); /// ``` - fn try_get_f32_le(&mut self) -> Result { + fn try_get_f32_le(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_le()?)) } @@ -2183,12 +2167,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f32_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_ne()); /// ``` - fn try_get_f32_ne(&mut self) -> Result { + fn try_get_f32_ne(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_ne()?)) } @@ -2210,12 +2194,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64()); /// ``` - fn try_get_f64(&mut self) -> Result { + fn try_get_f64(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64()?)) } @@ -2237,12 +2221,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64_le()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_le()); /// ``` - fn try_get_f64_le(&mut self) -> Result { + fn try_get_f64_le(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_le()?)) } @@ -2267,12 +2251,12 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, TryGetError}; + /// use bytes::{Buf, Error}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::NotEnoughBytes), buf.try_get_f64_ne()); + /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_ne()); /// ``` - fn try_get_f64_ne(&mut self) -> Result { + fn try_get_f64_ne(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_ne()?)) } diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 9f6f8d8f9..1bf0a47e8 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -27,7 +27,7 @@ mod vec_deque; #[cfg(feature = "std")] mod writer; -pub use self::buf_impl::{Buf, TryGetError}; +pub use self::buf_impl::Buf; pub use self::buf_mut::BufMut; pub use self::chain::Chain; pub use self::iter::IntoIter; diff --git a/src/lib.rs b/src/lib.rs index dd34091c7..605567285 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ extern crate alloc; extern crate std; pub mod buf; -pub use crate::buf::{Buf, BufMut, TryGetError}; +pub use crate::buf::{Buf, BufMut}; mod bytes; mod bytes_mut; @@ -163,3 +163,25 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! { fn offset_from(dst: *const u8, original: *const u8) -> usize { dst as usize - original as usize } + +/// Error type for the Bytes crate. +#[derive(Debug, PartialEq, Eq)] +pub enum Error { + /// Indicates that there were not enough remaining + /// bytes in the buffer while attempting + /// to get a value from a [`Buf`] with one + /// of the `try_get_` methods. + OutOfBytes, +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + Error::OutOfBytes => write!(f, "Not enough bytes remaining in buffer to read value"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for Error { +} From 79a11c3c602f6cac7ff5562d78af1e3793d3a680 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Tue, 7 Jan 2025 06:06:22 +0100 Subject: [PATCH 04/15] Fixed build error when std is not enabled. --- src/buf/buf_impl.rs | 3 +-- src/lib.rs | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 4e333e662..5577f5f91 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -3,8 +3,7 @@ use crate::buf::{reader, Reader}; use crate::buf::{take, Chain, Take}; #[cfg(feature = "std")] use crate::{min_u64_usize, saturating_sub_usize_u64}; -use crate::{panic_advance, panic_does_not_fit}; -use crate::Error; +use crate::{panic_advance, panic_does_not_fit, Error}; #[cfg(feature = "std")] use std::io::IoSlice; diff --git a/src/lib.rs b/src/lib.rs index 605567285..9183776d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,6 +174,7 @@ pub enum Error { OutOfBytes, } +#[cfg(feature = "std")] impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { From b288e8cb3b9856ac515418716e0487da7d3329fd Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Tue, 7 Jan 2025 06:14:57 +0100 Subject: [PATCH 05/15] Added doc for panics when nbytes if too big in the (try_)get_int/uint methods --- src/buf/buf_impl.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 5577f5f91..ba0c0df7b 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -914,7 +914,8 @@ pub trait Buf { /// /// # Panics /// - /// This function panics if there is not enough remaining data in `self`. + /// This function panics if there is not enough remaining data in `self`, or + /// if `nbytes` is greater than 8. fn get_uint(&mut self, nbytes: usize) -> u64 { buf_get_impl!(be => self, u64, nbytes); } @@ -934,7 +935,8 @@ pub trait Buf { /// /// # Panics /// - /// This function panics if there is not enough remaining data in `self`. + /// This function panics if there is not enough remaining data in `self`, or + /// if `nbytes` is greater than 8. fn get_uint_le(&mut self, nbytes: usize) -> u64 { buf_get_impl!(le => self, u64, nbytes); } @@ -1937,6 +1939,10 @@ pub trait Buf { /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` > 8. fn try_get_uint(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, u64, nbytes); } @@ -1963,6 +1969,10 @@ pub trait Buf { /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_le(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` > 8. fn try_get_uint_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, u64, nbytes); } @@ -1995,6 +2005,10 @@ pub trait Buf { /// }; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_ne(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` is greater than 8. fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_uint(nbytes) @@ -2025,6 +2039,10 @@ pub trait Buf { /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` is greater than 8. fn try_get_int(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, i64, nbytes); } @@ -2051,6 +2069,10 @@ pub trait Buf { /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_le(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` is greater than 8. fn try_get_int_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, i64, nbytes); } @@ -2083,6 +2105,10 @@ pub trait Buf { /// }; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_ne(4)); /// ``` + /// + /// # Panics + /// + /// This function panics if `nbytes` is greater than 8. fn try_get_int_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_int(nbytes) From 2bf1110113150870d560da11ebe5bb540e103e28 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Wed, 8 Jan 2025 19:12:42 +0100 Subject: [PATCH 06/15] Added more assertions to the doc tests --- src/buf/buf_impl.rs | 173 +++++++++++++++++++++++++++++++++----------- 1 file changed, 129 insertions(+), 44 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index ba0c0df7b..8b6009b02 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -793,7 +793,7 @@ pub trait Buf { buf_get_impl!(self, u128::from_be_bytes); } - /// Gets a signed 128 bit integer from `self` in little-endian byte order. + /// Gets an unsigned 128 bit integer from `self` in little-endian byte order. /// /// The current position is advanced by 16. /// @@ -1176,7 +1176,7 @@ pub trait Buf { /// The cursor is advanced by the number of bytes copied. `self` must have /// enough remaining bytes to fill `dst`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1199,6 +1199,7 @@ pub trait Buf { /// let mut dst = [0; 12]; /// /// assert_eq!(Err(Error::OutOfBytes), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(11, buf.remaining()); /// ``` fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), Error> { if self.remaining() < dst.len() { @@ -1217,11 +1218,11 @@ pub trait Buf { Ok(()) } - /// Gets an unsigned 8 bit integer from `self` in big-endian byte order. + /// Gets an unsigned 8 bit integer from `self`. /// /// The current position is advanced by 1. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1231,6 +1232,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08 hello"[..]; /// assert_eq!(Ok(0x08_u8), buf.try_get_u8()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1240,14 +1242,19 @@ pub trait Buf { /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u8()); /// ``` fn try_get_u8(&mut self) -> Result { - buf_try_get_impl!(self, u8::from_be_bytes) + if self.remaining() < 1 { + return Err(Error::OutOfBytes); + } + let ret = self.chunk()[0]; + self.advance(1); + Ok(ret) } - /// Gets a signed 8 bit integer from `self` in big-endian byte order. + /// Gets a signed 8 bit integer from `self`. /// /// The current position is advanced by 1. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1257,6 +1264,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08 hello"[..]; /// assert_eq!(Ok(0x08_i8), buf.try_get_i8()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1266,14 +1274,19 @@ pub trait Buf { /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i8()); /// ``` fn try_get_i8(&mut self) -> Result { - buf_try_get_impl!(self, i8::from_be_bytes) + if self.remaining() < 1 { + return Err(Error::OutOfBytes); + } + let ret = self.chunk()[0] as i8; + self.advance(1); + Ok(ret) } /// Gets an unsigned 16 bit integer from `self` in big-endian byte order. /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1283,6 +1296,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09 hello"[..]; /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1290,6 +1304,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16(&mut self) -> Result { buf_try_get_impl!(self, u16::from_be_bytes) @@ -1299,7 +1314,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1309,6 +1324,7 @@ pub trait Buf { /// /// let mut buf = &b"\x09\x08 hello"[..]; /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1316,6 +1332,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_le()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_le(&mut self) -> Result { buf_try_get_impl!(self, u16::from_le_bytes) @@ -1325,7 +1342,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1338,6 +1355,7 @@ pub trait Buf { /// false => b"\x09\x08 hello", /// }; /// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1345,6 +1363,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_ne()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_ne(&mut self) -> Result { buf_try_get_impl!(self, u16::from_ne_bytes) @@ -1354,7 +1373,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1364,6 +1383,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09 hello"[..]; /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1371,6 +1391,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16(&mut self) -> Result { buf_try_get_impl!(self, i16::from_be_bytes) @@ -1380,7 +1401,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1390,6 +1411,7 @@ pub trait Buf { /// /// let mut buf = &b"\x09\x08 hello"[..]; /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1397,6 +1419,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_le()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_le(&mut self) -> Result { buf_try_get_impl!(self, i16::from_le_bytes) @@ -1406,7 +1429,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1419,6 +1442,7 @@ pub trait Buf { /// false => b"\x09\x08 hello", /// }; /// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1426,6 +1450,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_ne()); + /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_ne(&mut self) -> Result { buf_try_get_impl!(self, i16::from_ne_bytes) @@ -1435,7 +1460,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1445,6 +1470,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; /// assert_eq!(Ok(0x0809A0A1), buf.try_get_u32()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1452,6 +1478,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32(&mut self) -> Result { buf_try_get_impl!(self, u32::from_be_bytes) @@ -1461,7 +1488,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1471,6 +1498,7 @@ pub trait Buf { /// /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1478,6 +1506,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_le()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_le(&mut self) -> Result { buf_try_get_impl!(self, u32::from_le_bytes) @@ -1487,7 +1516,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1500,6 +1529,7 @@ pub trait Buf { /// false => b"\xA1\xA0\x09\x08 hello", /// }; /// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1507,6 +1537,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_ne()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_ne(&mut self) -> Result { buf_try_get_impl!(self, u32::from_ne_bytes) @@ -1516,7 +1547,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1526,6 +1557,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1533,6 +1565,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32(&mut self) -> Result { buf_try_get_impl!(self, i32::from_be_bytes) @@ -1542,7 +1575,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1552,6 +1585,7 @@ pub trait Buf { /// /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1559,6 +1593,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_le()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_le(&mut self) -> Result { buf_try_get_impl!(self, i32::from_le_bytes) @@ -1568,7 +1603,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1581,6 +1616,7 @@ pub trait Buf { /// false => b"\xA1\xA0\x09\x08 hello", /// }; /// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1588,6 +1624,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x09\xA0"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_ne()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_ne(&mut self) -> Result { buf_try_get_impl!(self, i32::from_ne_bytes) @@ -1597,7 +1634,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1607,6 +1644,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1614,6 +1652,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64(&mut self) -> Result { buf_try_get_impl!(self, u64::from_be_bytes) @@ -1623,7 +1662,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1633,6 +1672,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1640,6 +1680,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_le()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_le(&mut self) -> Result { buf_try_get_impl!(self, u64::from_le_bytes) @@ -1649,7 +1690,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1662,6 +1703,7 @@ pub trait Buf { /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1669,6 +1711,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_ne()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_ne(&mut self) -> Result { buf_try_get_impl!(self, u64::from_ne_bytes) @@ -1678,7 +1721,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1688,6 +1731,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1695,6 +1739,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64(&mut self) -> Result { buf_try_get_impl!(self, i64::from_be_bytes) @@ -1704,7 +1749,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1714,6 +1759,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1721,6 +1767,7 @@ pub trait Buf { /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_le()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_le(&mut self) -> Result { buf_try_get_impl!(self, i64::from_le_bytes) @@ -1730,7 +1777,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1743,6 +1790,7 @@ pub trait Buf { /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1750,6 +1798,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_ne()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_ne(&mut self) -> Result { buf_try_get_impl!(self, i64::from_ne_bytes) @@ -1759,7 +1808,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1769,6 +1818,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1776,6 +1826,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128(&mut self) -> Result { buf_try_get_impl!(self, u128::from_be_bytes) @@ -1785,7 +1836,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1795,6 +1846,7 @@ pub trait Buf { /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1802,6 +1854,7 @@ pub trait Buf { /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_le()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_le(&mut self) -> Result { buf_try_get_impl!(self, u128::from_le_bytes) @@ -1811,7 +1864,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1824,6 +1877,7 @@ pub trait Buf { /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1831,6 +1885,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_ne()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_ne(&mut self) -> Result { buf_try_get_impl!(self, u128::from_ne_bytes) @@ -1840,7 +1895,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1850,6 +1905,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1857,6 +1913,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128(&mut self) -> Result { buf_try_get_impl!(self, i128::from_be_bytes) @@ -1866,7 +1923,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1876,6 +1933,7 @@ pub trait Buf { /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1883,6 +1941,7 @@ pub trait Buf { /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_le()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_le(&mut self) -> Result { buf_try_get_impl!(self, i128::from_le_bytes) @@ -1892,7 +1951,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1905,6 +1964,7 @@ pub trait Buf { /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1912,6 +1972,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_ne()); + /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_ne(&mut self) -> Result { buf_try_get_impl!(self, i128::from_ne_bytes) @@ -1921,7 +1982,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1931,6 +1992,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03 hello"[..]; /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1938,6 +2000,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -1951,7 +2014,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1961,6 +2024,7 @@ pub trait Buf { /// /// let mut buf = &b"\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_le(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -1968,6 +2032,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_le(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -1981,7 +2046,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1994,6 +2059,7 @@ pub trait Buf { /// false => b"\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_ne(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2004,6 +2070,7 @@ pub trait Buf { /// false => b"\x03\x02\x01", /// }; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_ne(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -2021,7 +2088,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2031,6 +2098,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03 hello"[..]; /// assert_eq!(Ok(0x010203_i64), buf.try_get_int(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2038,6 +2106,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -2051,7 +2120,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2061,6 +2130,7 @@ pub trait Buf { /// /// let mut buf = &b"\x03\x02\x01 hello"[..]; /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_le(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2068,6 +2138,7 @@ pub trait Buf { /// /// let mut buf = &b"\x01\x02\x03"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_le(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -2081,7 +2152,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2094,6 +2165,7 @@ pub trait Buf { /// false => b"\x03\x02\x01 hello", /// }; /// assert_eq!(Ok(0x010203_i64), buf.try_get_int_ne(3)); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2104,6 +2176,7 @@ pub trait Buf { /// false => b"\x03\x02\x01", /// }; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_ne(4)); + /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics @@ -2122,7 +2195,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2132,6 +2205,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; /// assert_eq!(1.2f32, buf.get_f32()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2139,6 +2213,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\x99\x99"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32()?)) @@ -2149,7 +2224,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2159,6 +2234,7 @@ pub trait Buf { /// /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; /// assert_eq!(1.2f32, buf.get_f32_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2166,6 +2242,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\x99\x99"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_le()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_le(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_le()?)) @@ -2176,7 +2253,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2189,6 +2266,7 @@ pub trait Buf { /// false => b"\x9A\x99\x99\x3F hello", /// }; /// assert_eq!(1.2f32, buf.get_f32_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2196,6 +2274,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\x99\x99"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_ne()); + /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_ne(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_ne()?)) @@ -2206,7 +2285,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2216,6 +2295,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; /// assert_eq!(1.2f64, buf.get_f64()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2223,6 +2303,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64()?)) @@ -2233,7 +2314,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2243,6 +2324,7 @@ pub trait Buf { /// /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; /// assert_eq!(1.2f64, buf.get_f64_le()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2250,6 +2332,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_le()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_le(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_le()?)) @@ -2260,7 +2343,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::NotEnoughBytes)` when there are not enough + /// Returns `Err(Error::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2273,6 +2356,7 @@ pub trait Buf { /// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello", /// }; /// assert_eq!(1.2f64, buf.get_f64_ne()); + /// assert_eq!(6, buf.remaining()); /// ``` /// /// ``` @@ -2280,6 +2364,7 @@ pub trait Buf { /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_ne()); + /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_ne(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_ne()?)) From 7059258b9af711f30589dcf4e7ead185bc9b1eba Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Wed, 8 Jan 2025 19:21:36 +0100 Subject: [PATCH 07/15] Rolled back to using TryGetError instead of Error on the crate level. --- src/buf/buf_impl.rs | 346 +++++++++++++++++++++++--------------------- src/buf/mod.rs | 2 +- src/lib.rs | 25 +--- 3 files changed, 187 insertions(+), 186 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 8b6009b02..dfbcad3ff 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -3,13 +3,37 @@ use crate::buf::{reader, Reader}; use crate::buf::{take, Chain, Take}; #[cfg(feature = "std")] use crate::{min_u64_usize, saturating_sub_usize_u64}; -use crate::{panic_advance, panic_does_not_fit, Error}; +use crate::{panic_advance, panic_does_not_fit}; #[cfg(feature = "std")] use std::io::IoSlice; use alloc::boxed::Box; + +/// Error type for the `try_get_` methods in [`Buf`]. +#[derive(Debug, PartialEq, Eq)] +pub enum TryGetError { + /// Indicates that there were not enough remaining + /// bytes in the buffer while attempting + /// to get a value from a [`Buf`] with one + /// of the `try_get_` methods. + OutOfBytes, +} + +#[cfg(feature = "std")] +impl std::fmt::Display for TryGetError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + TryGetError::OutOfBytes => write!(f, "Not enough bytes remaining in buffer to read value"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for TryGetError { +} + macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); @@ -71,7 +95,7 @@ macro_rules! buf_try_get_impl { const SIZE: usize = core::mem::size_of::<$typ>(); if $this.remaining() < SIZE { - return Err(Error::OutOfBytes); + return Err(TryGetError::OutOfBytes); } // try to convert directly from the bytes @@ -1176,7 +1200,7 @@ pub trait Buf { /// The cursor is advanced by the number of bytes copied. `self` must have /// enough remaining bytes to fill `dst`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1193,17 +1217,17 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"hello world"[..]; /// let mut dst = [0; 12]; /// - /// assert_eq!(Err(Error::OutOfBytes), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_copy_to_slice(&mut dst)); /// assert_eq!(11, buf.remaining()); /// ``` - fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), Error> { + fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { if self.remaining() < dst.len() { - return Err(Error::OutOfBytes); + return Err(TryGetError::OutOfBytes); } while !dst.is_empty() { @@ -1222,7 +1246,7 @@ pub trait Buf { /// /// The current position is advanced by 1. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1236,14 +1260,14 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u8()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u8()); /// ``` - fn try_get_u8(&mut self) -> Result { + fn try_get_u8(&mut self) -> Result { if self.remaining() < 1 { - return Err(Error::OutOfBytes); + return Err(TryGetError::OutOfBytes); } let ret = self.chunk()[0]; self.advance(1); @@ -1254,7 +1278,7 @@ pub trait Buf { /// /// The current position is advanced by 1. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1268,14 +1292,14 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i8()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i8()); /// ``` - fn try_get_i8(&mut self) -> Result { + fn try_get_i8(&mut self) -> Result { if self.remaining() < 1 { - return Err(Error::OutOfBytes); + return Err(TryGetError::OutOfBytes); } let ret = self.chunk()[0] as i8; self.advance(1); @@ -1286,7 +1310,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1300,13 +1324,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_u16(&mut self) -> Result { + fn try_get_u16(&mut self) -> Result { buf_try_get_impl!(self, u16::from_be_bytes) } @@ -1314,7 +1338,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1328,13 +1352,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16_le()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_u16_le(&mut self) -> Result { + fn try_get_u16_le(&mut self) -> Result { buf_try_get_impl!(self, u16::from_le_bytes) } @@ -1342,7 +1366,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1359,13 +1383,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u16_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_u16_ne(&mut self) -> Result { + fn try_get_u16_ne(&mut self) -> Result { buf_try_get_impl!(self, u16::from_ne_bytes) } @@ -1373,7 +1397,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1387,13 +1411,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_i16(&mut self) -> Result { + fn try_get_i16(&mut self) -> Result { buf_try_get_impl!(self, i16::from_be_bytes) } @@ -1401,7 +1425,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1415,13 +1439,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16_le()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_i16_le(&mut self) -> Result { + fn try_get_i16_le(&mut self) -> Result { buf_try_get_impl!(self, i16::from_le_bytes) } @@ -1429,7 +1453,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1446,13 +1470,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i16_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` - fn try_get_i16_ne(&mut self) -> Result { + fn try_get_i16_ne(&mut self) -> Result { buf_try_get_impl!(self, i16::from_ne_bytes) } @@ -1460,7 +1484,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1474,13 +1498,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_u32(&mut self) -> Result { + fn try_get_u32(&mut self) -> Result { buf_try_get_impl!(self, u32::from_be_bytes) } @@ -1488,7 +1512,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1502,13 +1526,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32_le()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_u32_le(&mut self) -> Result { + fn try_get_u32_le(&mut self) -> Result { buf_try_get_impl!(self, u32::from_le_bytes) } @@ -1516,7 +1540,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1533,13 +1557,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_u32_ne(&mut self) -> Result { + fn try_get_u32_ne(&mut self) -> Result { buf_try_get_impl!(self, u32::from_ne_bytes) } @@ -1547,7 +1571,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1561,13 +1585,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_i32(&mut self) -> Result { + fn try_get_i32(&mut self) -> Result { buf_try_get_impl!(self, i32::from_be_bytes) } @@ -1575,7 +1599,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1589,13 +1613,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32_le()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_i32_le(&mut self) -> Result { + fn try_get_i32_le(&mut self) -> Result { buf_try_get_impl!(self, i32::from_le_bytes) } @@ -1603,7 +1627,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1620,13 +1644,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_i32_ne(&mut self) -> Result { + fn try_get_i32_ne(&mut self) -> Result { buf_try_get_impl!(self, i32::from_ne_bytes) } @@ -1634,7 +1658,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1648,13 +1672,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_u64(&mut self) -> Result { + fn try_get_u64(&mut self) -> Result { buf_try_get_impl!(self, u64::from_be_bytes) } @@ -1662,7 +1686,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1676,13 +1700,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64_le()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_u64_le(&mut self) -> Result { + fn try_get_u64_le(&mut self) -> Result { buf_try_get_impl!(self, u64::from_le_bytes) } @@ -1690,7 +1714,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1707,13 +1731,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_u64_ne(&mut self) -> Result { + fn try_get_u64_ne(&mut self) -> Result { buf_try_get_impl!(self, u64::from_ne_bytes) } @@ -1721,7 +1745,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1735,13 +1759,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_i64(&mut self) -> Result { + fn try_get_i64(&mut self) -> Result { buf_try_get_impl!(self, i64::from_be_bytes) } @@ -1749,7 +1773,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1763,13 +1787,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64_le()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_i64_le(&mut self) -> Result { + fn try_get_i64_le(&mut self) -> Result { buf_try_get_impl!(self, i64::from_le_bytes) } @@ -1777,7 +1801,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1794,13 +1818,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_i64_ne(&mut self) -> Result { + fn try_get_i64_ne(&mut self) -> Result { buf_try_get_impl!(self, i64::from_ne_bytes) } @@ -1808,7 +1832,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1822,13 +1846,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_u128(&mut self) -> Result { + fn try_get_u128(&mut self) -> Result { buf_try_get_impl!(self, u128::from_be_bytes) } @@ -1836,7 +1860,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1850,13 +1874,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128_le()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_u128_le(&mut self) -> Result { + fn try_get_u128_le(&mut self) -> Result { buf_try_get_impl!(self, u128::from_le_bytes) } @@ -1864,7 +1888,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1881,13 +1905,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_u128_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_u128_ne(&mut self) -> Result { + fn try_get_u128_ne(&mut self) -> Result { buf_try_get_impl!(self, u128::from_ne_bytes) } @@ -1895,7 +1919,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1909,13 +1933,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_i128(&mut self) -> Result { + fn try_get_i128(&mut self) -> Result { buf_try_get_impl!(self, i128::from_be_bytes) } @@ -1923,7 +1947,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1937,13 +1961,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128_le()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_i128_le(&mut self) -> Result { + fn try_get_i128_le(&mut self) -> Result { buf_try_get_impl!(self, i128::from_le_bytes) } @@ -1951,7 +1975,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1968,13 +1992,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_i128_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` - fn try_get_i128_ne(&mut self) -> Result { + fn try_get_i128_ne(&mut self) -> Result { buf_try_get_impl!(self, i128::from_ne_bytes) } @@ -1982,7 +2006,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1996,17 +2020,17 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` > 8. - fn try_get_uint(&mut self, nbytes: usize) -> Result { + fn try_get_uint(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, u64, nbytes); } @@ -2014,7 +2038,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2028,17 +2052,17 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_le(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` > 8. - fn try_get_uint_le(&mut self, nbytes: usize) -> Result { + fn try_get_uint_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, u64, nbytes); } @@ -2046,7 +2070,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2063,20 +2087,20 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf: &[u8] = match cfg!(target_endian = "big") { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_uint_ne(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` is greater than 8. - fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { + fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_uint(nbytes) } else { @@ -2088,7 +2112,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2102,17 +2126,17 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` is greater than 8. - fn try_get_int(&mut self, nbytes: usize) -> Result { + fn try_get_int(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(be => self, i64, nbytes); } @@ -2120,7 +2144,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2134,17 +2158,17 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_le(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` is greater than 8. - fn try_get_int_le(&mut self, nbytes: usize) -> Result { + fn try_get_int_le(&mut self, nbytes: usize) -> Result { buf_try_get_impl!(le => self, i64, nbytes); } @@ -2152,7 +2176,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2169,20 +2193,20 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf: &[u8] = match cfg!(target_endian = "big") { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_int_ne(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// /// # Panics /// /// This function panics if `nbytes` is greater than 8. - fn try_get_int_ne(&mut self, nbytes: usize) -> Result { + fn try_get_int_ne(&mut self, nbytes: usize) -> Result { if cfg!(target_endian = "big") { self.try_get_int(nbytes) } else { @@ -2195,7 +2219,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2209,13 +2233,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_f32(&mut self) -> Result { + fn try_get_f32(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32()?)) } @@ -2224,7 +2248,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2238,13 +2262,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32_le()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_f32_le(&mut self) -> Result { + fn try_get_f32_le(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_le()?)) } @@ -2253,7 +2277,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2270,13 +2294,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` - fn try_get_f32_ne(&mut self) -> Result { + fn try_get_f32_ne(&mut self) -> Result { Ok(f32::from_bits(self.try_get_u32_ne()?)) } @@ -2285,7 +2309,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2299,13 +2323,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_f64(&mut self) -> Result { + fn try_get_f64(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64()?)) } @@ -2314,7 +2338,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2328,13 +2352,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64_le()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_f64_le(&mut self) -> Result { + fn try_get_f64_le(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_le()?)) } @@ -2343,7 +2367,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(Error::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2360,13 +2384,13 @@ pub trait Buf { /// ``` /// /// ``` - /// use bytes::{Buf, Error}; + /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(Error::OutOfBytes), buf.try_get_f64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` - fn try_get_f64_ne(&mut self) -> Result { + fn try_get_f64_ne(&mut self) -> Result { Ok(f64::from_bits(self.try_get_u64_ne()?)) } diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 1bf0a47e8..9f6f8d8f9 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -27,7 +27,7 @@ mod vec_deque; #[cfg(feature = "std")] mod writer; -pub use self::buf_impl::Buf; +pub use self::buf_impl::{Buf, TryGetError}; pub use self::buf_mut::BufMut; pub use self::chain::Chain; pub use self::iter::IntoIter; diff --git a/src/lib.rs b/src/lib.rs index 9183776d5..dd34091c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ extern crate alloc; extern crate std; pub mod buf; -pub use crate::buf::{Buf, BufMut}; +pub use crate::buf::{Buf, BufMut, TryGetError}; mod bytes; mod bytes_mut; @@ -163,26 +163,3 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! { fn offset_from(dst: *const u8, original: *const u8) -> usize { dst as usize - original as usize } - -/// Error type for the Bytes crate. -#[derive(Debug, PartialEq, Eq)] -pub enum Error { - /// Indicates that there were not enough remaining - /// bytes in the buffer while attempting - /// to get a value from a [`Buf`] with one - /// of the `try_get_` methods. - OutOfBytes, -} - -#[cfg(feature = "std")] -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - match self { - Error::OutOfBytes => write!(f, "Not enough bytes remaining in buffer to read value"), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for Error { -} From 11e83c25433c61ed91b8f6322ef5ff5d76438813 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Wed, 8 Jan 2025 19:31:42 +0100 Subject: [PATCH 08/15] Doc tweak to see if workflows are triggered. --- src/buf/buf_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index dfbcad3ff..748571234 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -11,7 +11,7 @@ use std::io::IoSlice; use alloc::boxed::Box; -/// Error type for the `try_get_` methods in [`Buf`]. +/// Error type for the `try_get_` methods of [`Buf`]. #[derive(Debug, PartialEq, Eq)] pub enum TryGetError { /// Indicates that there were not enough remaining From 3dc9fc67dbc987a20a9ef7e45f042e1f779f4802 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Wed, 8 Jan 2025 19:38:06 +0100 Subject: [PATCH 09/15] Ran cargo fmt --- src/buf/buf_impl.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 748571234..9705cef00 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -10,7 +10,6 @@ use std::io::IoSlice; use alloc::boxed::Box; - /// Error type for the `try_get_` methods of [`Buf`]. #[derive(Debug, PartialEq, Eq)] pub enum TryGetError { @@ -25,14 +24,15 @@ pub enum TryGetError { impl std::fmt::Display for TryGetError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { - TryGetError::OutOfBytes => write!(f, "Not enough bytes remaining in buffer to read value"), + TryGetError::OutOfBytes => { + write!(f, "Not enough bytes remaining in buffer to read value") + } } } } #[cfg(feature = "std")] -impl std::error::Error for TryGetError { -} +impl std::error::Error for TryGetError {} macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ From 1adaec7c50c9821ce1762e7468fba7c4f005d9e8 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Thu, 9 Jan 2025 17:44:35 +0100 Subject: [PATCH 10/15] TryGetError now contains requested and available byte counts. The buf_get_impl macro is implemented through buf_try_get_impl to avoid code duplication, and copy_to_slice is implemented through try_copy_to_slice. --- src/buf/buf_impl.rs | 204 +++++++++++++++++++------------------------- 1 file changed, 89 insertions(+), 115 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 9705cef00..796358494 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -17,15 +17,24 @@ pub enum TryGetError { /// bytes in the buffer while attempting /// to get a value from a [`Buf`] with one /// of the `try_get_` methods. - OutOfBytes, + OutOfBytes { + /// The number of bytes necessary to get the value + requested: usize, + + /// The number of bytes available in the buffer + available: usize, + }, } #[cfg(feature = "std")] impl std::fmt::Display for TryGetError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { - TryGetError::OutOfBytes => { - write!(f, "Not enough bytes remaining in buffer to read value") + TryGetError::OutOfBytes { + requested, + available, + } => { + write!(f, "Not enough bytes remaining in buffer to read value (requested {requested} but only {available} available)") } } } @@ -34,12 +43,15 @@ impl std::fmt::Display for TryGetError { #[cfg(feature = "std")] impl std::error::Error for TryGetError {} -macro_rules! buf_get_impl { +macro_rules! buf_try_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); if $this.remaining() < SIZE { - panic_advance(SIZE, $this.remaining()); + return Err(TryGetError::OutOfBytes { + requested: SIZE, + available: $this.remaining(), + }); } // try to convert directly from the bytes @@ -53,12 +65,12 @@ macro_rules! buf_get_impl { if let Some(ret) = ret { // if the direct conversion was possible, advance and return $this.advance(SIZE); - return ret; + return Ok(ret); } else { // if not we copy the bytes in a temp buffer then convert let mut buf = [0; SIZE]; $this.copy_to_slice(&mut buf); // (do the advance) - return $typ::$conv(buf); + return Ok($typ::$conv(buf)); } }}; (le => $this:ident, $typ:tt, $len_to_read:expr) => {{ @@ -73,8 +85,8 @@ macro_rules! buf_get_impl { None => panic_does_not_fit(SIZE, $len_to_read), }; - $this.copy_to_slice(subslice); - return $typ::from_le_bytes(buf); + $this.try_copy_to_slice(subslice)?; + return Ok($typ::from_le_bytes(buf)); }}; (be => $this:ident, $typ:tt, $len_to_read:expr) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); @@ -85,67 +97,32 @@ macro_rules! buf_get_impl { }; let mut buf = [0; SIZE]; - $this.copy_to_slice(&mut buf[slice_at..]); - return $typ::from_be_bytes(buf); + $this.try_copy_to_slice(&mut buf[slice_at..])?; + return Ok($typ::from_be_bytes(buf)); }}; } -macro_rules! buf_try_get_impl { +macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ - const SIZE: usize = core::mem::size_of::<$typ>(); - - if $this.remaining() < SIZE { - return Err(TryGetError::OutOfBytes); - } - - // try to convert directly from the bytes - // this Option trick is to avoid keeping a borrow on self - // when advance() is called (mut borrow) and to call bytes() only once - let ret = $this - .chunk() - .get(..SIZE) - .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) }); - - if let Some(ret) = ret { - // if the direct conversion was possible, advance and return - $this.advance(SIZE); - return Ok(ret); - } else { - // if not we copy the bytes in a temp buffer then convert - let mut buf = [0; SIZE]; - $this.copy_to_slice(&mut buf); // (do the advance) - return Ok($typ::$conv(buf)); - } + return unwrap_advance((|| buf_try_get_impl!($this, $typ::$conv))()); }}; (le => $this:ident, $typ:tt, $len_to_read:expr) => {{ - const SIZE: usize = core::mem::size_of::<$typ>(); - - // The same trick as above does not improve the best case speed. - // It seems to be linked to the way the method is optimised by the compiler - let mut buf = [0; SIZE]; - - let subslice = match buf.get_mut(..$len_to_read) { - Some(subslice) => subslice, - None => panic_does_not_fit(SIZE, $len_to_read), - }; - - $this.try_copy_to_slice(subslice)?; - return Ok($typ::from_le_bytes(buf)); + return unwrap_advance((|| buf_try_get_impl!(le => $this, $typ, $len_to_read))()); }}; (be => $this:ident, $typ:tt, $len_to_read:expr) => {{ - const SIZE: usize = core::mem::size_of::<$typ>(); - - let slice_at = match SIZE.checked_sub($len_to_read) { - Some(slice_at) => slice_at, - None => panic_does_not_fit(SIZE, $len_to_read), - }; - - let mut buf = [0; SIZE]; - $this.try_copy_to_slice(&mut buf[slice_at..])?; - return Ok($typ::from_be_bytes(buf)); + return unwrap_advance((|| buf_try_get_impl!(be => $this, $typ, $len_to_read))()); }}; } +fn unwrap_advance(value: Result) -> T { + value.unwrap_or_else( + |TryGetError::OutOfBytes { + requested, + available, + }| panic_advance(requested, available), + ) +} + // https://en.wikipedia.org/wiki/Sign_extension fn sign_extend(val: u64, nbytes: usize) -> i64 { let shift = (8 - nbytes) * 8; @@ -353,20 +330,8 @@ pub trait Buf { /// # Panics /// /// This function panics if `self.remaining() < dst.len()`. - fn copy_to_slice(&mut self, mut dst: &mut [u8]) { - if self.remaining() < dst.len() { - panic_advance(dst.len(), self.remaining()); - } - - while !dst.is_empty() { - let src = self.chunk(); - let cnt = usize::min(src.len(), dst.len()); - - dst[..cnt].copy_from_slice(&src[..cnt]); - dst = &mut dst[cnt..]; - - self.advance(cnt); - } + fn copy_to_slice(&mut self, dst: &mut [u8]) { + unwrap_advance(self.try_copy_to_slice(dst)); } /// Gets an unsigned 8 bit integer from `self`. @@ -1222,12 +1187,15 @@ pub trait Buf { /// let mut buf = &b"hello world"[..]; /// let mut dst = [0; 12]; /// - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 12, available: 11}), buf.try_copy_to_slice(&mut dst)); /// assert_eq!(11, buf.remaining()); /// ``` fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { if self.remaining() < dst.len() { - return Err(TryGetError::OutOfBytes); + return Err(TryGetError::OutOfBytes { + requested: dst.len(), + available: self.remaining(), + }); } while !dst.is_empty() { @@ -1263,11 +1231,14 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u8()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 1, available: 0}), buf.try_get_u8()); /// ``` fn try_get_u8(&mut self) -> Result { if self.remaining() < 1 { - return Err(TryGetError::OutOfBytes); + return Err(TryGetError::OutOfBytes { + requested: 1, + available: self.remaining(), + }); } let ret = self.chunk()[0]; self.advance(1); @@ -1295,11 +1266,14 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i8()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 1, available: 0}), buf.try_get_i8()); /// ``` fn try_get_i8(&mut self) -> Result { if self.remaining() < 1 { - return Err(TryGetError::OutOfBytes); + return Err(TryGetError::OutOfBytes { + requested: 1, + available: self.remaining(), + }); } let ret = self.chunk()[0] as i8; self.advance(1); @@ -1327,7 +1301,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16(&mut self) -> Result { @@ -1355,7 +1329,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16_le()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_le(&mut self) -> Result { @@ -1386,7 +1360,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u16_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_ne(&mut self) -> Result { @@ -1414,7 +1388,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16(&mut self) -> Result { @@ -1442,7 +1416,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16_le()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_le(&mut self) -> Result { @@ -1473,7 +1447,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i16_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_ne(&mut self) -> Result { @@ -1501,7 +1475,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32(&mut self) -> Result { @@ -1529,7 +1503,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_le(&mut self) -> Result { @@ -1560,7 +1534,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_ne(&mut self) -> Result { @@ -1588,7 +1562,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32(&mut self) -> Result { @@ -1616,7 +1590,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_le(&mut self) -> Result { @@ -1647,7 +1621,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_ne(&mut self) -> Result { @@ -1675,7 +1649,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64(&mut self) -> Result { @@ -1703,7 +1677,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_le(&mut self) -> Result { @@ -1734,7 +1708,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_ne(&mut self) -> Result { @@ -1762,7 +1736,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64(&mut self) -> Result { @@ -1790,7 +1764,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_le(&mut self) -> Result { @@ -1821,7 +1795,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_ne(&mut self) -> Result { @@ -1849,7 +1823,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128(&mut self) -> Result { @@ -1877,7 +1851,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128_le()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_le(&mut self) -> Result { @@ -1908,7 +1882,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_u128_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_ne(&mut self) -> Result { @@ -1936,7 +1910,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128(&mut self) -> Result { @@ -1964,7 +1938,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128_le()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_le(&mut self) -> Result { @@ -1995,7 +1969,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_i128_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_ne(&mut self) -> Result { @@ -2023,7 +1997,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2055,7 +2029,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint_le(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2093,7 +2067,7 @@ pub trait Buf { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_uint_ne(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2129,7 +2103,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2161,7 +2135,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int_le(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2199,7 +2173,7 @@ pub trait Buf { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_int_ne(4)); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2236,7 +2210,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32(&mut self) -> Result { @@ -2265,7 +2239,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_le(&mut self) -> Result { @@ -2297,7 +2271,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f32_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_ne(&mut self) -> Result { @@ -2326,7 +2300,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64(&mut self) -> Result { @@ -2355,7 +2329,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64_le()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_le(&mut self) -> Result { @@ -2387,7 +2361,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes), buf.try_get_f64_ne()); + /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_ne(&mut self) -> Result { From 6fda922bdb11e2bf5ebb6e9663ff181078f040cb Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Fri, 10 Jan 2025 06:19:22 +0100 Subject: [PATCH 11/15] Attempt to fix compilation error for write! (may have used too new syntax) --- src/buf/buf_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 796358494..6eae36e71 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -34,7 +34,7 @@ impl std::fmt::Display for TryGetError { requested, available, } => { - write!(f, "Not enough bytes remaining in buffer to read value (requested {requested} but only {available} available)") + write!(f, "Not enough bytes remaining in buffer to read value (requested {} but only {} available)", requested, available) } } } From 084d70bab04c735f15612c6f6a231f986e20595d Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Fri, 10 Jan 2025 21:21:19 +0100 Subject: [PATCH 12/15] Added the new try_get methods to deref_forward_buf, plus a few old ones that were missing. --- src/buf/buf_impl.rs | 255 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 6eae36e71..f013d4797 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -2615,6 +2615,36 @@ macro_rules! deref_forward_buf { (**self).get_i64_ne() } + #[inline] + fn get_u128(&mut self) -> u128 { + (**self).get_u128() + } + + #[inline] + fn get_u128_le(&mut self) -> u128 { + (**self).get_u128_le() + } + + #[inline] + fn get_u128_ne(&mut self) -> u128 { + (**self).get_u128_ne() + } + + #[inline] + fn get_i128(&mut self) -> i128 { + (**self).get_i128() + } + + #[inline] + fn get_i128_le(&mut self) -> i128 { + (**self).get_i128_le() + } + + #[inline] + fn get_i128_ne(&mut self) -> i128 { + (**self).get_i128_ne() + } + #[inline] fn get_uint(&mut self, nbytes: usize) -> u64 { (**self).get_uint(nbytes) @@ -2645,6 +2675,231 @@ macro_rules! deref_forward_buf { (**self).get_int_ne(nbytes) } + #[inline] + fn get_f32(&mut self) -> f32 { + (**self).get_f32() + } + + #[inline] + fn get_f32_le(&mut self) -> f32 { + (**self).get_f32_le() + } + + #[inline] + fn get_f32_ne(&mut self) -> f32 { + (**self).get_f32_ne() + } + + #[inline] + fn get_f64(&mut self) -> f64 { + (**self).get_f64() + } + + #[inline] + fn get_f64_le(&mut self) -> f64 { + (**self).get_f64_le() + } + + #[inline] + fn get_f64_ne(&mut self) -> f64 { + (**self).get_f64_ne() + } + + #[inline] + fn try_copy_to_slice(&mut self, dst: &mut [u8]) -> Result<(), TryGetError> { + (**self).try_copy_to_slice(dst) + } + + #[inline] + fn try_get_u8(&mut self) -> Result { + (**self).try_get_u8() + } + + #[inline] + fn try_get_i8(&mut self) -> Result { + (**self).try_get_i8() + } + + #[inline] + fn try_get_u16(&mut self) -> Result { + (**self).try_get_u16() + } + + #[inline] + fn try_get_u16_le(&mut self) -> Result { + (**self).try_get_u16_le() + } + + #[inline] + fn try_get_u16_ne(&mut self) -> Result { + (**self).try_get_u16_ne() + } + + #[inline] + fn try_get_i16(&mut self) -> Result { + (**self).try_get_i16() + } + + #[inline] + fn try_get_i16_le(&mut self) -> Result { + (**self).try_get_i16_le() + } + + #[inline] + fn try_get_i16_ne(&mut self) -> Result { + (**self).try_get_i16_ne() + } + + #[inline] + fn try_get_u32(&mut self) -> Result { + (**self).try_get_u32() + } + + #[inline] + fn try_get_u32_le(&mut self) -> Result { + (**self).try_get_u32_le() + } + + #[inline] + fn try_get_u32_ne(&mut self) -> Result { + (**self).try_get_u32_ne() + } + + #[inline] + fn try_get_i32(&mut self) -> Result { + (**self).try_get_i32() + } + + #[inline] + fn try_get_i32_le(&mut self) -> Result { + (**self).try_get_i32_le() + } + + #[inline] + fn try_get_i32_ne(&mut self) -> Result { + (**self).try_get_i32_ne() + } + + #[inline] + fn try_get_u64(&mut self) -> Result { + (**self).try_get_u64() + } + + #[inline] + fn try_get_u64_le(&mut self) -> Result { + (**self).try_get_u64_le() + } + + #[inline] + fn try_get_u64_ne(&mut self) -> Result { + (**self).try_get_u64_ne() + } + + #[inline] + fn try_get_i64(&mut self) -> Result { + (**self).try_get_i64() + } + + #[inline] + fn try_get_i64_le(&mut self) -> Result { + (**self).try_get_i64_le() + } + + #[inline] + fn try_get_i64_ne(&mut self) -> Result { + (**self).try_get_i64_ne() + } + + #[inline] + fn try_get_u128(&mut self) -> Result { + (**self).try_get_u128() + } + + #[inline] + fn try_get_u128_le(&mut self) -> Result { + (**self).try_get_u128_le() + } + + #[inline] + fn try_get_u128_ne(&mut self) -> Result { + (**self).try_get_u128_ne() + } + + #[inline] + fn try_get_i128(&mut self) -> Result { + (**self).try_get_i128() + } + + #[inline] + fn try_get_i128_le(&mut self) -> Result { + (**self).try_get_i128_le() + } + + #[inline] + fn try_get_i128_ne(&mut self) -> Result { + (**self).try_get_i128_ne() + } + + #[inline] + fn try_get_uint(&mut self, nbytes: usize) -> Result { + (**self).try_get_uint(nbytes) + } + + #[inline] + fn try_get_uint_le(&mut self, nbytes: usize) -> Result { + (**self).try_get_uint_le(nbytes) + } + + #[inline] + fn try_get_uint_ne(&mut self, nbytes: usize) -> Result { + (**self).try_get_uint_ne(nbytes) + } + + #[inline] + fn try_get_int(&mut self, nbytes: usize) -> Result { + (**self).try_get_int(nbytes) + } + + #[inline] + fn try_get_int_le(&mut self, nbytes: usize) -> Result { + (**self).try_get_int_le(nbytes) + } + + #[inline] + fn try_get_int_ne(&mut self, nbytes: usize) -> Result { + (**self).try_get_int_ne(nbytes) + } + + #[inline] + fn try_get_f32(&mut self) -> Result { + (**self).try_get_f32() + } + + #[inline] + fn try_get_f32_le(&mut self) -> Result { + (**self).try_get_f32_le() + } + + #[inline] + fn try_get_f32_ne(&mut self) -> Result { + (**self).try_get_f32_ne() + } + + #[inline] + fn try_get_f64(&mut self) -> Result { + (**self).try_get_f64() + } + + #[inline] + fn try_get_f64_le(&mut self) -> Result { + (**self).try_get_f64_le() + } + + #[inline] + fn try_get_f64_ne(&mut self) -> Result { + (**self).try_get_f64_ne() + } + #[inline] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { (**self).copy_to_bytes(len) From fd05103f2a70ff8946dd19714ff6b97ea42af038 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Fri, 10 Jan 2025 21:30:13 +0100 Subject: [PATCH 13/15] Display for TryGetError also for no_std builds. --- src/buf/buf_impl.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index f013d4797..608b301f5 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -26,9 +26,8 @@ pub enum TryGetError { }, } -#[cfg(feature = "std")] -impl std::fmt::Display for TryGetError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { +impl core::fmt::Display for TryGetError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { match self { TryGetError::OutOfBytes { requested, From aae8ba8468035d6a48a33fa935d7084256d1e738 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Thu, 16 Jan 2025 20:28:10 +0100 Subject: [PATCH 14/15] Changed TryGetError from enum to struct. Implemented conversion to std::io::Error using From. Used TryGetError as the parameter for panic_advance, and moved TryGetError to before panic_advance in lib.rs. Removed unwrap_advance, as calling panic_advance is now convenient enough. --- src/buf/buf_impl.rs | 249 ++++++++++++++++++++------------------------ src/buf/buf_mut.rs | 52 +++++++-- src/buf/mod.rs | 2 +- src/bytes_mut.rs | 7 +- src/lib.rs | 40 ++++++- 5 files changed, 199 insertions(+), 151 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 608b301f5..d9c51aac2 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -3,51 +3,19 @@ use crate::buf::{reader, Reader}; use crate::buf::{take, Chain, Take}; #[cfg(feature = "std")] use crate::{min_u64_usize, saturating_sub_usize_u64}; -use crate::{panic_advance, panic_does_not_fit}; +use crate::{panic_advance, panic_does_not_fit, TryGetError}; #[cfg(feature = "std")] use std::io::IoSlice; use alloc::boxed::Box; -/// Error type for the `try_get_` methods of [`Buf`]. -#[derive(Debug, PartialEq, Eq)] -pub enum TryGetError { - /// Indicates that there were not enough remaining - /// bytes in the buffer while attempting - /// to get a value from a [`Buf`] with one - /// of the `try_get_` methods. - OutOfBytes { - /// The number of bytes necessary to get the value - requested: usize, - - /// The number of bytes available in the buffer - available: usize, - }, -} - -impl core::fmt::Display for TryGetError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { - match self { - TryGetError::OutOfBytes { - requested, - available, - } => { - write!(f, "Not enough bytes remaining in buffer to read value (requested {} but only {} available)", requested, available) - } - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for TryGetError {} - macro_rules! buf_try_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ const SIZE: usize = core::mem::size_of::<$typ>(); if $this.remaining() < SIZE { - return Err(TryGetError::OutOfBytes { + return Err(TryGetError { requested: SIZE, available: $this.remaining(), }); @@ -103,25 +71,19 @@ macro_rules! buf_try_get_impl { macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ - return unwrap_advance((|| buf_try_get_impl!($this, $typ::$conv))()); + return (|| buf_try_get_impl!($this, $typ::$conv))() + .unwrap_or_else(|error| panic_advance(&error)); }}; (le => $this:ident, $typ:tt, $len_to_read:expr) => {{ - return unwrap_advance((|| buf_try_get_impl!(le => $this, $typ, $len_to_read))()); + return (|| buf_try_get_impl!(le => $this, $typ, $len_to_read))() + .unwrap_or_else(|error| panic_advance(&error)); }}; (be => $this:ident, $typ:tt, $len_to_read:expr) => {{ - return unwrap_advance((|| buf_try_get_impl!(be => $this, $typ, $len_to_read))()); + return (|| buf_try_get_impl!(be => $this, $typ, $len_to_read))() + .unwrap_or_else(|error| panic_advance(&error)); }}; } -fn unwrap_advance(value: Result) -> T { - value.unwrap_or_else( - |TryGetError::OutOfBytes { - requested, - available, - }| panic_advance(requested, available), - ) -} - // https://en.wikipedia.org/wiki/Sign_extension fn sign_extend(val: u64, nbytes: usize) -> i64 { let shift = (8 - nbytes) * 8; @@ -330,7 +292,8 @@ pub trait Buf { /// /// This function panics if `self.remaining() < dst.len()`. fn copy_to_slice(&mut self, dst: &mut [u8]) { - unwrap_advance(self.try_copy_to_slice(dst)); + self.try_copy_to_slice(dst) + .unwrap_or_else(|error| panic_advance(&error)); } /// Gets an unsigned 8 bit integer from `self`. @@ -351,7 +314,10 @@ pub trait Buf { /// This function panics if there is no more remaining data in `self`. fn get_u8(&mut self) -> u8 { if self.remaining() < 1 { - panic_advance(1, 0); + panic_advance(&TryGetError { + requested: 1, + available: 0, + }) } let ret = self.chunk()[0]; self.advance(1); @@ -376,7 +342,10 @@ pub trait Buf { /// This function panics if there is no more remaining data in `self`. fn get_i8(&mut self) -> i8 { if self.remaining() < 1 { - panic_advance(1, 0); + panic_advance(&TryGetError { + requested: 1, + available: 0, + }); } let ret = self.chunk()[0] as i8; self.advance(1); @@ -1164,7 +1133,7 @@ pub trait Buf { /// The cursor is advanced by the number of bytes copied. `self` must have /// enough remaining bytes to fill `dst`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1186,12 +1155,12 @@ pub trait Buf { /// let mut buf = &b"hello world"[..]; /// let mut dst = [0; 12]; /// - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 12, available: 11}), buf.try_copy_to_slice(&mut dst)); + /// assert_eq!(Err(TryGetError{requested: 12, available: 11}), buf.try_copy_to_slice(&mut dst)); /// assert_eq!(11, buf.remaining()); /// ``` fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { if self.remaining() < dst.len() { - return Err(TryGetError::OutOfBytes { + return Err(TryGetError { requested: dst.len(), available: self.remaining(), }); @@ -1213,7 +1182,7 @@ pub trait Buf { /// /// The current position is advanced by 1. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1230,11 +1199,11 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 1, available: 0}), buf.try_get_u8()); + /// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_u8()); /// ``` fn try_get_u8(&mut self) -> Result { if self.remaining() < 1 { - return Err(TryGetError::OutOfBytes { + return Err(TryGetError { requested: 1, available: self.remaining(), }); @@ -1248,7 +1217,7 @@ pub trait Buf { /// /// The current position is advanced by 1. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1265,11 +1234,11 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b""[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 1, available: 0}), buf.try_get_i8()); + /// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_i8()); /// ``` fn try_get_i8(&mut self) -> Result { if self.remaining() < 1 { - return Err(TryGetError::OutOfBytes { + return Err(TryGetError { requested: 1, available: self.remaining(), }); @@ -1283,7 +1252,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1300,7 +1269,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16(&mut self) -> Result { @@ -1311,7 +1280,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1328,7 +1297,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16_le()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_le()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_le(&mut self) -> Result { @@ -1339,7 +1308,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1359,7 +1328,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_u16_ne()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_u16_ne(&mut self) -> Result { @@ -1370,7 +1339,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1387,7 +1356,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16(&mut self) -> Result { @@ -1398,7 +1367,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1415,7 +1384,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16_le()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_le()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_le(&mut self) -> Result { @@ -1426,7 +1395,7 @@ pub trait Buf { /// /// The current position is advanced by 2. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1446,7 +1415,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 2, available: 1}), buf.try_get_i16_ne()); + /// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_ne()); /// assert_eq!(1, buf.remaining()); /// ``` fn try_get_i16_ne(&mut self) -> Result { @@ -1457,7 +1426,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1474,7 +1443,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32(&mut self) -> Result { @@ -1485,7 +1454,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1502,7 +1471,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32_le()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_le(&mut self) -> Result { @@ -1513,7 +1482,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1533,7 +1502,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_u32_ne()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_u32_ne(&mut self) -> Result { @@ -1544,7 +1513,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1561,7 +1530,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32(&mut self) -> Result { @@ -1572,7 +1541,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1589,7 +1558,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32_le()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_le(&mut self) -> Result { @@ -1600,7 +1569,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1620,7 +1589,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x09\xA0"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_i32_ne()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_i32_ne(&mut self) -> Result { @@ -1631,7 +1600,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1648,7 +1617,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64(&mut self) -> Result { @@ -1659,7 +1628,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1676,7 +1645,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64_le()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_le(&mut self) -> Result { @@ -1687,7 +1656,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1707,7 +1676,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_u64_ne()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_u64_ne(&mut self) -> Result { @@ -1718,7 +1687,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1735,7 +1704,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64(&mut self) -> Result { @@ -1746,7 +1715,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1763,7 +1732,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64_le()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_le(&mut self) -> Result { @@ -1774,7 +1743,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1794,7 +1763,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_i64_ne()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_i64_ne(&mut self) -> Result { @@ -1805,7 +1774,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1822,7 +1791,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128(&mut self) -> Result { @@ -1833,7 +1802,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1850,7 +1819,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128_le()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_le()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_le(&mut self) -> Result { @@ -1861,7 +1830,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1881,7 +1850,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_u128_ne()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_u128_ne(&mut self) -> Result { @@ -1892,7 +1861,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1909,7 +1878,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128(&mut self) -> Result { @@ -1920,7 +1889,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1937,7 +1906,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128_le()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_le()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_le(&mut self) -> Result { @@ -1948,7 +1917,7 @@ pub trait Buf { /// /// The current position is advanced by 16. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1968,7 +1937,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 16, available: 15}), buf.try_get_i128_ne()); + /// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_ne()); /// assert_eq!(15, buf.remaining()); /// ``` fn try_get_i128_ne(&mut self) -> Result { @@ -1979,7 +1948,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -1996,7 +1965,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2011,7 +1980,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2028,7 +1997,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint_le(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2043,7 +2012,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2066,7 +2035,7 @@ pub trait Buf { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_uint_ne(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2085,7 +2054,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2102,7 +2071,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2117,7 +2086,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2134,7 +2103,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x01\x02\x03"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int_le(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_le(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2149,7 +2118,7 @@ pub trait Buf { /// /// The current position is advanced by `nbytes`. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2172,7 +2141,7 @@ pub trait Buf { /// true => b"\x01\x02\x03", /// false => b"\x03\x02\x01", /// }; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_int_ne(4)); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_ne(4)); /// assert_eq!(3, buf.remaining()); /// ``` /// @@ -2192,7 +2161,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2209,7 +2178,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32(&mut self) -> Result { @@ -2221,7 +2190,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2238,7 +2207,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32_le()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_le()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_le(&mut self) -> Result { @@ -2250,7 +2219,7 @@ pub trait Buf { /// /// The current position is advanced by 4. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2270,7 +2239,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\x99\x99"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 4, available: 3}), buf.try_get_f32_ne()); + /// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_ne()); /// assert_eq!(3, buf.remaining()); /// ``` fn try_get_f32_ne(&mut self) -> Result { @@ -2282,7 +2251,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2299,7 +2268,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64(&mut self) -> Result { @@ -2311,7 +2280,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2328,7 +2297,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64_le()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_le()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_le(&mut self) -> Result { @@ -2340,7 +2309,7 @@ pub trait Buf { /// /// The current position is advanced by 8. /// - /// Returns `Err(TryGetError::OutOfBytes)` when there are not enough + /// Returns `Err(TryGetError)` when there are not enough /// remaining bytes to read the value. /// /// # Examples @@ -2360,7 +2329,7 @@ pub trait Buf { /// use bytes::{Buf, TryGetError}; /// /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; - /// assert_eq!(Err(TryGetError::OutOfBytes{requested: 8, available: 7}), buf.try_get_f64_ne()); + /// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_ne()); /// assert_eq!(7, buf.remaining()); /// ``` fn try_get_f64_ne(&mut self) -> Result { @@ -2390,7 +2359,10 @@ pub trait Buf { use super::BufMut; if self.remaining() < len { - panic_advance(len, self.remaining()); + panic_advance(&TryGetError { + requested: len, + available: self.remaining(), + }); } let mut ret = crate::BytesMut::with_capacity(len); @@ -2928,7 +2900,10 @@ impl Buf for &[u8] { #[inline] fn advance(&mut self, cnt: usize) { if self.len() < cnt { - panic_advance(cnt, self.len()); + panic_advance(&TryGetError { + requested: cnt, + available: self.len(), + }); } *self = &self[cnt..]; @@ -2937,7 +2912,10 @@ impl Buf for &[u8] { #[inline] fn copy_to_slice(&mut self, dst: &mut [u8]) { if self.len() < dst.len() { - panic_advance(dst.len(), self.len()); + panic_advance(&TryGetError { + requested: dst.len(), + available: self.len(), + }); } dst.copy_from_slice(&self[..dst.len()]); @@ -2967,7 +2945,10 @@ impl> Buf for std::io::Cursor { // We intentionally allow `cnt == 0` here even if `pos > len`. let max_cnt = saturating_sub_usize_u64(len, pos); if cnt > max_cnt { - panic_advance(cnt, max_cnt); + panic_advance(&TryGetError { + requested: cnt, + available: max_cnt, + }); } // This will not overflow because either `cnt == 0` or the sum is not diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 2a3b5e9ee..26645c6ae 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,7 +1,7 @@ use crate::buf::{limit, Chain, Limit, UninitSlice}; #[cfg(feature = "std")] use crate::buf::{writer, Writer}; -use crate::{panic_advance, panic_does_not_fit}; +use crate::{panic_advance, panic_does_not_fit, TryGetError}; use core::{mem, ptr, usize}; @@ -204,7 +204,10 @@ pub unsafe trait BufMut { Self: Sized, { if self.remaining_mut() < src.remaining() { - panic_advance(src.remaining(), self.remaining_mut()); + panic_advance(&TryGetError { + requested: src.remaining(), + available: self.remaining_mut(), + }); } while src.has_remaining() { @@ -242,7 +245,10 @@ pub unsafe trait BufMut { #[inline] fn put_slice(&mut self, mut src: &[u8]) { if self.remaining_mut() < src.len() { - panic_advance(src.len(), self.remaining_mut()); + panic_advance(&TryGetError { + requested: src.len(), + available: self.remaining_mut(), + }); } while !src.is_empty() { @@ -285,7 +291,10 @@ pub unsafe trait BufMut { #[inline] fn put_bytes(&mut self, val: u8, mut cnt: usize) { if self.remaining_mut() < cnt { - panic_advance(cnt, self.remaining_mut()); + panic_advance(&TryGetError { + requested: cnt, + available: self.remaining_mut(), + }) } while cnt > 0 { @@ -1487,7 +1496,10 @@ unsafe impl BufMut for &mut [u8] { #[inline] unsafe fn advance_mut(&mut self, cnt: usize) { if self.len() < cnt { - panic_advance(cnt, self.len()); + panic_advance(&TryGetError { + requested: cnt, + available: self.len(), + }); } // Lifetime dance taken from `impl Write for &mut [u8]`. @@ -1498,7 +1510,10 @@ unsafe impl BufMut for &mut [u8] { #[inline] fn put_slice(&mut self, src: &[u8]) { if self.len() < src.len() { - panic_advance(src.len(), self.len()); + panic_advance(&TryGetError { + requested: src.len(), + available: self.len(), + }); } self[..src.len()].copy_from_slice(src); @@ -1509,7 +1524,10 @@ unsafe impl BufMut for &mut [u8] { #[inline] fn put_bytes(&mut self, val: u8, cnt: usize) { if self.len() < cnt { - panic_advance(cnt, self.len()); + panic_advance(&TryGetError { + requested: cnt, + available: self.len(), + }); } // SAFETY: We just checked that the pointer is valid for `cnt` bytes. @@ -1534,7 +1552,10 @@ unsafe impl BufMut for &mut [core::mem::MaybeUninit] { #[inline] unsafe fn advance_mut(&mut self, cnt: usize) { if self.len() < cnt { - panic_advance(cnt, self.len()); + panic_advance(&TryGetError { + requested: cnt, + available: self.len(), + }); } // Lifetime dance taken from `impl Write for &mut [u8]`. @@ -1545,7 +1566,10 @@ unsafe impl BufMut for &mut [core::mem::MaybeUninit] { #[inline] fn put_slice(&mut self, src: &[u8]) { if self.len() < src.len() { - panic_advance(src.len(), self.len()); + panic_advance(&TryGetError { + requested: src.len(), + available: self.len(), + }); } // SAFETY: We just checked that the pointer is valid for `src.len()` bytes. @@ -1558,7 +1582,10 @@ unsafe impl BufMut for &mut [core::mem::MaybeUninit] { #[inline] fn put_bytes(&mut self, val: u8, cnt: usize) { if self.len() < cnt { - panic_advance(cnt, self.len()); + panic_advance(&TryGetError { + requested: cnt, + available: self.len(), + }); } // SAFETY: We just checked that the pointer is valid for `cnt` bytes. @@ -1582,7 +1609,10 @@ unsafe impl BufMut for Vec { let remaining = self.capacity() - len; if remaining < cnt { - panic_advance(cnt, remaining); + panic_advance(&TryGetError { + requested: cnt, + available: remaining, + }); } // Addition will not overflow since the sum is at most the capacity. diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 9f6f8d8f9..1bf0a47e8 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -27,7 +27,7 @@ mod vec_deque; #[cfg(feature = "std")] mod writer; -pub use self::buf_impl::{Buf, TryGetError}; +pub use self::buf_impl::Buf; pub use self::buf_mut::BufMut; pub use self::chain::Chain; pub use self::iter::IntoIter; diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index a0d77bc24..d5db5124b 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -17,7 +17,7 @@ use crate::bytes::Vtable; #[allow(unused)] use crate::loom::sync::atomic::AtomicMut; use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; -use crate::{offset_from, Buf, BufMut, Bytes}; +use crate::{offset_from, Buf, BufMut, Bytes, TryGetError}; /// A unique reference to a contiguous slice of memory. /// @@ -1178,7 +1178,10 @@ unsafe impl BufMut for BytesMut { unsafe fn advance_mut(&mut self, cnt: usize) { let remaining = self.cap - self.len(); if cnt > remaining { - super::panic_advance(cnt, remaining); + super::panic_advance(&TryGetError { + requested: cnt, + available: remaining, + }); } // Addition won't overflow since it is at most `self.cap`. self.len = self.len() + cnt; diff --git a/src/lib.rs b/src/lib.rs index dd34091c7..a9fe2f897 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ extern crate alloc; extern crate std; pub mod buf; -pub use crate::buf::{Buf, BufMut, TryGetError}; +pub use crate::buf::{Buf, BufMut}; mod bytes; mod bytes_mut; @@ -132,12 +132,46 @@ fn min_u64_usize(a: u64, b: usize) -> usize { } } +/// Error type for the `try_get_` methods of [`Buf`]. +/// Indicates that there were not enough remaining +/// bytes in the buffer while attempting +/// to get a value from a [`Buf`] with one +/// of the `try_get_` methods. +#[derive(Debug, PartialEq, Eq)] +pub struct TryGetError { + /// The number of bytes necessary to get the value + pub requested: usize, + + /// The number of bytes available in the buffer + pub available: usize, +} + +impl core::fmt::Display for TryGetError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + write!( + f, + "Not enough bytes remaining in buffer to read value (requested {} but only {} available)", + self.requested, + self.available) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for TryGetError {} + +#[cfg(feature = "std")] +impl From for std::io::Error { + fn from(error: TryGetError) -> Self { + std::io::Error::new(std::io::ErrorKind::Other, error) + } +} + /// Panic with a nice error message. #[cold] -fn panic_advance(idx: usize, len: usize) -> ! { +fn panic_advance(error_info: &TryGetError) -> ! { panic!( "advance out of bounds: the len is {} but advancing by {}", - len, idx + error_info.available, error_info.requested ); } From 67541903983def50751c5f0e61ba6a2854af57e5 Mon Sep 17 00:00:00 2001 From: Helge Penne Date: Fri, 17 Jan 2025 05:59:09 +0100 Subject: [PATCH 15/15] Small code formatting fix in Display for TryGetError. --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a9fe2f897..cfc828130 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,7 +152,8 @@ impl core::fmt::Display for TryGetError { f, "Not enough bytes remaining in buffer to read value (requested {} but only {} available)", self.requested, - self.available) + self.available + ) } }