use puns::{
Pun, Tag,
num::{u8c, u16be},
};
#[derive(Pun, Clone, Copy)]
#[repr(transparent)]
struct Protocol(u8);
#[derive(Pun, Clone, Copy)]
#[repr(C)]
#[pun(tag(Protocol = u8c<17>))]
struct Udp {
ports: [u8; 4],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = 4 + length)]
#[pun(assert(sum16(0, size_of_val(self)) == 0xffff))]
struct Packet<P: ?Sized + Tag<Protocol> = [u8]> {
protocol: P::Tag,
length: u8,
checksum: u16be,
#[pun(regions(payload: P))]
data: [u8],
}
#[derive(Pun, Clone, Copy)]
#[repr(C)]
#[pun(assert(sum16(0, size_of_val(self)) == 0xffff))]
struct FixedUdp {
protocol: u8c<17>,
length: u8,
checksum: u16be,
payload: Udp,
}
#[derive(Pun, Clone, Copy)]
#[repr(C)]
#[pun(assert(sum16(0, size_of_val(self)) == 0xffff))]
struct Fixed {
protocol: Protocol,
length: u8,
checksum: u16be,
payload: [u8; 4],
}
fn main() {
let bytes = [17u8, 4, 0xee, 0xfb, 0, 0, 0, 0];
let udp: &Packet<Udp> = puns::try_pun_ref(&bytes[..]).unwrap();
let any: &Packet = puns::pun_ref(udp);
assert!(any.payload().is_ok());
let fixed: FixedUdp = puns::try_pun(bytes).unwrap();
let _: Fixed = puns::pun(fixed);
let mut both = [fixed, fixed];
let _: &[Fixed; 2] = puns::pun_ref(&both);
let _: &Fixed = puns::tail_ref(&both);
let _: &mut FixedUdp = puns::head_mut(&mut both);
}