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;

fn be_f32(input: &mut &[u8]) -> ModalResult<f32> {
    f32(winnow::binary::Endianness::Big).parse_next(input)
};

assert_eq!(be_f32.parse_peek(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert!(be_f32.parse_peek(&b"abc"[..]).is_err());

fn le_f32(input: &mut &[u8]) -> ModalResult<f32> {
    f32(winnow::binary::Endianness::Little).parse_next(input)
};

assert_eq!(le_f32.parse_peek(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
assert!(le_f32.parse_peek(&b"abc"[..]).is_err());
use winnow::binary::f32;

fn be_f32(input: &mut Partial<&[u8]>) -> ModalResult<f32> {
    f32(winnow::binary::Endianness::Big).parse_next(input)
};

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

fn le_f32(input: &mut Partial<&[u8]>) -> ModalResult<f32> {
    f32(winnow::binary::Endianness::Little).parse_next(input)
};

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