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