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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::{BitField, BitRead, BitWrite, ByteOrder, Error, Protocol};
use core::any::Any;

impl BitField for bool {
    fn read(
        read: &mut dyn BitRead,
        _: ByteOrder,
        _: &mut dyn Any,
        bits: u32,
    ) -> Result<Self, Error> {
        if read.read_u8_bf(bits)? == 0 {
            Ok(false)
        } else {
            Ok(true)
        }
    }

    fn write(
        &self,
        write: &mut dyn BitWrite,
        _: ByteOrder,
        _: &mut dyn Any,
        bits: u32,
    ) -> Result<(), Error> {
        write.write_u8_bf(bits, if *self { 1 } else { 0 })?;
        Ok(())
    }
}

impl Protocol for bool {
    fn read(read: &mut dyn BitRead, _: ByteOrder, _: &mut dyn Any) -> Result<Self, Error> {
        if read.read_u8()? == 0 {
            Ok(false)
        } else {
            Ok(true)
        }
    }

    fn write(&self, write: &mut dyn BitWrite, _: ByteOrder, _: &mut dyn Any) -> Result<(), Error> {
        write.write_u8(if *self { 1 } else { 0 })?;
        Ok(())
    }
}

impl Protocol for u8 {
    fn read(read: &mut dyn BitRead, _: ByteOrder, _: &mut dyn Any) -> Result<Self, Error> {
        Ok(read.read_u8()?)
    }

    fn write(&self, write: &mut dyn BitWrite, _: ByteOrder, _: &mut dyn Any) -> Result<(), Error> {
        write.write_u8(*self)?;
        Ok(())
    }
}

impl Protocol for i8 {
    fn read(read: &mut dyn BitRead, _: ByteOrder, _: &mut dyn Any) -> Result<Self, Error> {
        Ok(read.read_i8()?)
    }

    fn write(&self, write: &mut dyn BitWrite, _: ByteOrder, _: &mut dyn Any) -> Result<(), Error> {
        write.write_i8(*self)?;
        Ok(())
    }
}

macro_rules! impl_parcel_for_numeric {
    ($ty:ident => [$read_fn:ident : $write_fn:ident]) => {
        impl Protocol for $ty {
            fn read(
                read: &mut dyn BitRead,
                byte_order: ByteOrder,
                _: &mut dyn Any,
            ) -> Result<Self, Error> {
                byte_order.$read_fn(read)
            }

            fn write(
                &self,
                write: &mut dyn BitWrite,
                byte_order: ByteOrder,
                _: &mut dyn Any,
            ) -> Result<(), Error> {
                byte_order.$write_fn(*self, write)?;
                Ok(())
            }
        }
    };
}

macro_rules! impl_bitfield_for_numeric {
    ($ty:ident => [$read_fn:ident : $write_fn:ident]) => {
        impl BitField for $ty {
            fn read(
                read: &mut dyn BitRead,
                _: ByteOrder,
                _: &mut dyn Any,
                bits: u32,
            ) -> Result<Self, Error> {
                Ok(BitRead::$read_fn(read, bits)?)
            }

            fn write(
                &self,
                write: &mut dyn BitWrite,
                _: ByteOrder,
                _: &mut dyn Any,
                bits: u32,
            ) -> Result<(), Error> {
                BitWrite::$write_fn(write, bits, *self)?;
                Ok(())
            }
        }
    };
}

impl_parcel_for_numeric!(u16 => [read_u16 : write_u16]);
impl_parcel_for_numeric!(i16 => [read_i16 : write_i16]);
impl_parcel_for_numeric!(u32 => [read_u32 : write_u32]);
impl_parcel_for_numeric!(i32 => [read_i32 : write_i32]);
impl_parcel_for_numeric!(u64 => [read_u64 : write_u64]);
impl_parcel_for_numeric!(i64 => [read_i64 : write_i64]);
impl_parcel_for_numeric!(u128 => [read_u128 : write_u128]);
impl_parcel_for_numeric!(i128 => [read_i128 : write_i128]);
impl_parcel_for_numeric!(f32 => [read_f32 : write_f32]);
impl_parcel_for_numeric!(f64 => [read_f64 : write_f64]);

impl_bitfield_for_numeric!(u8 => [read_u8_bf : write_u8_bf]);
impl_bitfield_for_numeric!(i8 => [read_i8_bf : write_i8_bf]);
impl_bitfield_for_numeric!(u16 => [read_u16_bf : write_u16_bf]);
impl_bitfield_for_numeric!(i16 => [read_i16_bf : write_i16_bf]);
impl_bitfield_for_numeric!(u32 => [read_u32_bf : write_u32_bf]);
impl_bitfield_for_numeric!(i32 => [read_i32_bf : write_i32_bf]);