1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use core::fmt::{Display, Formatter};
use crate::decode::RmpReadErr;
use super::RmpRead;
#[derive(Debug)]
#[non_exhaustive]
pub enum BytesReadError {
InsufficientBytes {
expected: usize,
actual: usize,
position: u64
}
}
impl Display for BytesReadError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match *self {
BytesReadError::InsufficientBytes { expected, actual, position } => {
write!(f, "Expected at least bytes {}, but only got {} (pos {})", expected, actual, position)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for BytesReadError {}
impl RmpReadErr for BytesReadError {}
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Bytes<'a> {
current_position: u64,
bytes: &'a [u8],
}
impl<'a> Bytes<'a> {
#[inline]
pub fn new(bytes: &'a [u8]) -> Self {
Bytes { bytes, current_position: 0 }
}
#[inline]
pub fn remaining_slice(&self) -> &'a [u8] {
self.bytes
}
#[inline]
pub fn position(&self) -> u64 {
self.current_position
}
}
impl<'a> From<&'a [u8]> for Bytes<'a> {
#[inline]
fn from(bytes: &'a [u8]) -> Self {
Bytes { bytes, current_position: 0 }
}
}
impl RmpRead for Bytes<'_> {
type Error = BytesReadError;
#[inline]
fn read_u8(&mut self) -> Result<u8, Self::Error> {
if let Some((&first, newly_remaining)) = self.bytes.split_first() {
self.bytes = newly_remaining;
self.current_position += 1;
Ok(first)
} else {
Err(BytesReadError::InsufficientBytes {
expected: 1,
actual: 0,
position: self.current_position
})
}
}
#[inline]
fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
let to_read = buf.len();
if to_read <= self.bytes.len() {
let (src, newly_remaining) = self.bytes.split_at(to_read);
self.bytes = newly_remaining;
self.current_position += to_read as u64;
buf.copy_from_slice(src);
Ok(())
} else {
Err(BytesReadError::InsufficientBytes {
expected: to_read,
actual: self.bytes.len(),
position: self.current_position
})
}
}
}
#[cfg(not(feature = "std"))]
impl<'a> RmpRead for &'a [u8] {
type Error = BytesReadError;
fn read_u8(&mut self) -> Result<u8, Self::Error> {
if let Some((&first, newly_remaining)) = self.split_first() {
*self = newly_remaining;
Ok(first)
} else {
Err(BytesReadError::InsufficientBytes {
expected: 1,
actual: 0,
position: 0
})
}
}
fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
let to_read = buf.len();
if to_read <= self.len() {
let (src, newly_remaining) = self.split_at(to_read);
*self = newly_remaining;
buf.copy_from_slice(src);
Ok(())
} else {
Err(BytesReadError::InsufficientBytes {
expected: to_read,
actual: self.len(),
position: 0
})
}
}
}