1
use crate::PRes;
2
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
3
use std::io::{Cursor, Read, Write};
4
use unsigned_varint::{encode, io};
5
use zigzag::ZigZag;
6

7
pub trait InfallibleRead {
8
    fn read_exact(&mut self, buf: &mut [u8]);
9
}
10

11
pub trait InfallibleWrite {
12
    fn write_all(&mut self, buf: &[u8]);
13
}
14

15
struct InfallibleReadWrapper<'a, T: InfallibleRead + ?Sized> {
16
    reader: &'a mut T,
17
}
18

19
#[allow(dead_code)]
20
impl<'a, T: InfallibleRead + ?Sized> InfallibleReadWrapper<'a, T> {
21 1
    fn new(reader: &'a mut T) -> Self {
22 1
        InfallibleReadWrapper { reader }
23 1
    }
24
}
25
impl<'a, T: InfallibleRead + ?Sized> Read for InfallibleReadWrapper<'a, T> {
26 1
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
27 1
        InfallibleRead::read_exact(self.reader, buf);
28 1
        Ok(buf.len())
29 1
    }
30
}
31

32
// End of snippet taken from integer-ecoding specialized for InfallibleRead
33

34
pub(crate) trait InfallibleReadVarInt: InfallibleRead {
35
    #[inline]
36 1
    fn read_varint_u8(&mut self) -> u8 {
37 1
        io::read_u8(InfallibleReadWrapper::new(self)).expect("infallible")
38 1
    }
39

40
    #[inline]
41 1
    fn read_varint_u16(&mut self) -> u16 {
42 1
        io::read_u16(InfallibleReadWrapper::new(self)).expect("infallible")
43 1
    }
44

45
    #[inline]
46 1
    fn read_varint_u32(&mut self) -> u32 {
47 1
        io::read_u32(InfallibleReadWrapper::new(self)).expect("infallible")
48 1
    }
49

50
    #[inline]
51 1
    fn read_varint_u64(&mut self) -> u64 {
52 1
        io::read_u64(InfallibleReadWrapper::new(self)).expect("infallible")
53 1
    }
54

55
    #[inline]
56 1
    fn read_varint_u128(&mut self) -> u128 {
57 1
        io::read_u128(InfallibleReadWrapper::new(self)).expect("infallible")
58 1
    }
59

60
    #[inline]
61 1
    fn read_varint_i8(&mut self) -> i8 {
62 1
        ZigZag::decode(self.read_varint_u8())
63 1
    }
64

65
    #[inline]
66 1
    fn read_varint_i16(&mut self) -> i16 {
67 1
        ZigZag::decode(self.read_varint_u16())
68 1
    }
69

70
    #[inline]
71 1
    fn read_varint_i32(&mut self) -> i32 {
72 1
        ZigZag::decode(self.read_varint_u32())
73 1
    }
74

75
    #[inline]
76 1
    fn read_varint_i64(&mut self) -> i64 {
77 1
        ZigZag::decode(self.read_varint_u64())
78 1
    }
79

80
    #[inline]
81 1
    fn read_varint_i128(&mut self) -> i128 {
82 1
        ZigZag::decode(self.read_varint_u128())
83 1
    }
84
}
85
impl<R: InfallibleRead + ?Sized> InfallibleReadVarInt for R {}
86

