[−][src]Macro rcodec::struct_codec
Shorthand for creating a Codec
for a struct.
The given struct must support HList
conversions, either by using the HListSupport
attribute
or by manually implementing the FromHList
and ToHList
traits.
Examples
use pl_hlist::*; use rcodec::{byte_vector, struct_codec}; use rcodec::codec::*; #[derive(Debug, PartialEq, Eq, HListSupport)] pub struct Header { foo: u8, bar: u32 } let magic = byte_vector!(0xCA, 0xFE); let header_codec = struct_codec!( Header from { "magic" => constant(&magic) } >> { "foo" => uint8 } :: { "junk" => ignore(2) } >> { "bar" => uint32 } ); let bytes = byte_vector!(0xCA, 0xFE, 0x07, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x06); let header = header_codec.decode(&bytes).unwrap().value; assert_eq!(header, Header { foo: 7, bar: 6 });