/rust/registry/src/index.crates.io-1949cf8c6b5b557f/storekey-0.11.0/src/reader.rs
Line | Count | Source |
1 | | use std::borrow::Cow; |
2 | | use std::io::BufRead; |
3 | | |
4 | | use super::DecodeError; |
5 | | use super::types::{EscapedSlice, EscapedStr}; |
6 | | |
7 | | /// Struct used in [`storekey::Decode`] for reading types from the buffer. |
8 | | /// |
9 | | /// This type handles unescaping bytes the buffer mostly transparently. |
10 | | /// It has an internal flag for marking when an escaped byte can be read from the buffer. |
11 | | /// Reading from the buffer in any way unmarks this flag. |
12 | | pub struct Reader<R> { |
13 | | inner: R, |
14 | | expect_escaped: bool, |
15 | | } |
16 | | |
17 | | macro_rules! impl_prims { |
18 | | (signed $ty:ident, $name:ident) => { |
19 | | #[inline] |
20 | 316 | pub fn $name(&mut self) -> Result<$ty, DecodeError> { |
21 | 316 | Ok($ty::from_be_bytes(self.read_array()?) ^ $ty::MIN) |
22 | 316 | } <storekey::reader::BorrowReader>::read_i64 Line | Count | Source | 20 | 316 | pub fn $name(&mut self) -> Result<$ty, DecodeError> { | 21 | 316 | Ok($ty::from_be_bytes(self.read_array()?) ^ $ty::MIN) | 22 | 316 | } |
Unexecuted instantiation: <storekey::reader::Reader<_>>::read_i8 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_i16 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_i32 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_i64 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_i128 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_i8 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_i16 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_i32 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_i64 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_i128 |
23 | | }; |
24 | | ($ty:ident, $name:ident) => { |
25 | | #[inline] |
26 | 23.2k | pub fn $name(&mut self) -> Result<$ty, DecodeError> { |
27 | 23.2k | Ok($ty::from_be_bytes(self.read_array()?)) |
28 | 23.2k | } <storekey::reader::BorrowReader>::read_u8 Line | Count | Source | 26 | 17.4k | pub fn $name(&mut self) -> Result<$ty, DecodeError> { | 27 | 17.4k | Ok($ty::from_be_bytes(self.read_array()?)) | 28 | 17.4k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u16 <storekey::reader::BorrowReader>::read_u32 Line | Count | Source | 26 | 5.80k | pub fn $name(&mut self) -> Result<$ty, DecodeError> { | 27 | 5.80k | Ok($ty::from_be_bytes(self.read_array()?)) | 28 | 5.80k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u64 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_u8 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_u16 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_u32 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_u64 Unexecuted instantiation: <storekey::reader::Reader<_>>::read_u128 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u8 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u16 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u32 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u64 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_u128 |
29 | | }; |
30 | | } |
31 | | |
32 | | impl<R: BufRead> Reader<R> { |
33 | | /// Create a new reader. |
34 | 0 | pub const fn new(r: R) -> Self { |
35 | 0 | Reader { |
36 | 0 | inner: r, |
37 | 0 | expect_escaped: false, |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | | /// Returns if the reader is empty / contains no more data. |
42 | | #[inline] |
43 | 0 | pub fn is_empty(&mut self) -> Result<bool, DecodeError> { |
44 | 0 | Ok(self.inner.fill_buf()?.is_empty()) |
45 | 0 | } |
46 | | |
47 | | /// Mark the next byte as possibly containing an escaped bytes. |
48 | | #[inline] |
49 | 0 | pub fn expect_escaped(&mut self) { |
50 | 0 | self.expect_escaped = true; |
51 | 0 | } |
52 | | |
53 | | /// Try to read a terminator byte if there is one. |
54 | | /// |
55 | | /// Returns true if the next byte is a terminator, otherwise returns false and the reader does |
56 | | /// not advance. |
57 | | /// |
58 | | /// Sets the `expect_escaped` flag marking the next byte as being possibly escaped. |
59 | | #[inline] |
60 | 0 | pub fn read_terminal(&mut self) -> Result<bool, DecodeError> { |
61 | 0 | self.expect_escaped = true; |
62 | 0 | let buf = self.inner.fill_buf()?; |
63 | 0 | match buf.first() { |
64 | | Some(0) => { |
65 | 0 | self.inner.consume(1); |
66 | 0 | Ok(true) |
67 | | } |
68 | 0 | Some(_) => Ok(false), |
69 | 0 | None => Err(DecodeError::UnexpectedEnd), |
70 | | } |
71 | 0 | } |
72 | | |
73 | | /// Reads an fixed size array of u8 from the reader, unescaping possible escaped bytes. |
74 | | /// |
75 | | /// All other `read_*` functions of `Reader` which read a fixed size type call this function to |
76 | | /// read a certain amount of bytes from the reader. |
77 | | /// |
78 | | /// This type does not expect a null terminator after the end of the array as it is reading a |
79 | | /// fixed size type. |
80 | | /// |
81 | | /// Calling this function unsets the expected escape flag before returning. |
82 | | #[inline] |
83 | 0 | pub fn read_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], DecodeError> { |
84 | | const { assert!(SIZE > 0, "read_array should at minimum read a single byte") }; |
85 | 0 | if self.expect_escaped { |
86 | 0 | self.expect_escaped = false; |
87 | 0 | let mut buffer = [0]; |
88 | 0 | self.inner.read_exact(&mut buffer[..])?; |
89 | 0 | if buffer[0] != 1 { |
90 | 0 | let mut res = [0u8; SIZE]; |
91 | 0 | self.inner.read_exact(&mut res[1..])?; |
92 | 0 | res[0] = buffer[0]; |
93 | 0 | return Ok(res); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | 0 | let mut res = [0u8; SIZE]; |
98 | 0 | self.inner.read_exact(&mut res[..])?; |
99 | 0 | Ok(res) |
100 | 0 | } |
101 | | |
102 | | /// Reads a runtime sized `Vec<u8>` from the reader, expected the sequence of bytes to be |
103 | | /// ended by a terminal zero byte. |
104 | | /// |
105 | | /// Calling this function unsets the expected escape flag before returning. |
106 | | #[inline] |
107 | 0 | pub fn read_vec(&mut self) -> Result<Vec<u8>, DecodeError> { |
108 | 0 | self.expect_escaped = false; |
109 | 0 | let mut buffer = Vec::new(); |
110 | | |
111 | 0 | let mut read_u8 = || -> Result<u8, DecodeError> { |
112 | 0 | let mut buffer = [0u8]; |
113 | 0 | if self.inner.read(&mut buffer)? == 0 { |
114 | 0 | return Err(DecodeError::UnexpectedEnd); |
115 | 0 | }; |
116 | 0 | Ok(buffer[0]) |
117 | 0 | }; |
118 | | |
119 | | loop { |
120 | 0 | let next = read_u8()?; |
121 | 0 | if next == 1 { |
122 | 0 | let next = read_u8()?; |
123 | 0 | buffer.push(next); |
124 | 0 | continue; |
125 | 0 | } |
126 | 0 | if next == 0 { |
127 | 0 | break; |
128 | 0 | } |
129 | 0 | buffer.push(next) |
130 | | } |
131 | 0 | Ok(buffer) |
132 | 0 | } |
133 | | |
134 | | /// Reads a runtime sized `String` from the reader, expected the sequence of bytes to be |
135 | | /// ended by a terminal zero byte. |
136 | | /// |
137 | | /// Calling this function unsets the expected escape flag before returning. |
138 | | #[inline] |
139 | 0 | pub fn read_string(&mut self) -> Result<String, DecodeError> { |
140 | 0 | let buf = self.read_vec()?; |
141 | 0 | String::from_utf8(buf).map_err(|_| DecodeError::Utf8) |
142 | 0 | } |
143 | | |
144 | | #[inline] |
145 | 0 | pub fn read_f32(&mut self) -> Result<f32, DecodeError> { |
146 | 0 | let v = self.read_u32()? as i32; |
147 | 0 | let t = ((v ^ i32::MIN) >> 31) | i32::MIN; |
148 | 0 | Ok(f32::from_bits((v ^ t) as u32)) |
149 | 0 | } |
150 | | |
151 | | #[inline] |
152 | 0 | pub fn read_f64(&mut self) -> Result<f64, DecodeError> { |
153 | 0 | let v = self.read_u64()? as i64; |
154 | 0 | let t = ((v ^ i64::MIN) >> 63) | i64::MIN; |
155 | 0 | Ok(f64::from_bits((v ^ t) as u64)) |
156 | 0 | } |
157 | | |
158 | | impl_prims! {signed i8, read_i8} |
159 | | impl_prims! {u8, read_u8} |
160 | | impl_prims! {signed i16,read_i16} |
161 | | impl_prims! {u16,read_u16} |
162 | | impl_prims! {signed i32,read_i32} |
163 | | impl_prims! {u32,read_u32} |
164 | | impl_prims! {signed i64,read_i64} |
165 | | impl_prims! {u64,read_u64} |
166 | | impl_prims! {signed i128,read_i128} |
167 | | impl_prims! {u128,read_u128} |
168 | | } |
169 | | |
170 | | /// Struct used in [`storekey::BorrowDecode`] for reading types from the buffer. |
171 | | /// |
172 | | /// This type handles unescaping bytes the buffer mostly transparently. |
173 | | /// It has an internal flag for marking when an escaped byte can be read from the buffer. |
174 | | /// Reading from the buffer in any way unmarks this flag. |
175 | | pub struct BorrowReader<'de> { |
176 | | inner: &'de [u8], |
177 | | expect_escaped: bool, |
178 | | } |
179 | | |
180 | | impl<'de> BorrowReader<'de> { |
181 | | /// Create a new reader. |
182 | 2.90k | pub const fn new(slice: &'de [u8]) -> Self { |
183 | 2.90k | BorrowReader { |
184 | 2.90k | inner: slice, |
185 | 2.90k | expect_escaped: false, |
186 | 2.90k | } |
187 | 2.90k | } |
188 | | |
189 | | #[inline] |
190 | 2.90k | pub fn is_empty(&self) -> bool { |
191 | 2.90k | self.inner.is_empty() |
192 | 2.90k | } <storekey::reader::BorrowReader>::is_empty Line | Count | Source | 190 | 2.90k | pub fn is_empty(&self) -> bool { | 191 | 2.90k | self.inner.is_empty() | 192 | 2.90k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::is_empty |
193 | | |
194 | | #[inline] |
195 | 29.0k | fn advance(&mut self, s: usize) { |
196 | 29.0k | self.inner = &self.inner[s..]; |
197 | 29.0k | } <storekey::reader::BorrowReader>::advance Line | Count | Source | 195 | 29.0k | fn advance(&mut self, s: usize) { | 196 | 29.0k | self.inner = &self.inner[s..]; | 197 | 29.0k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::advance |
198 | | |
199 | | /// Mark the next byte as possibly containing an escaped bytes. |
200 | | #[inline] |
201 | 0 | pub fn expect_escaped(&mut self) { |
202 | 0 | self.expect_escaped = true; |
203 | 0 | } |
204 | | |
205 | | /// Try to read a terminator byte if there is one. |
206 | | /// |
207 | | /// Returns true if the next byte is a terminator, otherwise returns false and the reader does |
208 | | /// not advance. |
209 | | /// |
210 | | /// Sets the `expect_escaped` flag marking the next byte as being possibly escaped. |
211 | | #[inline] |
212 | 0 | pub fn read_terminal(&mut self) -> Result<bool, DecodeError> { |
213 | 0 | self.expect_escaped = true; |
214 | 0 | let term = self.inner.first().ok_or(DecodeError::UnexpectedEnd)?; |
215 | 0 | if *term == 0 { |
216 | 0 | self.advance(1); |
217 | 0 | Ok(true) |
218 | | } else { |
219 | 0 | Ok(false) |
220 | | } |
221 | 0 | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_terminal Unexecuted instantiation: <storekey::reader::BorrowReader>::read_terminal |
222 | | |
223 | | /// Reads an fixed size array of u8 from the reader, unescaping possible escaped bytes. |
224 | | /// |
225 | | /// All other `read_*` functions of `Reader` which read a fixed size type call this function to |
226 | | /// read a certain amount of bytes from the reader. |
227 | | /// |
228 | | /// This type does not expect a null terminator after the end of the array as it is reading a |
229 | | /// fixed size type. |
230 | | /// |
231 | | /// Calling this function unsets the expected escape flag before returning. |
232 | | #[inline] |
233 | 23.5k | pub fn read_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], DecodeError> { |
234 | 23.5k | if self.expect_escaped { |
235 | 0 | self.expect_escaped = false; |
236 | 0 | if *self.inner.first().ok_or(DecodeError::UnexpectedEnd)? == 1 { |
237 | 0 | self.advance(1); |
238 | 0 | } |
239 | 23.5k | } |
240 | 23.5k | let slice = self.inner.get(..SIZE).ok_or(DecodeError::UnexpectedEnd)?; |
241 | 23.5k | let mut res = [0u8; SIZE]; |
242 | 23.5k | res.copy_from_slice(slice); |
243 | 23.5k | self.advance(SIZE); |
244 | 23.5k | Ok(res) |
245 | 23.5k | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_array::<16> <storekey::reader::BorrowReader>::read_array::<1> Line | Count | Source | 233 | 17.4k | pub fn read_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], DecodeError> { | 234 | 17.4k | if self.expect_escaped { | 235 | 0 | self.expect_escaped = false; | 236 | 0 | if *self.inner.first().ok_or(DecodeError::UnexpectedEnd)? == 1 { | 237 | 0 | self.advance(1); | 238 | 0 | } | 239 | 17.4k | } | 240 | 17.4k | let slice = self.inner.get(..SIZE).ok_or(DecodeError::UnexpectedEnd)?; | 241 | 17.4k | let mut res = [0u8; SIZE]; | 242 | 17.4k | res.copy_from_slice(slice); | 243 | 17.4k | self.advance(SIZE); | 244 | 17.4k | Ok(res) | 245 | 17.4k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_array::<2> <storekey::reader::BorrowReader>::read_array::<4> Line | Count | Source | 233 | 5.80k | pub fn read_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], DecodeError> { | 234 | 5.80k | if self.expect_escaped { | 235 | 0 | self.expect_escaped = false; | 236 | 0 | if *self.inner.first().ok_or(DecodeError::UnexpectedEnd)? == 1 { | 237 | 0 | self.advance(1); | 238 | 0 | } | 239 | 5.80k | } | 240 | 5.80k | let slice = self.inner.get(..SIZE).ok_or(DecodeError::UnexpectedEnd)?; | 241 | 5.80k | let mut res = [0u8; SIZE]; | 242 | 5.80k | res.copy_from_slice(slice); | 243 | 5.80k | self.advance(SIZE); | 244 | 5.80k | Ok(res) | 245 | 5.80k | } |
<storekey::reader::BorrowReader>::read_array::<8> Line | Count | Source | 233 | 316 | pub fn read_array<const SIZE: usize>(&mut self) -> Result<[u8; SIZE], DecodeError> { | 234 | 316 | if self.expect_escaped { | 235 | 0 | self.expect_escaped = false; | 236 | 0 | if *self.inner.first().ok_or(DecodeError::UnexpectedEnd)? == 1 { | 237 | 0 | self.advance(1); | 238 | 0 | } | 239 | 316 | } | 240 | 316 | let slice = self.inner.get(..SIZE).ok_or(DecodeError::UnexpectedEnd)?; | 241 | 316 | let mut res = [0u8; SIZE]; | 242 | 316 | res.copy_from_slice(slice); | 243 | 316 | self.advance(SIZE); | 244 | 316 | Ok(res) | 245 | 316 | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_array::<_> |
246 | | |
247 | | #[inline] |
248 | 0 | fn read_into_vec(&mut self, buffer: &mut Vec<u8>) -> Result<(), DecodeError> { |
249 | 0 | self.expect_escaped = false; |
250 | 0 | let mut iter = self.inner.iter(); |
251 | | loop { |
252 | 0 | let Some(next) = iter.next().copied() else { |
253 | 0 | return Err(DecodeError::UnexpectedEnd); |
254 | | }; |
255 | 0 | if next == 1 { |
256 | 0 | let Some(next) = iter.next().copied() else { |
257 | 0 | return Err(DecodeError::UnexpectedEnd); |
258 | | }; |
259 | 0 | buffer.push(next); |
260 | 0 | continue; |
261 | 0 | } |
262 | 0 | if next == 0 { |
263 | 0 | break; |
264 | 0 | } |
265 | 0 | buffer.push(next) |
266 | | } |
267 | 0 | self.inner = iter.as_slice(); |
268 | 0 | Ok(()) |
269 | 0 | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_into_vec Unexecuted instantiation: <storekey::reader::BorrowReader>::read_into_vec |
270 | | |
271 | | /// Reads a runtime sized `Cow<[u8]>` from the reader, expected the sequence of bytes to be |
272 | | /// ended by a terminal zero byte. |
273 | | /// |
274 | | /// If the string encoded in the buffer does not contain escaped characters this function will |
275 | | /// return a `Cow::Borrowed`. |
276 | | /// |
277 | | /// Calling this function unsets the expected escape flag before returning. |
278 | | #[inline] |
279 | 5.49k | pub fn read_cow(&mut self) -> Result<Cow<'de, [u8]>, DecodeError> { |
280 | 5.49k | self.expect_escaped = false; |
281 | 34.1k | for i in 0.. { |
282 | 34.1k | match self.inner.get(i) { |
283 | | Some(0) => { |
284 | | // hit the end without encountering a escape character so the slice can be |
285 | | // borrowed. |
286 | 5.49k | let slice = &self.inner[..i]; |
287 | 5.49k | self.advance(i + 1); |
288 | 5.49k | return Ok(Cow::Borrowed(slice)); |
289 | | } |
290 | | Some(1) => { |
291 | | // Hit an escape character so we need to create a buffer. |
292 | 0 | let mut buffer = self.inner[..i].to_vec(); |
293 | 0 | buffer.push(*self.inner.get(i + 1).ok_or(DecodeError::UnexpectedEnd)?); |
294 | 0 | self.advance(i + 2); |
295 | 0 | self.read_into_vec(&mut buffer)?; |
296 | 0 | return Ok(Cow::Owned(buffer)); |
297 | | } |
298 | 28.6k | Some(_) => {} |
299 | 0 | None => return Err(DecodeError::UnexpectedEnd), |
300 | | } |
301 | | } |
302 | 0 | unreachable!() |
303 | 5.49k | } <storekey::reader::BorrowReader>::read_cow Line | Count | Source | 279 | 5.49k | pub fn read_cow(&mut self) -> Result<Cow<'de, [u8]>, DecodeError> { | 280 | 5.49k | self.expect_escaped = false; | 281 | 34.1k | for i in 0.. { | 282 | 34.1k | match self.inner.get(i) { | 283 | | Some(0) => { | 284 | | // hit the end without encountering a escape character so the slice can be | 285 | | // borrowed. | 286 | 5.49k | let slice = &self.inner[..i]; | 287 | 5.49k | self.advance(i + 1); | 288 | 5.49k | return Ok(Cow::Borrowed(slice)); | 289 | | } | 290 | | Some(1) => { | 291 | | // Hit an escape character so we need to create a buffer. | 292 | 0 | let mut buffer = self.inner[..i].to_vec(); | 293 | 0 | buffer.push(*self.inner.get(i + 1).ok_or(DecodeError::UnexpectedEnd)?); | 294 | 0 | self.advance(i + 2); | 295 | 0 | self.read_into_vec(&mut buffer)?; | 296 | 0 | return Ok(Cow::Owned(buffer)); | 297 | | } | 298 | 28.6k | Some(_) => {} | 299 | 0 | None => return Err(DecodeError::UnexpectedEnd), | 300 | | } | 301 | | } | 302 | 0 | unreachable!() | 303 | 5.49k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_cow |
304 | | |
305 | | /// Reads a runtime sized `Vec<u8>` from the reader, expected the sequence of bytes to be |
306 | | /// ended by a terminal zero byte. |
307 | | /// |
308 | | /// Calling this function unsets the expected escape flag before returning. |
309 | | #[inline] |
310 | 0 | pub fn read_vec(&mut self) -> Result<Vec<u8>, DecodeError> { |
311 | 0 | self.expect_escaped = false; |
312 | 0 | let mut buffer = Vec::new(); |
313 | 0 | self.read_into_vec(&mut buffer)?; |
314 | 0 | Ok(buffer) |
315 | 0 | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_vec Unexecuted instantiation: <storekey::reader::BorrowReader>::read_vec |
316 | | |
317 | | /// Reads a runtime sized `Cow<str>` from the reader, expected the sequence of bytes to be |
318 | | /// ended by a terminal zero byte. |
319 | | /// |
320 | | /// If the string encoded in the buffer does not contain escaped characters this function will |
321 | | /// return a `Cow::Borrowed`. |
322 | | /// |
323 | | /// Calling this function unsets the expected escape flag before returning. |
324 | | #[inline] |
325 | 5.49k | pub fn read_str_cow(&mut self) -> Result<Cow<'de, str>, DecodeError> { |
326 | 5.49k | match self.read_cow()? { |
327 | 5.49k | Cow::Borrowed(x) => { |
328 | 5.49k | Ok(Cow::Borrowed(str::from_utf8(x).map_err(|_| DecodeError::Utf8)?)) |
329 | | } |
330 | 0 | Cow::Owned(x) => Ok(Cow::Owned(String::from_utf8(x).map_err(|_| DecodeError::Utf8)?)), |
331 | | } |
332 | 5.49k | } <storekey::reader::BorrowReader>::read_str_cow Line | Count | Source | 325 | 5.49k | pub fn read_str_cow(&mut self) -> Result<Cow<'de, str>, DecodeError> { | 326 | 5.49k | match self.read_cow()? { | 327 | 5.49k | Cow::Borrowed(x) => { | 328 | 5.49k | Ok(Cow::Borrowed(str::from_utf8(x).map_err(|_| DecodeError::Utf8)?)) | 329 | | } | 330 | 0 | Cow::Owned(x) => Ok(Cow::Owned(String::from_utf8(x).map_err(|_| DecodeError::Utf8)?)), | 331 | | } | 332 | 5.49k | } |
Unexecuted instantiation: <storekey::reader::BorrowReader>::read_str_cow |
333 | | |
334 | | /// Reads a runtime sized `String` from the reader, expected the sequence of bytes to be |
335 | | /// ended by a terminal zero byte. |
336 | | /// |
337 | | /// Calling this function unsets the expected escape flag before returning. |
338 | | #[inline] |
339 | 0 | pub fn read_string(&mut self) -> Result<String, DecodeError> { |
340 | 0 | let buffer = self.read_vec()?; |
341 | 0 | String::from_utf8(buffer).map_err(|_| DecodeError::Utf8) |
342 | 0 | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_string Unexecuted instantiation: <storekey::reader::BorrowReader>::read_string |
343 | | |
344 | | /// Reads an escaped slice from the reader, expecting the sequence of bytes to be ended by a |
345 | | /// terminal zero byte. |
346 | | /// |
347 | | /// This function never allocates and always returns a borrowed value. |
348 | | /// |
349 | | /// Calling this function unsets the expected escape flag before returning. |
350 | | #[inline] |
351 | 0 | pub fn read_escaped_slice(&mut self) -> Result<&'de EscapedSlice, DecodeError> { |
352 | 0 | self.expect_escaped = false; |
353 | 0 | let mut i = 0; |
354 | | loop { |
355 | 0 | match self.inner.get(i) { |
356 | | Some(0) => { |
357 | 0 | let res = unsafe { EscapedSlice::from_slice(&self.inner[..i + 1]) }; |
358 | 0 | self.advance(i + 1); |
359 | 0 | return Ok(res); |
360 | | } |
361 | 0 | Some(1) => { |
362 | 0 | i += 2; |
363 | 0 | } |
364 | 0 | Some(_) => { |
365 | 0 | i += 1; |
366 | 0 | } |
367 | 0 | None => return Err(DecodeError::UnexpectedEnd), |
368 | | } |
369 | | } |
370 | 0 | } |
371 | | |
372 | | /// Reads an escaped str from the reader, expecting the sequence of bytes to be ended by a |
373 | | /// terminal zero byte. |
374 | | /// |
375 | | /// This function never allocates and always returns a borrowed value. |
376 | | /// |
377 | | /// Calling this function unsets the expected escape flag before returning. |
378 | | #[inline] |
379 | 0 | pub fn read_escaped_str(&mut self) -> Result<&'de EscapedStr, DecodeError> { |
380 | 0 | let str = str::from_utf8(self.read_escaped_slice()?.as_bytes()) |
381 | 0 | .map_err(|_| DecodeError::UnexpectedEnd)?; |
382 | 0 | Ok(unsafe { EscapedStr::from_str(str) }) |
383 | 0 | } |
384 | | |
385 | | #[inline] |
386 | 0 | pub fn read_f32(&mut self) -> Result<f32, DecodeError> { |
387 | 0 | let v = self.read_u32()? as i32; |
388 | 0 | let t = ((v ^ i32::MIN) >> 31) | i32::MIN; |
389 | 0 | Ok(f32::from_bits((v ^ t) as u32)) |
390 | 0 | } |
391 | | |
392 | | #[inline] |
393 | 0 | pub fn read_f64(&mut self) -> Result<f64, DecodeError> { |
394 | 0 | let v = self.read_u64()? as i64; |
395 | 0 | let t = ((v ^ i64::MIN) >> 63) | i64::MIN; |
396 | 0 | Ok(f64::from_bits((v ^ t) as u64)) |
397 | 0 | } Unexecuted instantiation: <storekey::reader::BorrowReader>::read_f64 Unexecuted instantiation: <storekey::reader::BorrowReader>::read_f64 |
398 | | |
399 | | impl_prims! {signed i8, read_i8} |
400 | | impl_prims! {u8, read_u8} |
401 | | impl_prims! {signed i16,read_i16} |
402 | | impl_prims! {u16,read_u16} |
403 | | impl_prims! {signed i32,read_i32} |
404 | | impl_prims! {u32,read_u32} |
405 | | impl_prims! {signed i64,read_i64} |
406 | | impl_prims! {u64,read_u64} |
407 | | impl_prims! {signed i128,read_i128} |
408 | | impl_prims! {u128,read_u128} |
409 | | } |