rmp/decode/uint.rs
1use std::io::Read;
2
3use Marker;
4use super::{read_marker, read_data_u8, read_data_u16, read_data_u32, read_data_u64, ValueReadError};
5
6/// Attempts to read a single byte from the given reader and to decode it as a positive fixnum
7/// value.
8///
9/// According to the MessagePack specification, a positive fixed integer value is represented using
10/// a single byte in `[0x00; 0x7f]` range inclusively, prepended with a special marker mask.
11///
12/// # Errors
13///
14/// This function will return `ValueReadError` on any I/O error while reading the marker,
15/// except the EINTR, which is handled internally.
16///
17/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
18/// expected one, indicating you with the actual type.
19///
20/// # Note
21///
22/// This function will silently retry on every EINTR received from the underlying `Read` until
23/// successful read.
24pub fn read_pfix<R: Read>(rd: &mut R) -> Result<u8, ValueReadError> {
25 match try!(read_marker(rd)) {
26 Marker::FixPos(val) => Ok(val),
27 marker => Err(ValueReadError::TypeMismatch(marker)),
28 }
29}
30
31/// Attempts to read exactly 2 bytes from the given reader and to decode them as `u8` value.
32///
33/// The first byte should be the marker and the second one should represent the data itself.
34///
35/// # Errors
36///
37/// This function will return `ValueReadError` on any I/O error while reading either the marker or
38/// the data.
39///
40/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
41/// expected one, indicating you with the actual type.
42pub fn read_u8<R: Read>(rd: &mut R) -> Result<u8, ValueReadError> {
43 match try!(read_marker(rd)) {
44 Marker::U8 => read_data_u8(rd),
45 marker => Err(ValueReadError::TypeMismatch(marker)),
46 }
47}
48
49/// Attempts to read exactly 3 bytes from the given reader and to decode them as `u16` value.
50///
51/// The first byte should be the marker and the others should represent the data itself.
52///
53/// # Errors
54///
55/// This function will return `ValueReadError` on any I/O error while reading either the marker or
56/// the data.
57///
58/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
59/// expected one, indicating you with the actual type.
60///
61/// # Note
62///
63/// This function will silently retry on every EINTR received from the underlying `Read` until
64/// successful read.
65pub fn read_u16<R: Read>(rd: &mut R) -> Result<u16, ValueReadError> {
66 match try!(read_marker(rd)) {
67 Marker::U16 => read_data_u16(rd),
68 marker => Err(ValueReadError::TypeMismatch(marker)),
69 }
70}
71
72/// Attempts to read exactly 5 bytes from the given reader and to decode them as `u32` value.
73///
74/// The first byte should be the marker and the others should represent the data itself.
75///
76/// # Errors
77///
78/// This function will return `ValueReadError` on any I/O error while reading either the marker or
79/// the data.
80///
81/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
82/// expected one, indicating you with the actual type.
83///
84/// # Note
85///
86/// This function will silently retry on every EINTR received from the underlying `Read` until
87/// successful read.
88pub fn read_u32<R: Read>(rd: &mut R) -> Result<u32, ValueReadError> {
89 match try!(read_marker(rd)) {
90 Marker::U32 => read_data_u32(rd),
91 marker => Err(ValueReadError::TypeMismatch(marker)),
92 }
93}
94
95/// Attempts to read exactly 9 bytes from the given reader and to decode them as `u64` value.
96///
97/// The first byte should be the marker and the others should represent the data itself.
98///
99/// # Errors
100///
101/// This function will return `ValueReadError` on any I/O error while reading either the marker or
102/// the data.
103///
104/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
105/// expected one, indicating you with the actual type.
106///
107/// # Note
108///
109/// This function will silently retry on every EINTR received from the underlying `Read` until
110/// successful read.
111pub fn read_u64<R: Read>(rd: &mut R) -> Result<u64, ValueReadError> {
112 match try!(read_marker(rd)) {
113 Marker::U64 => read_data_u64(rd),
114 marker => Err(ValueReadError::TypeMismatch(marker)),
115 }
116}