summaryrefslogtreecommitdiff
path: root/rust/zerocopy/benches/formats/coco_static_size.rs
blob: 0839497e174858c1e1e5c0ba10dd02950e17eb94 (plain)
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
use zerocopy_derive::*;

// The only valid value of this type are the bytes `0xC0C0`.
#[derive(TryFromBytes, KnownLayout, Immutable, IntoBytes)]
#[repr(u16)]
pub enum C0C0 {
    _XC0C0 = 0xC0C0,
}

macro_rules! define_packet {
    ($name: ident, $trait: ident, $leading_field: ty) => {
        #[derive($trait, KnownLayout, Immutable, IntoBytes)]
        #[repr(C, align(2))]
        pub struct $name {
            magic_number: $leading_field,
            mug_size: u8,
            temperature: u8,
            marshmallows: [u8; 2],
        }
    };
}

/// Packet begins with bytes 0xC0C0.
define_packet!(CocoPacket, TryFromBytes, C0C0);

/// Packet begins with any two bytes.
define_packet!(LocoPacket, FromBytes, [u8; 2]);