1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::{Error, Result};
mod core;
mod heapless;
mod intx;
mod macaddr;
mod std;
/// Parse an object from a stream of bytes with little endianness.
pub trait FromLeStream: Sized {
/// Parse an object from a stream of bytes with little endianness.
///
/// # Errors
///
/// Returns [`None`] if the stream terminates prematurely.
fn from_le_stream<T>(bytes: T) -> Option<Self>
where
T: Iterator<Item = u8>;
/// Parse an object from a stream of bytes with little endianness
/// that contains exactly the bytes to construct `Self`.
///
/// # Errors
///
/// Returns an [`Error`] if the stream terminates prematurely
/// or is not exhausted after deserializing `Self`.
fn from_le_stream_exact<T>(mut bytes: T) -> Result<Self>
where
T: Iterator<Item = u8>,
{
let instance = Self::from_le_stream(&mut bytes).ok_or(Error::UnexpectedEndOfStream)?;
if let Some(next_byte) = bytes.next() {
Err(Error::StreamNotExhausted {
instance,
next_byte,
})
} else {
Ok(instance)
}
}
/// Parse an object from a slice of bytes with little endianness
/// that contains exactly the bytes to construct `Self`.
///
/// # Errors
///
/// Returns an [`Error`] if the buffer is too small or contains excess data.
fn from_le_slice(bytes: &[u8]) -> Result<Self> {
Self::from_le_stream_exact(bytes.iter().copied())
}
}