winnow::binary

Function f32

Source
pub fn f32<Input, Error>(endian: Endianness) -> impl Parser<Input, f32, Error>
where Input: StreamIsPartial + Stream<Token = u8>, Error: ParserError<Input>,
Expand description

Recognizes a 4 byte floating point number

If the parameter is winnow::binary::Endianness::Big, parse a big endian f32 float, otherwise if winnow::binary::Endianness::Little parse a little endian f32 float.

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::f32;

let be_f32 = |s| {
    f32(winnow::binary::Endianness::Big).parse_peek(s)
};

assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert_eq!(be_f32(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice))));

let le_f32 = |s| {
    f32(winnow::binary::Endianness::Little).parse_peek(s)
};

assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
assert_eq!(le_f32(&b"abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"abc"[..], ErrorKind::Slice))));
use winnow::binary::f32;

let be_f32 = |s| {
    f32::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(s)
};

assert_eq!(be_f32(Partial::new(&[0x41, 0x48, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(be_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1))));

let le_f32 = |s| {
    f32::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(s)
};

assert_eq!(le_f32(Partial::new(&[0x00, 0x00, 0x48, 0x41][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(le_f32(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1))));