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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! # Byte Struct
//!
//! Pack and unpack structure as raw bytes with packed or bit field layout.
//!
//! ## Example
//! ```
//! use byte_struct::*;
//!
//! bitfields!(
//!     #[derive(PartialEq, Debug)]
//!     GIFColorTableInfo: u8 {
//!         global_color_table_flag: 1,
//!         color_resolution: 3,
//!         sort_flag: 1,
//!         global_color_table_size: 3,
//!     }
//! );
//!
//! #[derive(ByteStruct, PartialEq, Debug)]
//! #[byte_struct_le]
//! struct GIFLogicalScreenDescriptor {
//!     width: u16,
//!     height: u16,
//!     color_table_info: GIFColorTableInfo,
//!     background_color_index: u8,
//!     pixel_aspect_ratio: u8,
//! }
//!
//! fn example() {
//!     assert_eq!(GIFLogicalScreenDescriptor::BYTE_LEN, 7);
//!     let raw_descriptor = [0x03, 0x00, 0x05, 0x00, 0xF7, 0x00, 0x00];
//!     let descriptor = GIFLogicalScreenDescriptor::read_bytes(&raw_descriptor[..]);
//!     assert_eq!(descriptor, GIFLogicalScreenDescriptor{
//!         width: 3,
//!         height: 5,
//!         color_table_info: GIFColorTableInfo {
//!             global_color_table_flag: 1,
//!             color_resolution: 3,
//!             sort_flag: 1,
//!             global_color_table_size: 7,
//!         },
//!         background_color_index: 0,
//!         pixel_aspect_ratio: 0,
//!     });
//!     let mut raw_another = [0; GIFLogicalScreenDescriptor::BYTE_LEN];
//!     descriptor.write_bytes(&mut raw_another[..]);
//!     assert_eq!(raw_descriptor, raw_another);
//! }
//! ```

pub use byte_struct_derive::{ByteStruct, ByteStructBE, ByteStructLE};

/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
pub trait ByteStructLen {
    /// The length of the packed bytes of this type
    const BYTE_LEN: usize;
}

/// A data structure that can be packed into or unpacked from raw bytes.
///
/// This trait can be derived by
/// [`#[derive(ByteStruct)]`](https://docs.rs/byte_struct_derive/*/byte_struct_derive/derive.ByteStruct.html).
///
/// One can implement this trait for custom types in order to pack or unpack an object in a special way.
pub trait ByteStruct: ByteStructLen {
    /// Packs the struct into raw bytes and write to a slice
    fn write_bytes(&self, bytes: &mut [u8]);

    /// Unpacks raw bytes from a slice into a new struct
    fn read_bytes(bytes: &[u8]) -> Self;
}

/// A type that can be packed into or unpacked from raw bytes under given default byte order.
///
/// This trait is implemented for most numeric primitive types,
/// except for `bool`, `char`, `isize` and `usize`.
///
/// This is also implemented for array types whose element type implements `ByteStructUnspecifiedByteOrder`
/// and whose size is between 1 and 32 (inclusive).
///
/// This trait is automatically implemented for all types that implements [`ByteStruct`].
/// In this case, all members of `ByteStructUnspecifiedByteOrder` are direct wrappers of [`ByteStruct`] members.
///
/// Members in this trait are meant to be called by byte_struct internal only.
/// They do not do what one might expect:
/// the byte orders specified in `read_bytes_default_*` / `write_bytes_default_*` functions
/// are only **default byte orders**.
/// The default byte order is only respected when the type itself does not carry byte order specification
/// (e.g. primitive types).
/// In contrast, since [`ByteStruct`] types always have fixed packing method,
/// the default byte order has no effect on them, and the three versions of read / write functions for them,
/// `_default_le`, `_default_be` and no-spec from [`ByteStruct`], behave exactly the same.
///
/// One can implement this trait for custom types in order to pack or unpack an object in a special way,
/// but only when the said type changes its packing method depending on the default byte order.
/// An example for this is a custom fixed-size large integer type.
/// If the packing method is independent from the default byte order, please implement [`ByteStruct`] instead.
///
/// [`ByteStruct`]: trait.ByteStruct.html
pub trait ByteStructUnspecifiedByteOrder: ByteStructLen {
    /// Packs the object into raw bytes with little-endian as the default byte order
    fn write_bytes_default_le(&self, bytes: &mut [u8]);

    /// Unpacks raw bytes into a new object with little-endian as the default byte order
    fn read_bytes_default_le(bytes: &[u8]) -> Self;

    /// Packs the object into raw bytes with big-endian as the default byte order
    fn write_bytes_default_be(&self, bytes: &mut [u8]);

    /// Unpacks raw bytes into a new object with big-endian as the default byte order
    fn read_bytes_default_be(bytes: &[u8]) -> Self;
}

