le_stream/
try_from_le_stream.rs

1use crate::FromLeStream;
2
3/// Try to parse an object from a stream of bytes with little endianness.
4pub trait TryFromLeStream<T>: TryFrom<T> {
5    /// Try to parse an object from a stream of bytes with little endianness.
6    ///
7    /// # Errors
8    ///
9    /// Returns `None` if the stream terminates prematurely, or `Some(error)` if the parsing fails.
10    fn try_from_le_stream<I>(bytes: I) -> Result<Self, Option<<Self as TryFrom<T>>::Error>>
11    where
12        I: Iterator<Item = u8>;
13}
14
15impl<T, U> TryFromLeStream<T> for U
16where
17    T: FromLeStream,
18    U: TryFrom<T>,
19{
20    fn try_from_le_stream<I>(bytes: I) -> Result<Self, Option<<U as TryFrom<T>>::Error>>
21    where
22        I: Iterator<Item = u8>,
23    {
24        T::from_le_stream(bytes).map_or_else(|| Err(None), |value| U::try_from(value).map_err(Some))
25    }
26}