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
macro_rules! impl_smart_ptr_type {
    ($ty:ident) => {
        #[allow(unused_imports)]
        mod impl_protocol {
            use super::*;
            use std::ops::Deref;

            impl<T: crate::Protocol> crate::Protocol for $ty<T> {
                fn read(
                    read: &mut dyn crate::BitRead,
                    byte_order: crate::ByteOrder,
                    ctx: &mut dyn core::any::Any,
                ) -> Result<Self, crate::Error> {
                    let value = T::read(read, byte_order, ctx)?;
                    Ok($ty::new(value))
                }

                fn write(
                    &self,
                    write: &mut dyn crate::BitWrite,
                    byte_order: crate::ByteOrder,
                    ctx: &mut dyn core::any::Any,
                ) -> Result<(), crate::Error> {
                    self.deref().write(write, byte_order, ctx)
                }
            }
        }

        #[cfg(test)]
        #[allow(unused_imports)]
        mod tests {
            use super::*;

            #[test]
            fn read_protocol() {
                assert_eq!(
                    <$ty<u8> as crate::Protocol>::read(
                        &mut bitstream_io::BitReader::endian(
                            [7u8].as_slice(),
                            bitstream_io::BigEndian
                        ),
                        crate::ByteOrder::BigEndian,
                        &mut ()
                    )
                    .unwrap(),
                    $ty::new(7)
                )
            }

            #[test]
            fn write_protocol() {
                let mut data: Vec<u8> = Vec::new();
                crate::Protocol::write(
                    &$ty::new(7u8),
                    &mut bitstream_io::BitWriter::endian(&mut data, bitstream_io::BigEndian),
                    crate::ByteOrder::BigEndian,
                    &mut (),
                )
                .unwrap();
                assert_eq!(vec![7], data);
            }
        }
    };
}

mod box_ {
    impl_smart_ptr_type!(Box);
}

mod rc {
    use std::rc::Rc;

    impl_smart_ptr_type!(Rc);
}

mod arc {
    use std::sync::Arc;

    impl_smart_ptr_type!(Arc);
}