Function winnow::bits::bytes

source ·
pub fn bytes<I, O, E1, E2, P>(
    parser: P
) -> impl FnMut((I, usize)) -> IResult<(I, usize), O, E2>where
    E1: ParseError<I> + ErrorConvert<E2>,
    E2: ParseError<(I, usize)>,
    I: Stream<Token = u8>,
    P: Parser<I, O, E1>,
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::bits::{bits, bytes, take};
use winnow::combinator::rest;
use winnow::error::Error;

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

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

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

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

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