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
use crate::{Error, Result};

mod impls;

/// 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: &mut 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>(bytes: &mut T) -> Result<Self>
    where
        T: Iterator<Item = u8>,
    {
        let instance = Self::from_le_stream(bytes).ok_or(Error::UnexpectedEndOfStream)?;
        bytes.next().map_or_else(
            || Ok(instance),
            |next_byte| Err(Error::StreamNotExhausted(next_byte)),
        )
    }
}