87
pub(crate) trait InfallibleWriteVarInt: InfallibleWrite {
88
    #[inline]
89 1
    fn write_varint_u8(&mut self, value: u8) {
90 1
        self.write_all(encode::u8(value, &mut encode::u8_buffer()));
91 1
    }
92

93
    #[inline]
94 1
    fn write_varint_u16(&mut self, value: u16) {
95 1
        self.write_all(encode::u16(value, &mut encode::u16_buffer()));
96 1
    }
97

98
    #[inline]
99 1
    fn write_varint_u32(&mut self, value: u32) {
100 1
        self.write_all(encode::u32(value, &mut encode::u32_buffer()));
101 1
    }
102

103
    #[inline]
104 1
    fn write_varint_u64(&mut self, value: u64) {
105 1
        self.write_all(encode::u64(value, &mut encode::u64_buffer()));
106 1
    }
107

108
    #[inline]
109 1
    fn write_varint_u128(&mut self, value: u128) {
110 1
        self.write_all(encode::u128(value, &mut encode::u128_buffer()));
111 1
    }
112

113
    #[inline]
114 1
    fn write_varint_i8(&mut self, value: i8) {
115 1
        self.write_varint_u8(ZigZag::encode(value))
116 1
    }
117

118
    #[inline]
119 1
    fn write_varint_i16(&mut self, value: i16) {
120 1
        self.write_varint_u16(ZigZag::encode(value))
121 1
    }
122

123
    #[inline]
124 1
    fn write_varint_i32(&mut self, value: i32) {
125 1
        self.write_varint_u32(ZigZag::encode(value))
126 1
    }
127

128
    #[inline]
129 1
    fn write_varint_i64(&mut self, value: i64) {
130 1
        self.write_varint_u64(ZigZag::encode(value))
131 1
    }
132

133
    #[inline]
134 1
    fn write_varint_i128(&mut self, value: i128) {
135 1
        self.write_varint_u128(ZigZag::encode(value))
136 1
    }
137
}
138

139
impl<W: InfallibleWrite + ?Sized> InfallibleWriteVarInt for W {}
140

141
pub(crate) trait InfallibleWriteFormat: InfallibleWrite {
142
    #[inline]
143 1
    fn write_u8(&mut self, value: u8) {
144 1
        self.write_all(&[value])
145 1
    }
146

147
    #[inline]
148 1
    fn write_u16(&mut self, value: u16) {
149 1
        self.write_all(&value.to_be_bytes())
150 1
    }
151

152
    #[inline]
153 1
    fn write_u32(&mut self, value: u32) {
154 1
        self.write_all(&value.to_be_bytes())
155 1
    }
156

157
    #[inline]
158 1
    fn write_u64(&mut self, value: u64) {
159 1
        self.write_all(&value.to_be_bytes())
160 1
    }
161

162
    #[inline]
163
    fn write_u128(&mut self, value: u128) {
164
        self.write_all(&value.to_be_bytes())
165
    }
166

167
    #[inline]
168
    fn write_i8(&mut self, value: i8) {
169
        self.write_all(&value.to_be_bytes())
170
    }
171

172
    #[inline]
173
    fn write_i16(&mut self, value: i16) {
174
        self.write_all(&value.to_be_bytes())
175
    }
176

177
    #[inline]
178
    fn write_i32(&mut self, value: i32) {
179
        self.write_all(&value.to_be_bytes())
180
    }
181

182
    #[inline]
183
    fn write_i64(&mut self, value: i64) {
184
        self.write_all(&value.to_be_bytes())
185
    }
186

187
    #[inline]
188
    fn write_i128(&mut self, value: i128) {
189
        self.write_all(&value.to_be_bytes())
190
    }
191

192
    #[inline]
193 1
    fn write_f32(&mut self, value: f32) {
194 1
        self.write_all(&value.to_be_bytes())
195 1
    }
196

197
    #[inline]
198 1
    fn write_f64(&mut self, value: f64) {
199 1
        self.write_all(&value.to_be_bytes())
200 1
    }
201
}
202

203
impl<W: InfallibleWrite + ?Sized> InfallibleWriteFormat for W {}
204

205
impl InfallibleWrite for Vec<u8> {
206 1
    fn write_all(&mut self, buf: &[u8]) {
207 1
        self.extend_from_slice(buf);
208 1
    }
209
}
210

