modular_bitfield_msb/private/
impls.rs1use crate::{
2 error::{
3 InvalidBitPattern,
4 OutOfBounds,
5 },
6 Specifier,
7};
8
9impl Specifier for bool {
10 const BITS: usize = 1;
11 const STRUCT: bool = false;
12 type Bytes = u8;
13 type InOut = bool;
14
15 #[inline]
16 fn into_bytes(input: Self::InOut) -> Result<Self::Bytes, OutOfBounds> {
17 Ok(input as u8)
18 }
19
20 #[inline]
21 fn from_bytes(
22 bytes: Self::Bytes,
23 ) -> Result<Self::InOut, InvalidBitPattern<Self::Bytes>> {
24 match bytes {
25 0 => Ok(false),
26 1 => Ok(true),
27 invalid_bytes => Err(InvalidBitPattern { invalid_bytes }),
28 }
29 }
30}
31
32macro_rules! impl_specifier_for_primitive {
33 ( $( ($prim:ty: $bits:literal) ),* $(,)? ) => {
34 $(
35 impl Specifier for $prim {
36 const BITS: usize = $bits;
37 const STRUCT: bool = false;
38 type Bytes = $prim;
39 type InOut = $prim;
40
41 #[inline]
42 fn into_bytes(input: Self::InOut) -> Result<Self::Bytes, OutOfBounds> {
43 Ok(input)
44 }
45
46 #[inline]
47 fn from_bytes(bytes: Self::Bytes) -> Result<Self::InOut, InvalidBitPattern<Self::Bytes>> {
48 Ok(bytes)
49 }
50 }
51 )*
52 };
53}
54impl_specifier_for_primitive!(
55 (u8: 8),
56 (u16: 16),
57 (u32: 32),
58 (u64: 64),
59 (u128: 128),
60);