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