Skip to main content

le_stream/
consume.rs

1use crate::{FromLeStream, Result};
2
3/// Consumes a stream of bytes with little endianness to crate an object.
4pub trait Consume<T> {
5    /// Consumes the iterator to create an instance of `T`.
6    ///
7    /// # Errors
8    ///
9    /// Returns an [`Error`] if the stream terminates prematurely or contains excess bytes.
10    fn consume(self) -> Result<T>;
11
12    /// Consumes the iterator partially to create an instance of `T`.
13    fn consume_partial(self) -> Option<T>;
14}
15
16impl<T, I> Consume<T> for I
17where
18    T: FromLeStream,
19    I: Iterator<Item = u8>,
20{
21    fn consume(self) -> Result<T> {
22        T::from_le_stream_exact(self)
23    }
24
25    fn consume_partial(self) -> Option<T> {
26        T::from_le_stream(self)
27    }
28}