|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -#![allow(dead_code)] |
19 | | - |
20 | 18 | use std::io; |
21 | 19 | use std::io::Cursor; |
22 | 20 | use std::io::Read; |
23 | 21 |
|
24 | | -pub(crate) struct SketchBytes { |
25 | | - bytes: Vec<u8>, |
26 | | -} |
27 | | - |
28 | | -impl SketchBytes { |
29 | | - pub fn with_capacity(capacity: usize) -> Self { |
30 | | - Self { |
31 | | - bytes: Vec::with_capacity(capacity), |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - pub fn into_bytes(self) -> Vec<u8> { |
36 | | - self.bytes |
37 | | - } |
38 | | - |
39 | | - pub fn write(&mut self, buf: &[u8]) { |
40 | | - self.bytes.extend_from_slice(buf); |
41 | | - } |
42 | | - |
43 | | - pub fn write_u8(&mut self, n: u8) { |
44 | | - self.bytes.push(n); |
45 | | - } |
46 | | - |
47 | | - pub fn write_i8(&mut self, n: i8) { |
48 | | - self.bytes.push(n as u8); |
49 | | - } |
50 | | - |
51 | | - pub fn write_u16_le(&mut self, n: u16) { |
52 | | - self.write(&n.to_le_bytes()); |
53 | | - } |
54 | | - |
55 | | - pub fn write_u16_be(&mut self, n: u16) { |
56 | | - self.write(&n.to_be_bytes()); |
57 | | - } |
58 | | - |
59 | | - pub fn write_i16_le(&mut self, n: i16) { |
60 | | - self.write(&n.to_le_bytes()); |
61 | | - } |
62 | | - |
63 | | - pub fn write_i16_be(&mut self, n: i16) { |
64 | | - self.write(&n.to_be_bytes()); |
65 | | - } |
66 | | - |
67 | | - pub fn write_u32_le(&mut self, n: u32) { |
68 | | - self.write(&n.to_le_bytes()); |
69 | | - } |
70 | | - |
71 | | - pub fn write_u32_be(&mut self, n: u32) { |
72 | | - self.write(&n.to_be_bytes()); |
73 | | - } |
74 | | - |
75 | | - pub fn write_i32_le(&mut self, n: i32) { |
76 | | - self.write(&n.to_le_bytes()); |
77 | | - } |
78 | | - |
79 | | - pub fn write_i32_be(&mut self, n: i32) { |
80 | | - self.write(&n.to_be_bytes()); |
81 | | - } |
82 | | - |
83 | | - pub fn write_u64_le(&mut self, n: u64) { |
84 | | - self.write(&n.to_le_bytes()); |
85 | | - } |
86 | | - |
87 | | - pub fn write_u64_be(&mut self, n: u64) { |
88 | | - self.write(&n.to_be_bytes()); |
89 | | - } |
90 | | - |
91 | | - pub fn write_i64_le(&mut self, n: i64) { |
92 | | - self.write(&n.to_le_bytes()); |
93 | | - } |
94 | | - |
95 | | - pub fn write_i64_be(&mut self, n: i64) { |
96 | | - self.write(&n.to_be_bytes()); |
97 | | - } |
98 | | - |
99 | | - pub fn write_f32_le(&mut self, n: f32) { |
100 | | - self.write(&n.to_le_bytes()); |
101 | | - } |
102 | | - |
103 | | - pub fn write_f32_be(&mut self, n: f32) { |
104 | | - self.write(&n.to_be_bytes()); |
105 | | - } |
106 | | - |
107 | | - pub fn write_f64_le(&mut self, n: f64) { |
108 | | - self.write(&n.to_le_bytes()); |
109 | | - } |
110 | | - |
111 | | - pub fn write_f64_be(&mut self, n: f64) { |
112 | | - self.write(&n.to_be_bytes()); |
113 | | - } |
114 | | -} |
115 | | - |
116 | | -pub(crate) struct SketchSlice<'a> { |
| 22 | +/// A wrapper around a byte slice that provides methods for reading various types of data from it. |
| 23 | +pub struct SketchSlice<'a> { |
117 | 24 | slice: Cursor<&'a [u8]>, |
118 | 25 | } |
119 | 26 |
|
120 | 27 | impl SketchSlice<'_> { |
| 28 | + /// Creates a new `SketchSlice` from the given byte slice. |
121 | 29 | pub fn new(slice: &[u8]) -> SketchSlice<'_> { |
122 | 30 | SketchSlice { |
123 | 31 | slice: Cursor::new(slice), |
124 | 32 | } |
125 | 33 | } |
126 | 34 |
|
| 35 | + /// Advances the position of the slice by `n` bytes. |
127 | 36 | pub fn advance(&mut self, n: u64) { |
128 | 37 | let pos = self.slice.position(); |
129 | 38 | self.slice.set_position(pos + n); |
130 | 39 | } |
131 | 40 |
|
| 41 | + /// Reads exactly `buf.len()` bytes from the slice into `buf`. |
132 | 42 | pub fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { |
133 | 43 | self.slice.read_exact(buf) |
134 | 44 | } |
135 | 45 |
|
| 46 | + /// Reads a single byte from the slice and returns it as a `u8`. |
136 | 47 | pub fn read_u8(&mut self) -> io::Result<u8> { |
137 | 48 | let mut buf = [0u8; 1]; |
138 | 49 | self.read_exact(&mut buf)?; |
139 | 50 | Ok(buf[0]) |
140 | 51 | } |
141 | 52 |
|
| 53 | + /// Reads a single byte from the slice and returns it as an `i8`. |
142 | 54 | pub fn read_i8(&mut self) -> io::Result<i8> { |
143 | 55 | let mut buf = [0u8; 1]; |
144 | 56 | self.read_exact(&mut buf)?; |
145 | 57 | Ok(buf[0] as i8) |
146 | 58 | } |
147 | 59 |
|
| 60 | + /// Reads a 16-bit unsigned integer from the slice in little-endian byte order. |
148 | 61 | pub fn read_u16_le(&mut self) -> io::Result<u16> { |
149 | 62 | let mut buf = [0u8; 2]; |
150 | 63 | self.read_exact(&mut buf)?; |
151 | 64 | Ok(u16::from_le_bytes(buf)) |
152 | 65 | } |
153 | 66 |
|
| 67 | + /// Reads a 16-bit unsigned integer from the slice in big-endian byte order. |
154 | 68 | pub fn read_u16_be(&mut self) -> io::Result<u16> { |
155 | 69 | let mut buf = [0u8; 2]; |
156 | 70 | self.read_exact(&mut buf)?; |
157 | 71 | Ok(u16::from_be_bytes(buf)) |
158 | 72 | } |
159 | 73 |
|
| 74 | + /// Reads a 16-bit signed integer from the slice in little-endian byte order. |
160 | 75 | pub fn read_i16_le(&mut self) -> io::Result<i16> { |
161 | 76 | let mut buf = [0u8; 2]; |
162 | 77 | self.read_exact(&mut buf)?; |
163 | 78 | Ok(i16::from_le_bytes(buf)) |
164 | 79 | } |
165 | 80 |
|
| 81 | + /// Reads a 16-bit signed integer from the slice in big-endian byte order. |
166 | 82 | pub fn read_i16_be(&mut self) -> io::Result<i16> { |
167 | 83 | let mut buf = [0u8; 2]; |
168 | 84 | self.read_exact(&mut buf)?; |
169 | 85 | Ok(i16::from_be_bytes(buf)) |
170 | 86 | } |
171 | 87 |
|
| 88 | + /// Reads a 32-bit unsigned integer from the slice in little-endian byte order. |
172 | 89 | pub fn read_u32_le(&mut self) -> io::Result<u32> { |
173 | 90 | let mut buf = [0u8; 4]; |
174 | 91 | self.read_exact(&mut buf)?; |
175 | 92 | Ok(u32::from_le_bytes(buf)) |
176 | 93 | } |
177 | 94 |
|
| 95 | + /// Reads a 32-bit unsigned integer from the slice in big-endian byte order. |
178 | 96 | pub fn read_u32_be(&mut self) -> io::Result<u32> { |
179 | 97 | let mut buf = [0u8; 4]; |
180 | 98 | self.read_exact(&mut buf)?; |
181 | 99 | Ok(u32::from_be_bytes(buf)) |
182 | 100 | } |
183 | 101 |
|
| 102 | + /// Reads a 32-bit signed integer from the slice in little-endian byte order. |
184 | 103 | pub fn read_i32_le(&mut self) -> io::Result<i32> { |
185 | 104 | let mut buf = [0u8; 4]; |
186 | 105 | self.read_exact(&mut buf)?; |
187 | 106 | Ok(i32::from_le_bytes(buf)) |
188 | 107 | } |
189 | 108 |
|
| 109 | + /// Reads a 32-bit signed integer from the slice in big-endian byte order. |
190 | 110 | pub fn read_i32_be(&mut self) -> io::Result<i32> { |
191 | 111 | let mut buf = [0u8; 4]; |
192 | 112 | self.read_exact(&mut buf)?; |
193 | 113 | Ok(i32::from_be_bytes(buf)) |
194 | 114 | } |
195 | 115 |
|
| 116 | + /// Reads a 16-bit unsigned integer from the slice in little-endian byte order. |
196 | 117 | pub fn read_u64_le(&mut self) -> io::Result<u64> { |
197 | 118 | let mut buf = [0u8; 8]; |
198 | 119 | self.read_exact(&mut buf)?; |
199 | 120 | Ok(u64::from_le_bytes(buf)) |
200 | 121 | } |
201 | 122 |
|
| 123 | + /// Reads a 16-bit unsigned integer from the slice in big-endian byte order. |
202 | 124 | pub fn read_u64_be(&mut self) -> io::Result<u64> { |
203 | 125 | let mut buf = [0u8; 8]; |
204 | 126 | self.read_exact(&mut buf)?; |
205 | 127 | Ok(u64::from_be_bytes(buf)) |
206 | 128 | } |
207 | 129 |
|
| 130 | + /// Reads a 16-bit signed integer from the slice in little-endian byte order. |
208 | 131 | pub fn read_i64_le(&mut self) -> io::Result<i64> { |
209 | 132 | let mut buf = [0u8; 8]; |
210 | 133 | self.read_exact(&mut buf)?; |
211 | 134 | Ok(i64::from_le_bytes(buf)) |
212 | 135 | } |
213 | 136 |
|
| 137 | + /// Reads a 16-bit signed integer from the slice in big-endian byte order. |
214 | 138 | pub fn read_i64_be(&mut self) -> io::Result<i64> { |
215 | 139 | let mut buf = [0u8; 8]; |
216 | 140 | self.read_exact(&mut buf)?; |
217 | 141 | Ok(i64::from_be_bytes(buf)) |
218 | 142 | } |
219 | 143 |
|
| 144 | + /// Reads a 32-bit floating-point number from the slice in little-endian byte order. |
220 | 145 | pub fn read_f32_le(&mut self) -> io::Result<f32> { |
221 | 146 | let mut buf = [0u8; 4]; |
222 | 147 | self.read_exact(&mut buf)?; |
223 | 148 | Ok(f32::from_le_bytes(buf)) |
224 | 149 | } |
225 | 150 |
|
| 151 | + /// Reads a 32-bit floating-point number from the slice in big-endian byte order. |
226 | 152 | pub fn read_f32_be(&mut self) -> io::Result<f32> { |
227 | 153 | let mut buf = [0u8; 4]; |
228 | 154 | self.read_exact(&mut buf)?; |
229 | 155 | Ok(f32::from_be_bytes(buf)) |
230 | 156 | } |
231 | 157 |
|
| 158 | + /// Reads a 64-bit floating-point number from the slice in little-endian byte order. |
232 | 159 | pub fn read_f64_le(&mut self) -> io::Result<f64> { |
233 | 160 | let mut buf = [0u8; 8]; |
234 | 161 | self.read_exact(&mut buf)?; |
235 | 162 | Ok(f64::from_le_bytes(buf)) |
236 | 163 | } |
237 | 164 |
|
| 165 | + /// Reads a 64-bit floating-point number from the slice in big-endian byte order. |
238 | 166 | pub fn read_f64_be(&mut self) -> io::Result<f64> { |
239 | 167 | let mut buf = [0u8; 8]; |
240 | 168 | self.read_exact(&mut buf)?; |
|
0 commit comments