bin_layout/types/
scalar.rs1use crate::*;
2
3impl Encoder for bool {
4 #[inline]
5 fn encoder(&self, writer: &mut impl Write) -> io::Result<()> {
6 writer.write_all(&[*self as u8])
7 }
8}
9
10impl Decoder<'_> for bool {
11 #[inline]
12 fn decoder(c: &mut &[u8]) -> Result<Self> {
13 u8::decoder(c).map(|byte| byte != 0)
14 }
15}
16
17impl Encoder for char {
18 #[inline]
19 fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
20 u32::from(*self).encoder(c)
21 }
22}
23impl Decoder<'_> for char {
24 #[inline]
25 fn decoder(c: &mut &[u8]) -> Result<Self> {
26 let num = u32::decoder(c)?;
27 char::from_u32(num).ok_or_else(|| format!("{num} is not a valid char").into())
28 }
29}
30
31impl Encoder for u8 {
34 #[inline]
35 fn encoder(&self, writer: &mut impl Write) -> io::Result<()> {
36 writer.write_all(&[*self])
37 }
38}
39
40impl Decoder<'_> for u8 {
41 #[inline]
42 fn decoder(reader: &mut &[u8]) -> Result<Self> {
43 if !reader.is_empty() {
44 unsafe {
45 let slice = reader.get_unchecked(0);
46 *reader = reader.get_unchecked(1..);
47 Ok(*slice)
48 }
49 } else {
50 Err("Insufficient bytes".into())
51 }
52 }
53}
54
55impl Encoder for i8 {
56 #[inline]
57 fn encoder(&self, writer: &mut impl Write) -> io::Result<()> {
58 writer.write_all(&[*self as u8])
59 }
60}
61impl Decoder<'_> for i8 {
62 #[inline]
63 fn decoder(c: &mut &[u8]) -> Result<Self> {
64 u8::decoder(c).map(|byte| byte as i8)
65 }
66}
67macro_rules! impl_data_type_for {
68 [$($rty:ty)*] => ($(
69 impl Encoder for $rty {
70 #[inline] fn encoder(&self, writer: &mut impl Write) -> io::Result<()> {
71 #[cfg(not(any(feature = "BE", feature = "NE")))]
72 return writer.write_all(&self.to_le_bytes());
73 #[cfg(feature = "BE")]
74 return writer.write_all(&self.to_be_bytes());
75 #[cfg(feature = "NE")]
76 return writer.write_all(&self.to_ne_bytes());
77 }
78 }
79 impl Decoder<'_> for $rty {
80 #[inline] fn decoder(c: &mut &[u8]) -> Result<Self> {
81 let arr = <&[u8; std::mem::size_of::<Self>()]>::decoder(c)?;
82 #[cfg(not(any(feature = "BE", feature = "NE")))]
83 return Ok(Self::from_le_bytes(*arr));
84 #[cfg(feature = "BE")]
85 return Ok(Self::from_be_bytes(*arr));
86 #[cfg(feature = "NE")]
87 return Ok(Self::from_ne_bytes(*arr));
88 }
89 }
90 )*);
91}
92
93impl_data_type_for!(
94 u16 u32 u64 u128
95 i16 i32 i64 i128
96 usize isize
97 f32 f64
98);
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_scaler_type() {
106 for word in [0x_A5C11, 0x_C0DE, 0x_DEC0DE, 0x_ADDED, 0x_AB0DE, 0x_CAFE] {
107 assert_eq!(word, u32::decode(&word.encode()).unwrap());
108 }
109 for word in [
110 0x_DEAD_BEEF,
111 0x_Faded_Face,
112 0x_BAD_F00D,
113 0x_C01D_C0FFEE,
114 0x_C0CA_C01A,
115 ] {
116 assert_eq!(word, u64::decode(&word.encode()).unwrap());
117 }
118 }
119}