toad_msg/from_bytes.rs
1use toad_cursor::Cursor;
2
3/// Trait for converting a sequence of bytes into some data structure
4pub trait TryFromBytes<A: AsRef<[u8]>>: Sized {
5 /// Error type yielded if conversion fails
6 type Error;
7
8 /// Try to convert from some sequence of bytes `T`
9 /// into `Self`
10 fn try_from_bytes(bytes: A) -> Result<Self, Self::Error>;
11}
12
13/// Trait adding the ability for a _piece_ of a data structure to parse itself by mutating a cursor over a byte buffer.
14pub(crate) trait TryConsumeBytes<A: AsRef<[u8]>>: Sized {
15 /// Error type yielded if conversion fails
16 type Error;
17
18 /// Try to convert from some sequence of bytes `T`
19 /// into `Self`
20 fn try_consume_bytes(bytes: &mut Cursor<A>) -> Result<Self, Self::Error>;
21}