rmp/decode/
sint.rs

1use std::io::Read;
2
3use Marker;
4use super::{read_marker, read_data_i8, read_data_i16, read_data_i32, read_data_i64, ValueReadError};
5
6/// Attempts to read a single byte from the given reader and to decode it as a negative fixnum
7/// value.
8///
9/// According to the MessagePack specification, a negative fixed integer value is represented using
10/// a single byte in `[0xe0; 0xff]` 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_nfix<R: Read>(rd: &mut R) -> Result<i8, ValueReadError> {
25    match try!(read_marker(rd)) {
26        Marker::FixNeg(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 `i8` 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.
42///
43/// # Note
44///
45/// This function will silently retry on every EINTR received from the underlying `Read` until
46/// successful read.
47pub fn read_i8<R: Read>(rd: &mut R) -> Result<i8, ValueReadError> {
48    match try!(read_marker(rd)) {
49        Marker::I8 => read_data_i8(rd),
50        marker => Err(ValueReadError::TypeMismatch(marker)),
51    }
52}
53
54/// Attempts to read exactly 3 bytes from the given reader and to decode them as `i16` value.
55///
56/// The first byte should be the marker and the others should represent the data itself.
57///
58/// # Errors
59///
60/// This function will return `ValueReadError` on any I/O error while reading either the marker or
61/// the data.
62///
63/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
64/// expected one, indicating you with the actual type.
65///
66/// # Note
67///
68/// This function will silently retry on every EINTR received from the underlying `Read` until
69/// successful read.
70pub fn read_i16<R: Read>(rd: &mut R) -> Result<i16, ValueReadError> {
71    match try!(read_marker(rd)) {
72        Marker::I16 => read_data_i16(rd),
73        marker => Err(ValueReadError::TypeMismatch(marker)),
74    }
75}
76
77/// Attempts to read exactly 5 bytes from the given reader and to decode them as `i32` value.
78///
79/// The first byte should be the marker and the others should represent the data itself.
80///
81/// # Errors
82///
83/// This function will return `ValueReadError` on any I/O error while reading either the marker or
84/// the data.
85///
86/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
87/// expected one, indicating you with the actual type.
88///
89/// # Note
90///
91/// This function will silently retry on every EINTR received from the underlying `Read` until
92/// successful read.
93pub fn read_i32<R: Read>(rd: &mut R) -> Result<i32, ValueReadError> {
94    match try!(read_marker(rd)) {
95        Marker::I32 => read_data_i32(rd),
96        marker => Err(ValueReadError::TypeMismatch(marker)),
97    }
98}
99
100/// Attempts to read exactly 9 bytes from the given reader and to decode them as `i64` value.
101///
102/// The first byte should be the marker and the others should represent the data itself.
103///
104/// # Errors
105///
106/// This function will return `ValueReadError` on any I/O error while reading either the marker or
107/// the data.
108///
109/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
110/// expected one, indicating you with the actual type.
111///
112/// # Note
113///
114/// This function will silently retry on every EINTR received from the underlying `Read` until
115/// successful read.
116pub fn read_i64<R: Read>(rd: &mut R) -> Result<i64, ValueReadError> {
117    match try!(read_marker(rd)) {
118        Marker::I64 => read_data_i64(rd),
119        marker => Err(ValueReadError::TypeMismatch(marker)),
120    }
121}