safer_bytes/lib.rs
1//! A safe, non-panicking wrapper around [`bytes::Buf`]
2
3#![deny(
4 clippy::all,
5 clippy::cargo,
6 missing_docs,
7 missing_copy_implementations,
8 missing_debug_implementations
9)]
10#![warn(clippy::pedantic)]
11
12use bytes::Buf;
13pub use bytes::{BufMut, Bytes, BytesMut};
14
15pub mod error;
16mod safe_buf;
17
18/// Unchecked buffer reading methods
19pub mod unchecked {
20 pub use bytes::Buf;
21}
22
23#[doc(inline)]
24pub use error::Error;
25
26/// Type alias for the return type of fallible functions in this crate
27pub type Result<T> = std::result::Result<T, Error>;
28
29pub use safe_buf::SafeBuf;
30
31/// Objects which implement [`FromBuf`] are capable of constructing themselves
32/// by reading bytes from a [`Buf`]
33pub trait FromBuf: Sized {
34 /// read an instance of `Self` from a buffer
35 ///
36 /// # Errors
37 ///
38 /// This method will return an error if the number of bytes remaining in the
39 /// buffer is insufficent, or if the type cannot be parsed from the bytes.
40 fn from_buf<B>(buffer: B) -> Result<Self>
41 where
42 B: Buf;
43}