Skip to main content

le_stream/from_le_stream/
core.rs

1use core::convert::Infallible;
2use core::iter::once;
3use core::marker::PhantomData;
4use core::ops::{Range, RangeInclusive};
5
6use crate::FromLeStream;
7
8macro_rules! impl_primitives {
9    ($($typ:ty,)+) => {
10        $(
11            impl FromLeStream for $typ {
12                fn from_le_stream<T>(bytes: T) -> Option<Self>
13                where
14                    T: Iterator<Item = u8>,
15                {
16                    <[u8; size_of::<Self>()]>::from_le_stream(bytes).map(Self::from_le_bytes)
17                }
18            }
19        )+
20    };
21}
22
23// Implement u8 separately, since all other conversions depend on it.
24impl_primitives!(
25    u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64,
26);
27
28impl FromLeStream for u8 {
29    fn from_le_stream<T>(mut bytes: T) -> Option<Self>
30    where
31        T: Iterator<Item = Self>,
32    {
33        bytes.next()
34    }
35}
36
37impl FromLeStream for Infallible {
38    fn from_le_stream<T>(_: T) -> Option<Self> {
39        unreachable!()
40    }
41}
42
43impl FromLeStream for () {
44    /// This is guaranteed to always return `Some(())`.
45    fn from_le_stream<T>(_: T) -> Option<Self>
46    where
47        T: Iterator<Item = u8>,
48    {
49        Some(())
50    }
51}
52
53impl<T> FromLeStream for PhantomData<T> {
54    /// This is guaranteed to always return `Some(PhantomData<T>)`.
55    fn from_le_stream<I>(_: I) -> Option<Self>
56    where
57        I: Iterator<Item = u8>,
58    {
59        Some(Self)
60    }
61}
62
63impl FromLeStream for bool {
64    fn from_le_stream<T>(mut bytes: T) -> Option<Self>
65    where
66        T: Iterator<Item = u8>,
67    {
68        bytes.next().map(|byte| byte != 0)
69    }
70}
71
72impl<T, const SIZE: usize> FromLeStream for [T; SIZE]
73where
74    T: FromLeStream,
75{
76    fn from_le_stream<I>(mut bytes: I) -> Option<Self>
77    where
78        I: Iterator<Item = u8>,
79    {
80        let mut array = [const { None }; SIZE];
81
82        for element in &mut array {
83            element.replace(T::from_le_stream(&mut bytes)?);
84        }
85
86        Some(array.map(|element| element.expect("All elements are initialized.")))
87    }
88}
89
90impl<T> FromLeStream for Option<T>
91where
92    T: FromLeStream,
93{
94    /// This is guaranteed to always return `Some(Option<T>)`.
95    fn from_le_stream<I>(mut bytes: I) -> Option<Self>
96    where
97        I: Iterator<Item = u8>,
98    {
99        bytes.next().map_or_else(
100            || Some(None),
101            |byte| T::from_le_stream(once(byte).chain(bytes)).map(Some),
102        )
103    }
104}
105
106impl<T> FromLeStream for Range<T>
107where
108    T: FromLeStream,
109{
110    fn from_le_stream<I>(mut bytes: I) -> Option<Self>
111    where
112        I: Iterator<Item = u8>,
113    {
114        let start = T::from_le_stream(&mut bytes)?;
115        let end = T::from_le_stream(&mut bytes)?;
116        Some(start..end)
117    }
118}
119
120impl<T> FromLeStream for RangeInclusive<T>
121where
122    T: FromLeStream,
123{
124    fn from_le_stream<I>(mut bytes: I) -> Option<Self>
125    where
126        I: Iterator<Item = u8>,
127    {
128        let start = T::from_le_stream(&mut bytes)?;
129        let end = T::from_le_stream(&mut bytes)?;
130        Some(start..=end)
131    }
132}