211
pub(crate) trait InfallibleReadFormat: InfallibleRead {
212
    #[inline]
213 1
    fn read_u8(&mut self) -> u8 {
214 1
        let mut value = [0; 1];
215 1
        self.read_exact(&mut value);
216 1
        value[0]
217 1
    }
218

219
    #[inline]
220 1
    fn read_u16(&mut self) -> u16 {
221 1
        let mut value = [0; 2];
222 1
        self.read_exact(&mut value);
223 1
        u16::from_be_bytes(value)
224 1
    }
225

226
    #[inline]
227 1
    fn read_u32(&mut self) -> u32 {
228 1
        let mut value = [0; 4];
229 1
        self.read_exact(&mut value);
230 1
        u32::from_be_bytes(value)
231 1
    }
232

233
    #[inline]
234 1
    fn read_u64(&mut self) -> u64 {
235 1
        let mut value = [0; 8];
236 1
        self.read_exact(&mut value);
237 1
        u64::from_be_bytes(value)
238 1
    }
239

240
    #[inline]
241
    fn read_u128(&mut self) -> u128 {
242
        let mut value = [0; 16];
243
        self.read_exact(&mut value);
244
        u128::from_be_bytes(value)
245
    }
246

247
    #[inline]
248
    fn read_i8(&mut self) -> i8 {
249
        let mut value = [0; 1];
250
        self.read_exact(&mut value);
251
        i8::from_be_bytes(value)
252
    }
253

254
    #[inline]
255
    fn read_i16(&mut self) -> i16 {
256
        let mut value = [0; 2];
257
        self.read_exact(&mut value);
258
        i16::from_be_bytes(value)
259
    }
260

261
    #[inline]
262
    fn read_i32(&mut self) -> i32 {
263
        let mut value = [0; 4];
264
        self.read_exact(&mut value);
265
        i32::from_be_bytes(value)
266
    }
267

268
    #[inline]
269
    fn read_i64(&mut self) -> i64 {
270
        let mut value = [0; 8];
271
        self.read_exact(&mut value);
272
        i64::from_be_bytes(value)
273
    }
274

275
    #[inline]
276
    fn read_i128(&mut self) -> i128 {
277
        let mut value = [0; 16];
278
        self.read_exact(&mut value);
279
        i128::from_be_bytes(value)
280
    }
281

282
    #[inline]
283 1
    fn read_f32(&mut self) -> f32 {
284 1
        let mut value = [0; 4];
285 1
        self.read_exact(&mut value);
286 1
        f32::from_be_bytes(value)
287 1
    }
288

289
    #[inline]
290 1
    fn read_f64(&mut self) -> f64 {
291 1
        let mut value = [0; 8];
292 1
        self.read_exact(&mut value);
293 1
        f64::from_be_bytes(value)
294 1
    }
295
}
296

297
impl<R: InfallibleRead + ?Sized> InfallibleReadFormat for R {}
298

299
impl<T> InfallibleRead for Cursor<T>
300
where
301
    T: AsRef<[u8]>,
302
{
303 1
    fn read_exact(&mut self, buf: &mut [u8]) {
304 1
        Read::read_exact(self, buf).expect("never fail in a in memory buffer");
305 1
    }
306
}
307

308 1
pub(crate) fn read_u16(buf: &[u8]) -> u16 {
309 1
    BigEndian::read_u16(buf)
310 1
}
311 1
pub(crate) fn write_u16(buf: &mut [u8], val: u16) {
312 1
    BigEndian::write_u16(buf, val)
313 1
}
314 1
pub(crate) fn read_u64(buf: &[u8]) -> u64 {
315 1
    BigEndian::read_u64(buf)
316 1
}
317 1
pub(crate) fn write_u64(buf: &mut [u8], val: u64) {
318 1
    BigEndian::write_u64(buf, val)
319 1
}
320

