pub fn f64<I, E: ParseError<I>>(endian: Endianness) -> impl Parser<I, f64, E>where
I: StreamIsPartial + Stream<Token = u8>,
<I as Stream>::Slice: AsBytes,
Expand description
Recognizes an 8 byte floating point number
If the parameter is winnow::number::Endianness::Big
, parse a big endian f64 float,
otherwise if winnow::number::Endianness::Little
parse a little endian f64 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::f64;
let be_f64 = |s| {
f64(winnow::number::Endianness::Big).parse_next(s)
};
assert_eq!(be_f64(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert_eq!(be_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
let le_f64 = |s| {
f64(winnow::number::Endianness::Little).parse_next(s)
};
assert_eq!(le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
assert_eq!(le_f64(&b"abc"[..]), Err(ErrMode::Backtrack(Error::new(&b"abc"[..], ErrorKind::Slice))));
use winnow::number::f64;
let be_f64 = |s| {
f64::<_, Error<_>>(winnow::number::Endianness::Big).parse_next(s)
};
assert_eq!(be_f64(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(be_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));
let le_f64 = |s| {
f64::<_, Error<_>>(winnow::number::Endianness::Little).parse_next(s)
};
assert_eq!(le_f64(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(le_f64(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));