Function 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][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

§Example

use winnow::binary::f64;

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

assert_eq!(be_f64.parse_peek(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5)));
assert!(be_f64.parse_peek(&b"abc"[..]).is_err());

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

assert_eq!(le_f64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5)));
assert!(le_f64.parse_peek(&b"abc"[..]).is_err());
use winnow::binary::f64;

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

assert_eq!(be_f64.parse_peek(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(be_f64.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));

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

assert_eq!(le_f64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..])), Ok((Partial::new(&b""[..]), 12.5)));
assert_eq!(le_f64.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5))));