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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
pub use byte_struct_derive::{ByteStructLE, ByteStructBE}; pub trait ByteStruct { fn write_bytes(&self, bytes: &mut [u8]); fn read_bytes(bytes: &[u8]) -> Self; } pub trait ByteStructImpl { const BYTE_LEN: usize; fn write_le_bytes(&self, bytes: &mut [u8]); fn read_le_bytes(bytes: &[u8]) -> Self; fn write_be_bytes(&self, bytes: &mut [u8]); fn read_be_bytes(bytes: &[u8]) -> Self; } impl ByteStructImpl for u8 { const BYTE_LEN: usize = 1; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { u8::from_le_bytes([bytes[0]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { u8::from_be_bytes([bytes[0]]) } } impl ByteStructImpl for i8 { const BYTE_LEN: usize = 1; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { i8::from_le_bytes([bytes[0]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { i8::from_be_bytes([bytes[0]]) } } impl ByteStructImpl for u16 { const BYTE_LEN: usize = 2; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { u16::from_le_bytes([bytes[0], bytes[1]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { u16::from_be_bytes([bytes[0], bytes[1]]) } } impl ByteStructImpl for i16 { const BYTE_LEN: usize = 2; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { i16::from_le_bytes([bytes[0], bytes[1]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { i16::from_be_bytes([bytes[0], bytes[1]]) } } impl ByteStructImpl for u32 { const BYTE_LEN: usize = 4; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) } } impl ByteStructImpl for i32 { const BYTE_LEN: usize = 4; fn write_le_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_le_bytes()[..]); } fn read_le_bytes(bytes: &[u8]) -> Self { i32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) } fn write_be_bytes(&self, bytes: &mut [u8]) { bytes.copy_from_slice(&self.clone().to_be_bytes()[..]); } fn read_be_bytes(bytes: &[u8]) -> Self { i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) } } macro_rules! byte_struct_array { ($x:expr) => { impl<T: ByteStructImpl + Copy + Default> ByteStructImpl for [T; $x] { const BYTE_LEN: usize = ($x) * T::BYTE_LEN; fn write_le_bytes(&self, bytes: &mut [u8]) { let mut pos = 0; let len = T::BYTE_LEN; for i in 0 .. ($x) { self[i].write_le_bytes(&mut bytes[pos .. pos + len]); pos += len; } } fn read_le_bytes(bytes: &[u8]) -> Self { let mut pos = 0; let len = T::BYTE_LEN; let mut result = [T::default(); $x]; for i in 0 .. ($x) { result[i] = <T>::read_le_bytes(&bytes[pos .. pos + len]); pos += len; } result } fn write_be_bytes(&self, bytes: &mut [u8]) { let mut pos = 0; let len = T::BYTE_LEN; for i in 0 .. ($x) { self[i].write_be_bytes(&mut bytes[pos .. pos + len]); pos += len; } } fn read_be_bytes(bytes: &[u8]) -> Self { let mut pos = 0; let len = T::BYTE_LEN; let mut result = [T::default(); $x]; for i in 0 .. ($x) { result[i] = <T>::read_be_bytes(&bytes[pos .. pos + len]); pos += len; } result } } } } macro_rules! bsa0 { ($x:expr) => { byte_struct_array!($x);}} macro_rules! bsa1 { ($x:expr) => { bsa0!($x); bsa0!(1 + $x);}} macro_rules! bsa2 { ($x:expr) => { bsa1!($x); bsa1!(2 + $x);}} macro_rules! bsa3 { ($x:expr) => { bsa2!($x); bsa2!(4 + $x);}} macro_rules! bsa4 { ($x:expr) => { bsa3!($x); bsa3!(8 + $x);}} macro_rules! bsa5 { ($x:expr) => { bsa4!($x); bsa4!(16 + $x);}} bsa5!(1); byte_struct_array!(100); byte_struct_array!(3000); #[macro_export] macro_rules! bitfields{ ($visibility:vis $name:ident : $base:ty {$($field_vis:vis $field_name:ident : $field_len:expr),+ $(,)? }) => { #[derive(PartialEq, Debug, Default, Copy, Clone, Hash)] $visibility struct $name { $($field_vis $field_name: $base),* } impl $name { #[allow(unused_assignments)] fn from_raw(raw: $base) -> $name { let mut raw_v = raw; $( let mask: $base = (1 << $field_len) - 1; let $field_name = raw_v & mask; raw_v >>= $field_len; )* $name{$($field_name),*} } #[allow(unused_assignments)] fn to_raw(&self) -> $base { let mut raw: $base = 0; let mut pos = 0; $( raw |= self.$field_name << pos; pos += $field_len; )* raw } } impl ByteStructImpl for $name { const BYTE_LEN: usize = <$base>::BYTE_LEN; fn write_le_bytes(&self, bytes: &mut [u8]) { self.to_raw().write_le_bytes(bytes); } fn read_le_bytes(bytes: &[u8]) -> Self { <$name>::from_raw(<$base>::read_le_bytes(bytes)) } fn write_be_bytes(&self, bytes: &mut [u8]) { self.to_raw().write_be_bytes(bytes); } fn read_be_bytes(bytes: &[u8]) -> Self { <$name>::from_raw(<$base>::read_be_bytes(bytes)) } } } }