rmp/decode/
dec.rs

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