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