[][src]Macro rcodec::hcodec

macro_rules! hcodec {
    {} => { ... };
    { { $($head:tt)+ } } => { ... };
    { { $($head:tt)+ } :: $($tail:tt)+ } => { ... };
    { { $($head:tt)+ } >> $($tail:tt)+ } => { ... };
    { { $($head:tt)+ } >>= |$v:ident| $fnbody:block } => { ... };
}

Converts an HList of Codecs into a Codec that operates on an HList of values.

Note that we require braces around each element so that we have more freedom with operators. Rust macro rules state that simple exprs (without the braces) can only be followed by => , ; whereas blocks (with the braces) can be followed by any token like >> or ::.

Examples

use pl_hlist::*;
use rcodec::{byte_vector, hcodec};
use rcodec::codec::*;

let c = byte_vector!(0xCA, 0xFE);
let codec = hcodec!(
    { "magic"  => constant(&c) } >>
    { "field1" => uint8        } ::
    { "field2" => uint8        }
);

let bytes = byte_vector!(0xCA, 0xFE, 0x01, 0x02);
let decoded = codec.decode(&bytes).unwrap().value;
assert_eq!(decoded, hlist!(1, 2));