usesuper::RmpRead; usecrate::decode::RmpReadErr; use core::fmt::{Display, Formatter};
/// Indicates that an error occurred reading from [Bytes] #[derive(Debug)] #[non_exhaustive] // NOTE: We can't use thiserror because of no_std :( pubenum BytesReadError { /// Indicates that there were not enough bytes.
InsufficientBytes {
expected: usize,
actual: usize,
position: u64,
},
}
impl Display for BytesReadError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match *self {
BytesReadError::InsufficientBytes { expected, actual, position } => {
write!(f, "Expected at least bytes {expected}, but only got {actual} (pos {position})")
}
}
}
} #[cfg(feature = "std")] impl std::error::Error for BytesReadError {} impl RmpReadErr for BytesReadError {}
/// A wrapper around `&[u8]` to read more efficiently. /// /// This has a specialized implementation of `RmpWrite` /// and has error type [Infallible](core::convert::Infallible). /// /// This has the additional benefit of working on `#[no_std]` (unlike the builtin Read trait) /// /// See also [serde_bytes::Bytes](https://docs.rs/serde_bytes/0.11/serde_bytes/struct.Bytes.html) /// /// Unlike a plain `&[u8]` this also tracks an internal offset in the input (See [`Self::position`]). /// /// This is used for (limited) compatibility with [`std::io::Cursor`]. Unlike a [Cursor](std::io::Cursor) it does /// not support mark/reset. #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Ord, PartialOrd)] pubstruct Bytes<'a> { /// The internal position of the input buffer. /// /// This is not required for correctness. /// It is only used for error reporting (and to implement [`Self::position`])
current_position: u64,
bytes: &'a [u8],
} impl<'a> Bytes<'a> { /// Wrap an existing bytes slice. /// /// This sets the internal position to zero. #[inline] #[must_use] pubfn new(bytes: &'a [u8]) -> Self {
Bytes { bytes, current_position: 0 }
} /// Get a reference to the remaining bytes in the buffer. #[inline] #[must_use] pubfn remaining_slice(&self) -> &'a [u8] { self.bytes
} /// Return the position of the input buffer. /// /// This is not required for correctness, it only exists to help mimic /// [`Cursor::position`](std::io::Cursor::position) #[inline] #[must_use] pubfn position(&self) -> u64 { self.current_position
}
} impl<'a> From<&'a [u8]> for Bytes<'a> { #[inline] fn from(bytes: &'a [u8]) -> Self {
Bytes { bytes, current_position: 0 }
}
}
impl RmpRead for Bytes<'_> { type Error = BytesReadError;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.