rmp/decode/
dec.rs

1use super::{read_marker, RmpRead, ValueReadError};
2use crate::Marker;
3
4/// Attempts to read exactly 5 bytes from the given reader and to decode them as `f32` value.
5///
6/// The first byte should be the marker and the others should represent the data itself.
7///
8/// # Errors
9///
10/// This function will return `ValueReadError` on any I/O error while reading either the marker or
11/// the data.
12///
13/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
14/// expected one, indicating you with the actual type.
15///
16/// # Note
17///
18/// This function will silently retry on every EINTR received from the underlying `Read` until
19/// successful read.
20pub fn read_f32<R: RmpRead>(rd: &mut R) -> Result<f32, ValueReadError<R::Error>> {
21    match read_marker(rd)? {
22        Marker::F32 => Ok(rd.read_data_f32()?),
23        marker => Err(ValueReadError::TypeMismatch(marker)),
24    }
25}
26
27/// Attempts to read exactly 9 bytes from the given reader and to decode them as `f64` value.
28///
29/// The first byte should be the marker and the others should represent the data itself.
30///
31/// # Errors
32///
33/// This function will return `ValueReadError` on any I/O error while reading either the marker or
34/// the data.
35///
36/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
37/// expected one, indicating you with the actual type.
38///
39/// # Note
40///
41/// This function will silently retry on every EINTR received from the underlying `Read` until
42/// successful read.
43pub fn read_f64<R: RmpRead>(rd: &mut R) -> Result<f64, ValueReadError<R::Error>> {
44    match read_marker(rd)? {
45        Marker::F64 => Ok(rd.read_data_f64()?),
46        marker => Err(ValueReadError::TypeMismatch(marker)),
47    }
48}