Function winnow::binary::f64

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

Recognizes an 8 byte floating point number

If the parameter is winnow::binary::Endianness::Big, parse a big endian f64 float, otherwise if winnow::binary::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::binary::f64;

let be_f64 = |s| {
    f64(winnow::binary::Endianness::Big).parse_peek(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(InputError::new(&b"abc"[..], ErrorKind::Slice))));

let le_f64 = |s| {
    f64(winnow::binary::Endianness::Little).parse_peek(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(InputError::new(&b"abc"[..], ErrorKind::Slice))));
use winnow::binary::f64;

let be_f64 = |s| {
    f64::<_, InputError<_>>(winnow::binary::Endianness::Big).parse_peek(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::<_, InputError<_>>(winnow::binary::Endianness::Little).parse_peek(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))));