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
#[doc(hidden)]
macro_rules! __byte_array_newtype {
    ($name:ident, $length:path, $type:ty) => {
        #[repr(C)]
        #[derive(Clone)]
        struct $name($type);

        __byte_array_newtype_impl!($name, $length, $type);
    };
    (pub $name:ident, $length:path, $type:ty) => {
        #[repr(C)]
        #[derive(Clone)]
        pub struct $name($type);

        __byte_array_newtype_impl!($name, $length, $type);
    };
    ($doc:meta, pub $name:ident, $length:path, $type:ty) => {
        #[repr(C)]
        #[derive(Clone)]
        #[$doc]
        pub struct $name($type);

        __byte_array_newtype_impl!($name, $length, $type);
    };
    (pub(crate) $name:ident, $length:path, $type:ty) => {
        #[repr(C)]
        #[derive(Clone)]
        pub(crate) struct $name($type);

        __byte_array_newtype_impl!($name, $length, $type);
    };
}

#[doc(hidden)]
macro_rules! __byte_array_newtype_impl {
    ($name:ident, $length:expr, $type:ty) => {
        impl $name {
            #[allow(unused)]
            pub fn into_bytes(self) -> $type {
                self.into()
            }

            #[allow(unused)]
            pub fn as_bytes(&self) -> &$type {
                self.as_ref()
            }

            #[allow(unused)]
            pub fn as_slice(&self) -> &[u8] {
                self.as_ref()
            }

            #[allow(unused)]
            pub fn as_mut_slice(&mut self) -> &mut [u8] {
                self.as_mut()
            }

            #[allow(unused)]
            pub fn from_bytes(bytes: &[u8]) -> Result<$name, crate::Error> {
                if bytes.len() != $length {
                    let err = crate::Error::BadLength {
                        name: stringify!($name),
                        actual: bytes.len(),
                        expected: $length,
                    };
                    return Err(err);
                }
                let mut result = Self::default();
                result.0.copy_from_slice(bytes);
                Ok(result)
            }
        }

        impl Default for $name {
            #[allow(unused)]
            fn default() -> $name {
                $name([0; $length])
            }
        }

        impl From<$type> for $name {
            #[allow(unused)]
            fn from(inner: $type) -> $name {
                $name(inner)
            }
        }

        impl Into<$type> for $name {
            #[allow(unused)]
            fn into(self) -> $type {
                self.0
            }
        }

        impl AsRef<$type> for $name {
            #[allow(unused)]
            fn as_ref(&self) -> &$type {
                &self.0
            }
        }

        impl AsMut<$type> for $name {
            #[allow(unused)]
            fn as_mut(&mut self) -> &mut $type {
                &mut self.0
            }
        }

        impl AsRef<[u8]> for $name {
            #[allow(unused)]
            fn as_ref(&self) -> &[u8] {
                &self.0
            }
        }

        impl AsMut<[u8]> for $name {
            #[allow(unused)]
            fn as_mut(&mut self) -> &mut [u8] {
                &mut self.0
            }
        }
    };
}