impl<T: ByteStruct> ByteStructUnspecifiedByteOrder for T {
    /// A wrapper of [`ByteStruct::write_bytes`](trait.ByteStruct.html#tymethod.write_bytes)
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        self.write_bytes(bytes);
    }

    /// A wrapper of [`ByteStruct::read_bytes`](trait.ByteStruct.html#tymethod.read_bytes)
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        Self::read_bytes(bytes)
    }

    /// A wrapper of [`ByteStruct::write_bytes`](trait.ByteStruct.html#tymethod.write_bytes)
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        self.write_bytes(bytes);
    }

    /// A wrapper of [`ByteStruct::read_bytes`](trait.ByteStruct.html#tymethod.read_bytes)
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        Self::read_bytes(bytes)
    }
}

impl ByteStructLen for u8 {
    const BYTE_LEN: usize = 1;
}

impl ByteStructUnspecifiedByteOrder for u8 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        u8::from_le_bytes([bytes[0]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        u8::from_be_bytes([bytes[0]])
    }
}

impl ByteStructLen for i8 {
    const BYTE_LEN: usize = 1;
}

impl ByteStructUnspecifiedByteOrder for i8 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        i8::from_le_bytes([bytes[0]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        i8::from_be_bytes([bytes[0]])
    }
}

impl ByteStructLen for u16 {
    const BYTE_LEN: usize = 2;
}

impl ByteStructUnspecifiedByteOrder for u16 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        u16::from_le_bytes([bytes[0], bytes[1]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        u16::from_be_bytes([bytes[0], bytes[1]])
    }
}

impl ByteStructLen for i16 {
    const BYTE_LEN: usize = 2;
}

impl ByteStructUnspecifiedByteOrder for i16 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        i16::from_le_bytes([bytes[0], bytes[1]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        i16::from_be_bytes([bytes[0], bytes[1]])
    }
}

impl ByteStructLen for u32 {
    const BYTE_LEN: usize = 4;
}

impl ByteStructUnspecifiedByteOrder for u32 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
}

impl ByteStructLen for i32 {
    const BYTE_LEN: usize = 4;
}

impl ByteStructUnspecifiedByteOrder for i32 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        i32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
}

impl ByteStructLen for u64 {
    const BYTE_LEN: usize = 8;
}

impl ByteStructUnspecifiedByteOrder for u64 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        u64::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]])
    }
}

impl ByteStructLen for i64 {
    const BYTE_LEN: usize = 8;
}

impl ByteStructUnspecifiedByteOrder for i64 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        i64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        i64::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]])
    }
}

impl ByteStructLen for u128 {
    const BYTE_LEN: usize = 16;
}

impl ByteStructUnspecifiedByteOrder for u128 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        u128::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7],
            bytes[8], bytes[9], bytes[10], bytes[11],
            bytes[12], bytes[13], bytes[14], bytes[15],
            ])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        u128::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7],
            bytes[8], bytes[9], bytes[10], bytes[11],
            bytes[12], bytes[13], bytes[14], bytes[15]])
    }
}

impl ByteStructLen for i128 {
    const BYTE_LEN: usize = 16;
}

impl ByteStructUnspecifiedByteOrder for i128 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        i128::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7],
            bytes[8], bytes[9], bytes[10], bytes[11],
            bytes[12], bytes[13], bytes[14], bytes[15]])
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        i128::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7],
            bytes[8], bytes[9], bytes[10], bytes[11],
            bytes[12], bytes[13], bytes[14], bytes[15]])
    }
}

impl ByteStructLen for f32 {
    const BYTE_LEN: usize = 4;
}

impl ByteStructUnspecifiedByteOrder for f32 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_bits().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        f32::from_bits(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_bits().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        f32::from_bits(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }
}

impl ByteStructLen for f64 {
    const BYTE_LEN: usize = 8;
}

impl ByteStructUnspecifiedByteOrder for f64 {
    fn write_bytes_default_le(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_bits().to_le_bytes()[..]);
    }
    fn read_bytes_default_le(bytes: &[u8]) -> Self {
        f64::from_bits(u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]]))
    }
    fn write_bytes_default_be(&self, bytes: &mut [u8]) {
        bytes.copy_from_slice(&self.clone().to_bits().to_be_bytes()[..]);
    }
    fn read_bytes_default_be(bytes: &[u8]) -> Self {
        f64::from_bits(u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]]))
    }
}

