winnow::binary

Function le_u16

Source
pub fn le_u16<Input, Error>(input: &mut Input) -> PResult<u16, Error>
where Input: StreamIsPartial + Stream<Token = u8>, Error: ParserError<Input>,
Expand description

Recognizes a little endian unsigned 2 bytes integer.

Complete version: Returns an error if there is not enough input data.

[Partial version][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

ยงExample

use winnow::binary::le_u16;

fn parser(s: &[u8]) -> IResult<&[u8], u16> {
      le_u16.parse_peek(s)
}

assert_eq!(parser(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
assert_eq!(parser(&b"\x01"[..]), Err(ErrMode::Backtrack(InputError::new(&[0x01][..], ErrorKind::Slice))));
use winnow::binary::le_u16;

fn parser(s: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> {
      le_u16::<_, InputError<_>>.parse_peek(s)
}

assert_eq!(parser(Partial::new(&b"\x00\x01abcd"[..])), Ok((Partial::new(&b"abcd"[..]), 0x0100)));
assert_eq!(parser(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));