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