pub fn f32<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f32, E>where
I: StreamIsPartial + Stream<Token = u8>,
<I as Stream>::Slice: AsBytes,
Expand description
Recognizes a 4 byte floating point number
If the parameter is winnow::number::Endianness::Big
, parse a big endian f32 float,
otherwise if winnow::number::Endianness::Little
parse a little endian f32 float.
Complete version: returns an error if there is not enough input data
Partial version: Will return Err(winnow::error::ErrMode::Incomplete(_))
if there is not enough data.
Example
use winnow::number::f32;
let be_f32 = |s| {
f32(winnow::number::Endianness::Big).parse_next(s)
};
assert_eq!(be_f32(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert_eq!(be_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
let le_f32 = |s| {
f32(winnow::number::Endianness::Little).parse_next(s)
};
assert_eq!(le_f32(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5)));
assert_eq!(le_f32(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
use winnow::number::f32;
let be_f32 = |s| {
f32::<_, Error<_>>(winnow::number::Endianness::Big).parse_next(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::<_, Error<_>>(winnow::number::Endianness::Little).parse_next(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))));