Function winnow::binary::bits::bits

source ·
pub fn bits<Input, Output, BitError, ByteError, ParseNext>(
    parser: ParseNext,
) -> impl Parser<Input, Output, ByteError>
where BitError: ParserError<(Input, usize)> + ErrorConvert<ByteError>, ByteError: ParserError<Input>, (Input, usize): Stream, Input: Stream + Clone, ParseNext: Parser<(Input, usize), Output, BitError>,
Expand description

Converts a byte-level input to a bit-level input

See bytes to convert it back.

§Example

use winnow::prelude::*;
use winnow::Bytes;
use winnow::binary::bits::{bits, take};
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)> {
    bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input)
}

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

let output = parse(input).expect("We take 1.5 bytes and the input is longer than 2 bytes");

// The first byte is consumed, the second byte is partially consumed and dropped.
let remaining = output.0;
assert_eq!(remaining, stream(&[0xff, 0xff]));

let parsed = output.1;
assert_eq!(parsed.0, 0x01);
assert_eq!(parsed.1, 0x23);