Function combine::parser::range::length_prefix[][src]

pub fn length_prefix<Input, P>(len: P) -> length_prefix<Input, P> where
    <Input as StreamOnce>::Error: ParseError<<Input as StreamOnce>::Token, <Input as StreamOnce>::Range, <Input as StreamOnce>::Position>,
    Input: Stream,
    Input: RangeStream,
    P: Parser<Input>,
    usize: TryFrom<P::Output>,
    <usize as TryFrom<P::Output>>::Error: StdError + Send + Sync + 'static, 
Expand description

Takes a parser which parses a length then extracts a range of that length and returns it. Commonly used in binary formats

let mut input = Vec::new();
input.extend_from_slice(&3u16.to_be_bytes());
input.extend_from_slice(b"1234");

let mut parser = length_prefix(be_u16());
let result = parser.parse(&input[..]);
assert_eq!(result, Ok((&b"123"[..], &b"4"[..])));