321
pub(crate) trait WriteFormat: Write {
322
    #[inline]
323 0
    fn write_u8(&mut self, value: u8) -> PRes<()> {
324 0
        Ok(WriteBytesExt::write_u8(self, value)?)
325 0
    }
326

327
    #[inline]
328
    fn write_u16(&mut self, value: u16) -> PRes<()> {
329
        Ok(WriteBytesExt::write_u16::<BigEndian>(self, value)?)
330
    }
331

332
    #[inline]
333
    fn write_u32(&mut self, value: u32) -> PRes<()> {
334
        Ok(WriteBytesExt::write_u32::<BigEndian>(self, value)?)
335
    }
336

337
    #[inline]
338
    fn write_u64(&mut self, value: u64) -> PRes<()> {
339
        Ok(WriteBytesExt::write_u64::<BigEndian>(self, value)?)
340
    }
341

342
    #[inline]
343
    fn write_u128(&mut self, value: u128) -> PRes<()> {
344
        Ok(WriteBytesExt::write_u128::<BigEndian>(self, value)?)
345
    }
346

347
    #[inline]
348
    fn write_i8(&mut self, value: i8) -> PRes<()> {
349
        Ok(WriteBytesExt::write_i8(self, value)?)
350
    }
351

352
    #[inline]
353
    fn write_i16(&mut self, value: i16) -> PRes<()> {
354
        Ok(WriteBytesExt::write_i16::<BigEndian>(self, value)?)
355
    }
356

357
    #[inline]
358
    fn write_i32(&mut self, value: i32) -> PRes<()> {
359
        Ok(WriteBytesExt::write_i32::<BigEndian>(self, value)?)
360
    }
361

362
    #[inline]
363
    fn write_i64(&mut self, value: i64) -> PRes<()> {
364
        Ok(WriteBytesExt::write_i64::<BigEndian>(self, value)?)
365
    }
366

367
    #[inline]
368
    fn write_i128(&mut self, value: i128) -> PRes<()> {
369
        Ok(WriteBytesExt::write_i128::<BigEndian>(self, value)?)
370
    }
371

372
    #[inline]
373
    fn write_f32(&mut self, value: f32) -> PRes<()> {
374
        Ok(WriteBytesExt::write_f32::<BigEndian>(self, value)?)
375
    }
376

377
    #[inline]
378
    fn write_f64(&mut self, value: f64) -> PRes<()> {
379
        Ok(WriteBytesExt::write_f64::<BigEndian>(self, value)?)
380
    }
381
}
382

383
impl<W: Write + ?Sized> WriteFormat for W {}
384

385
pub(crate) trait ReadFormat: Read {
386
    #[inline]
387 1
    fn read_u8(&mut self) -> PRes<u8> {
388 1
        Ok(ReadBytesExt::read_u8(self)?)
389 1
    }
390

391
    #[inline]
392
    fn read_u16(&mut self) -> PRes<u16> {
393
        Ok(ReadBytesExt::read_u16::<BigEndian>(self)?)
394
    }
395

396
    #[inline]
397
    fn read_u32(&mut self) -> PRes<u32> {
398
        Ok(ReadBytesExt::read_u32::<BigEndian>(self)?)
399
    }
400

401
    #[inline]
402 0
    fn read_u64(&mut self) -> PRes<u64> {
403 0
        Ok(ReadBytesExt::read_u64::<BigEndian>(self)?)
404 0
    }
405

406
    #[inline]
407
    fn read_u128(&mut self) -> PRes<u128> {
408
        Ok(ReadBytesExt::read_u128::<BigEndian>(self)?)
409
    }
410

411
    #[inline]
412
    fn read_i8(&mut self) -> PRes<i8> {
413
        Ok(ReadBytesExt::read_i8(self)?)
414
    }
415

416
    #[inline]
417
    fn read_i16(&mut self) -> PRes<i16> {
418
        Ok(ReadBytesExt::read_i16::<BigEndian>(self)?)
419
    }
420

421
    #[inline]
422
    fn read_i32(&mut self) -> PRes<i32> {
423
        Ok(ReadBytesExt::read_i32::<BigEndian>(self)?)
424
    }
425

426
    #[inline]
427
    fn read_i64(&mut self) -> PRes<i64> {
428
        Ok(ReadBytesExt::read_i64::<BigEndian>(self)?)
429
    }
430

431
    #[inline]
432
    fn read_i128(&mut self) -> PRes<i128> {
433
        Ok(ReadBytesExt::read_i128::<BigEndian>(self)?)
434
    }
435

436
    #[inline]
437
    fn read_f32(&mut self) -> PRes<f32> {
438
        Ok(ReadBytesExt::read_f32::<BigEndian>(self)?)
439
    }
440

441
    #[inline]
442
    fn read_f64(&mut self) -> PRes<f64> {
443
        Ok(ReadBytesExt::read_f64::<BigEndian>(self)?)
444
    }
445
}
446

447
impl<R: Read + ?Sized> ReadFormat for R {}

Read our documentation on viewing source code .

Loading