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