winnow::binary::bits

Function bytes

Source
pub fn bytes<Input, Output, ByteError, BitError, ParseNext>(
    parser: ParseNext,
) -> impl Parser<(Input, usize), Output, BitError>
where ByteError: ParserError<Input> + ErrorConvert<BitError>, BitError: ParserError<(Input, usize)>, Input: Stream<Token = u8> + Clone, ParseNext: Parser<Input, Output, ByteError>,
Expand description

Convert a bits stream back into a byte stream

Warning: A partial byte remaining in the input will be ignored and the given parser will start parsing at the next full byte.

use winnow::prelude::*;
use winnow::Bytes;
use winnow::binary::bits::{bits, bytes, take};
use winnow::combinator::rest;
use winnow::error::InputError;

type Stream<'i> = &'i Bytes;

fn stream(b: &[u8]) -> Stream<'_> {
    Bytes::new(b)
}

fn parse(input: Stream<'_>) -> IResult<Stream<'_>, (u8, u8, &[u8])> {
  bits::<_, _, InputError<(_, usize)>, _, _>((
    take(4usize),
    take(8usize),
    bytes::<_, _, InputError<_>, _, _>(rest)
  )).parse_peek(input)
}

let input = stream(&[0x12, 0x34, 0xff, 0xff]);

assert_eq!(parse(input), Ok(( stream(&[]), (0x01, 0x23, &[0xff, 0xff][..]) )));