macro_rules! byte_struct_array {
    ($x:expr) => {
        impl<T: ByteStructLen> ByteStructLen for [T; $x] {
            const BYTE_LEN: usize = ($x) * T::BYTE_LEN;
        }

        impl<T: ByteStructUnspecifiedByteOrder> ByteStructUnspecifiedByteOrder for [T; $x] {
            fn write_bytes_default_le(&self, bytes: &mut [u8]) {
                let mut pos = 0;
                let len = T::BYTE_LEN;
                for i in 0 .. ($x) {
                    self[i].write_bytes_default_le(&mut bytes[pos .. pos + len]);
                    pos += len;
                }
            }
            fn read_bytes_default_le(bytes: &[u8]) -> Self {
                let mut pos = 0;
                let len = T::BYTE_LEN;
                let mut result: Self;
                unsafe {
                    result = std::mem::uninitialized();
                    for i in 0 .. ($x) {
                        std::ptr::write(&mut result[i], <T>::read_bytes_default_le(&bytes[pos .. pos + len]));
                        pos += len;
                    }
                }
                result
            }
            fn write_bytes_default_be(&self, bytes: &mut [u8]) {
                let mut pos = 0;
                let len = T::BYTE_LEN;
                for i in 0 .. ($x) {
                    self[i].write_bytes_default_be(&mut bytes[pos .. pos + len]);
                    pos += len;
                }
            }
            fn read_bytes_default_be(bytes: &[u8]) -> Self {
                let mut pos = 0;
                let len = T::BYTE_LEN;
                let mut result: Self;
                unsafe {
                    result = std::mem::uninitialized();
                    for i in 0 .. ($x) {
                        std::ptr::write(&mut result[i], <T>::read_bytes_default_be(&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);

/// Generates a structure that implements [`ByteStructUnspecifiedByteOrder`] with bit field semantics.
///
/// The bit fields are packed to / unpacked from the base integer type,
/// which is then packed / unpacked using the primitive type's [`ByteStructUnspecifiedByteOrder`] implementation.
/// Therefore, the byte order of bit fields is unspecified internally, and is only specified
/// by the parent structure that derives [`ByteStruct`](trait.ByteStruct.html), just like all primitive
/// types.
///
/// Note that the memory representation of the generated structure during runtime is NOT in bit field layout.
/// This macro only provides conversion method between the plain structure and the bit-field-packed bytes.
///
/// [`ByteStructUnspecifiedByteOrder`]: trait.ByteStructUnspecifiedByteOrder.html
///
/// # Example
/// ```
/// bitfields!(
///     // Specifies the struct name and the base type.
///     // The base type must be one of unsigned integer types.
///     // Attributes and visibility specifier can be attached before the struct name.
///     #[derive(PartialEq, Debug)]
///     SampleBitField: u16 {
///         // Specifies members and bit length from the least significant bit to the most.
///         // The bit layout is assumed packed, and paddings must be explicitly specified.
///         // The sum of bit length of all fields must equal the bit length of the base type.
///         // Attributes and visibility specifier can be attached before the field name.
///
///         // This creates bit field structure in the following layout:
///         //
///         // | MSB                                                        LSB |
///         // | 15| 14| 13| 12| 11| 10| 9 | 8 | 7 |  6 | 5 | 4 | 3 | 2 | 1 | 0 |
///         // |     z     |pad|               y                |       x       |
///         //
///         pub x: 4,
///         pub y: 8,
///         padding: 1,
///         pub z: 3,
///     }
/// );
///
/// // The macro above generates the structure below.
///
/// #[derive(PartialEq, Debug)]
/// struct SampleBitField {
///     pub x: u16,
///     pub y: u16,
///     padding: u16,
///     pub z: u16,
/// }
///
/// impl ByteStructUnspecifiedByteOrder for SampleBitField {
///     ...
/// }
/// ```
#[macro_export]
macro_rules! bitfields{
    (
        $(#[$outer:meta])*
        $visibility:vis $name:ident : $base:ty {
            $(
                $(#[$inner:ident $($args:tt)*])*
                $field_vis:vis $field_name:ident : $field_len:expr
            ),+ $(,)?
        }
    ) => {
        $(#[$outer])*
        $visibility struct $name {
            $(
                $(#[$inner $($args)*])*
                $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 ByteStructLen for $name {
            const BYTE_LEN: usize = <$base>::BYTE_LEN;
        }

        impl ByteStructUnspecifiedByteOrder for $name {
            fn write_bytes_default_le(&self, bytes: &mut [u8]) {
                self.to_raw().write_bytes_default_le(bytes);
            }
            fn read_bytes_default_le(bytes: &[u8]) -> Self {
                <$name>::from_raw(<$base>::read_bytes_default_le(bytes))
            }
            fn write_bytes_default_be(&self, bytes: &mut [u8]) {
                self.to_raw().write_bytes_default_be(bytes);
            }
            fn read_bytes_default_be(bytes: &[u8]) -> Self {
                <$name>::from_raw(<$base>::read_bytes_default_be(bytes))
            }
        }
    }
}