modular_bitfield_ordering/
lib.rs

1#![no_std]
2#![allow(non_camel_case_types)]
3
4use modular_bitfield::Specifier;
5use modular_bitfield::error::{InvalidBitPattern, OutOfBounds};
6
7pub struct u8be;
8pub struct u16be;
9pub struct u32be;
10pub struct u64be;
11pub struct u128be;
12
13pub struct u8le;
14pub struct u16le;
15pub struct u32le;
16pub struct u64le;
17pub struct u128le;
18
19
20macro_rules! impl_ordering_specifier {
21    ( $( ($typ:ty, $prim:ty: $bits:literal, $to_func:ident, $from_func:ident) ),* $(,)? ) => {
22        $(
23            impl Specifier for $typ {
24                const BITS: usize = $bits;
25                type Bytes = $prim;
26                type InOut = $prim;
27
28                #[inline]
29                fn into_bytes(input: Self::InOut) -> Result<Self::Bytes, OutOfBounds> {
30                    Ok(input.$to_func())
31                }
32
33                #[inline]
34                fn from_bytes(bytes: Self::Bytes) -> Result<Self::InOut, InvalidBitPattern<Self::Bytes>> {
35                    Ok(<$prim>::$from_func(bytes))
36                }
37            }
38        )*
39    };
40}
41
42impl_ordering_specifier!(
43    (u8be, u8: 8, to_be, from_be),
44    (u16be, u16: 16, to_be, from_be),
45    (u32be, u32: 32, to_be, from_be),
46    (u64be, u64: 64, to_be, from_be),
47    (u128be, u128: 128, to_be, from_be),
48
49    (u8le, u8: 8, to_le, from_le),
50    (u16le, u16: 16, to_le, from_le),
51    (u32le, u32: 32, to_le, from_le),
52    (u64le, u64: 64, to_le, from_le),
53    (u128le, u128: 128, to_le, from_le),
54);
55
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use modular_bitfield::{bitfield};
61
62    #[bitfield]
63    struct Foo {
64        x8: u8be,
65        x16: u16be,
66        x32: u32be,
67        x64: u64be,
68        x128: u128be,
69
70        c8: u8le,
71        c16: u16le,
72        c32: u32le,
73        c64: u64le,
74        c128: u128le,
75
76    }
77
78    #[test]
79    fn it_works() {
80        let _ = Foo::new().into_bytes();
81        let arr = [
82            42,
83            0x16u8, 0xd2,
84            0x37, 0x38, 0x39, 0x3a,
85            0x41,0x42,0x43,0x44, 0x45,0x46,0x47,0x48,
86            0x50,0x51,0x52,0x53, 0x54,0x55,0x56,0x57, 0x58,0x59,0x5a,0x5b, 0x5c,0x5d,0x5e,0x5f,
87
88            42,
89            0x16u8, 0xd2,
90            0x37, 0x38, 0x39, 0x3a,
91            0x41,0x42,0x43,0x44, 0x45,0x46,0x47,0x48,
92            0x50,0x51,0x52,0x53, 0x54,0x55,0x56,0x57, 0x58,0x59,0x5a,0x5b, 0x5c,0x5d,0x5e,0x5f,
93        ];
94        let foo = Foo::from_bytes(arr);
95
96        assert_eq!(foo.x8(), 42);
97        assert_eq!(foo.x16(), 0x16d2);
98        assert_eq!(foo.x32(), 0x3738393a);
99        assert_eq!(foo.x64(), 0x4142434445464748);
100        assert_eq!(foo.x128(), 0x505152535455565758595a5b5c5d5e5f);
101
102        assert_eq!(foo.c8(), 42);
103        assert_eq!(foo.c16(), 0xd216);
104        assert_eq!(foo.c32(), 0x3a393837);
105        assert_eq!(foo.c64(), 0x4847464544434241);
106        assert_eq!(foo.c128(), 0x5f5e5d5c5b5a59585756555453525150);
107    }
108}