orengine-utils 3.1.3

This repository provides utilities for building high-performance applications.
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Variable-length integer (varint) encoding and decoding.
//!
//! This module provides efficient serialization routines for integer types.
//!
//! Unsigned integers are encoded using a little-endian base-128 (LEB128-like)
//! variable-length representation. Small values occupy fewer bytes than larger
//! values.
//!
//! Signed integers are encoded using `ZigZag encoding` followed by unsigned
//! varint encoding. This efficiently stores small negative numbers.
//!
//! # Supported integer types
//!
//! - Unsigned: `u8`, `u16`, `u32`, `u64`, `u128`
//! - Signed: `i8`, `i16`, `i32`, `i64`, `i128`
//!
//! Convenience extension traits are also provided for all `Read` and `Write`
//! implementations.
//!
//! # Examples
//!
//! ```rust
//! use std::io::Cursor;
//! use orengine_utils::varint::{ReadVarInt, WriteVarInt};
//!
//! let mut buf = Vec::new();
//!
//! buf.write_varint(123u32).unwrap();
//! buf.write_varint(-42i32).unwrap();
//!
//! let mut cursor = Cursor::new(buf);
//!
//! let a: u32 = cursor.read_varint().unwrap();
//! let b: i32 = cursor.read_varint().unwrap();
//!
//! assert_eq!(a, 123);
//! assert_eq!(b, -42);
//! ```
use std::io::{self, Read, Write};

#[inline]
pub(crate) fn write_u8_to<W: Write + ?Sized>(n: u8, writer: &mut W) -> io::Result<usize> {
    writer.write_all(&[n])?;

    Ok(1)
}

#[inline]
pub(crate) fn read_u8_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<u8> {
    let mut result = [0];

    reader.read_exact(&mut result)?;

    Ok(u8::from_le_bytes(result))
}

#[inline]
pub(crate) fn write_i8_to<W: Write + ?Sized>(n: i8, writer: &mut W) -> io::Result<usize> {
    writer.write_all(&n.to_le_bytes())?;

    Ok(1)
}

#[inline]
pub(crate) fn read_i8_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<i8> {
    let mut result = [0];

    reader.read_exact(&mut result)?;

    Ok(result[0].cast_signed())
}

#[inline]
#[allow(clippy::cast_possible_truncation, reason = "False positive.")]
pub(crate) fn write_u128_to<W: Write + ?Sized>(mut n: u128, writer: &mut W) -> io::Result<usize> {
    let mut buf = [0u8; 19];
    let mut idx = 0;

    while n >= 0x80 {
        buf[idx] = (n as u8) | 0x80;
        n >>= 7;
        idx += 1;
    }

    buf[idx] = n as u8;
    idx += 1;

    writer.write_all(&buf[..idx])?;
    Ok(idx)
}

#[inline]
#[allow(clippy::cast_lossless, reason = "False positive.")]
pub(crate) fn read_u128_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<u128> {
    let mut result = 0u128;
    let mut shift = 0;
    let mut buf = [0u8; 1];

    loop {
        reader.read_exact(&mut buf)?;
        let byte = buf[0];

        if shift >= 126 && (byte & 0x80) != 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "varint too large for u128",
            ));
        }

        result |= ((byte & 0x7f) as u128) << shift;

        if byte & 0x80 == 0 {
            break;
        }

        shift += 7;
    }

    Ok(result)
}

macro_rules! impl_unsigned {
    ($ty:ty) => {
        paste::paste! {
            #[inline]
            #[allow(clippy::cast_lossless, reason = "It is generated code.")]
            #[allow(clippy::cast_possible_truncation, reason = "It is generated code.")]
            pub(crate) fn [<write_ $ty _to>]<W: Write + ?Sized>(
                value: $ty,
                writer: &mut W,
            ) -> io::Result<usize> {
                write_u128_to(value as u128, writer)
            }

            #[inline]
            #[allow(clippy::cast_lossless, reason = "It is generated code.")]
            #[allow(clippy::cast_possible_truncation, reason = "It is generated code.")]
            pub(crate) fn [<read_ $ty _from>]<R: Read + ?Sized>(
                reader: &mut R,
            ) -> io::Result<$ty> {
                let value = read_u128_from(reader)?;

                if value > <$ty>::MAX as u128 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!("varint too large for ", stringify!($ty)),
                    ));
                }

                Ok(value as $ty)
            }

            pub trait [<Write $ty:camel>]: Write {
                fn [<write_ $ty>](
                    &mut self,
                    value: $ty,
                ) -> io::Result<usize> {
                    [<write_ $ty _to>](value, self)
                }
            }

            impl<T: Write> [<Write $ty:camel>] for T {}

            pub trait [<Read $ty:camel>]: Read {
                fn [<read_ $ty>](
                    &mut self,
                ) -> io::Result<$ty> {
                    [<read_ $ty _from>](self)
                }
            }

            impl<T: Read> [<Read $ty:camel>] for T {}
        }
    };
}

impl_unsigned!(u16);
impl_unsigned!(u32);
impl_unsigned!(u64);

#[inline]
#[allow(clippy::cast_sign_loss, reason = "It will be restored")]
fn zigzag_encode(v: i128) -> u128 {
    ((v << 1) ^ (v >> 127)) as u128
}

#[inline]
#[allow(clippy::cast_possible_wrap, reason = "It will be restored")]
fn zigzag_decode(v: u128) -> i128 {
    ((v >> 1) as i128) ^ (-((v & 1) as i128))
}

#[inline]
pub(crate) fn write_i128_to<W: Write + ?Sized>(n: i128, writer: &mut W) -> io::Result<usize> {
    write_u128_to(zigzag_encode(n), writer)
}

#[inline]
pub(crate) fn read_i128_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<i128> {
    Ok(zigzag_decode(read_u128_from(reader)?))
}

macro_rules! impl_signed {
    ($ty:ty) => {
        paste::paste! {
            #[inline]
            #[allow(clippy::cast_lossless, reason = "It is generated code.")]
            #[allow(clippy::cast_possible_truncation, reason = "It is generated code.")]
            pub(crate) fn [<write_ $ty _to>]<W: Write + ?Sized>(
                value: $ty,
                writer: &mut W,
            ) -> io::Result<usize> {
                write_u128_to(zigzag_encode(value as i128), writer)
            }

            #[inline]
            #[allow(clippy::cast_lossless, reason = "It is generated code.")]
            #[allow(clippy::cast_possible_truncation, reason = "It is generated code.")]
            pub(crate) fn [<read_ $ty _from>]<R: Read + ?Sized>(
                reader: &mut R,
            ) -> io::Result<$ty> {
                let value = zigzag_decode(read_u128_from(reader)?);

                if value < <$ty>::MIN as i128 || value > <$ty>::MAX as i128 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!("varint too large for ", stringify!($ty)),
                    ));
                }

                Ok(value as $ty)
            }
        }
    };
}

impl_signed!(i16);
impl_signed!(i32);
impl_signed!(i64);

/// A type that can be encoded and decoded as a variable-length integer.
///
/// This trait is implemented for all primitive integer types supported by this
/// module.
///
/// It is primarily intended for generic serialization code.
///
/// Most users should prefer using the [`ReadVarInt`] and [`WriteVarInt`]
/// extension traits.
pub trait VarInt: Sized {
    /// Writes the number as little-endian base-128 variable-length representation to the writer.
    fn write_as_varint_to<W: Write + ?Sized>(self, writer: &mut W) -> io::Result<usize>;
    /// Reads the number as little-endian base-128 variable-length representation from the reader.
    fn read_varint_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<Self>;
}

macro_rules! impl_varint {
    ($($ty:ty),*) => {
        $(
            paste::paste! {
                impl VarInt for $ty {
                    #[inline]
                    fn write_as_varint_to<W: Write + ?Sized>(self, writer: &mut W) -> io::Result<usize> {
                        [<write_ $ty _to>](self, writer)
                    }

                    #[inline]
                    fn read_varint_from<R: Read + ?Sized>(reader: &mut R) -> io::Result<Self> {
                        [<read_ $ty _from>](reader)
                    }
                }
            }
        )*
    };
}

impl_varint!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);

/// Extension trait for writing variable-length integers.
///
/// Implemented automatically for every type implementing [`Write`].
///
/// # Example
///
/// ```rust
/// use std::io::Cursor;
/// use orengine_utils::varint::WriteVarInt;
///
/// let mut writer = Cursor::new(Vec::new());
///
/// writer.write_varint(42u64).unwrap();
/// ```
pub trait WriteVarInt: Write {
    /// Writes [`VarInt`] to the writer.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::io::Cursor;
    /// use orengine_utils::varint::WriteVarInt;
    ///
    /// let mut writer = Cursor::new(Vec::new());
    ///
    /// writer.write_varint(42u64).unwrap();
    /// ```
    fn write_varint<T: VarInt>(&mut self, value: T) -> io::Result<usize> {
        value.write_as_varint_to(self)
    }
}

/// Extension trait for reading variable-length integers.
///
/// Implemented automatically for every type implementing [`Read`].
///
/// # Example
///
/// ```rust
/// use std::io::Cursor;
/// use orengine_utils::varint::{ReadVarInt, WriteVarInt};
///
/// let mut data = Vec::new();
/// data.write_varint(500u32).unwrap();
///
/// let mut reader = Cursor::new(data);
///
/// let value: u32 = reader.read_varint().unwrap();
///
/// assert_eq!(value, 500);
/// ```
pub trait ReadVarInt: Read {
    /// Reads a [`VarInt`] from the reader.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::io::Cursor;
    /// use orengine_utils::varint::{ReadVarInt, WriteVarInt};
    ///
    /// let mut data = Vec::new();
    /// data.write_varint(500u32).unwrap();
    ///
    /// let mut reader = Cursor::new(data);
    ///
    /// let value: u32 = reader.read_varint().unwrap();
    ///
    /// assert_eq!(value, 500);
    /// ```
    fn read_varint<T: VarInt>(&mut self) -> io::Result<T> {
        T::read_varint_from(self)
    }
}

impl<W: Write> WriteVarInt for W {}
impl<R: Read> ReadVarInt for R {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    fn roundtrip<T>(value: T)
    where
        T: VarInt + Copy + PartialEq + std::fmt::Debug,
    {
        let mut buf = Vec::new();

        value.write_as_varint_to(&mut buf).unwrap();

        let decoded = T::read_varint_from(&mut Cursor::new(buf)).unwrap();

        assert_eq!(value, decoded);
    }

    #[test]
    fn roundtrip_unsigned() {
        roundtrip(0u8);
        roundtrip(1u8);
        roundtrip(u8::MAX);

        roundtrip(0u16);
        roundtrip(127u16);
        roundtrip(128u16);
        roundtrip(u16::MAX);

        roundtrip(0u32);
        roundtrip(u32::MAX);

        roundtrip(0u64);
        roundtrip(u64::MAX);

        roundtrip(0u128);
        roundtrip(u128::MAX);
    }

    #[test]
    fn roundtrip_signed() {
        roundtrip(0i8);
        roundtrip(-1i8);
        roundtrip(i8::MIN);
        roundtrip(i8::MAX);

        roundtrip(0i16);
        roundtrip(-1i16);
        roundtrip(i16::MIN);
        roundtrip(i16::MAX);

        roundtrip(0i32);
        roundtrip(i32::MIN);
        roundtrip(i32::MAX);

        roundtrip(0i64);
        roundtrip(i64::MIN);
        roundtrip(i64::MAX);

        roundtrip(0i128);
        roundtrip(i128::MIN);
        roundtrip(i128::MAX);
    }

    #[test]
    fn encoding_size() {
        let mut buf = Vec::new();

        assert_eq!(write_u128_to(0, &mut buf).unwrap(), 1);

        buf.clear();
        assert_eq!(write_u128_to(127, &mut buf).unwrap(), 1);

        buf.clear();
        assert_eq!(write_u128_to(128, &mut buf).unwrap(), 2);

        buf.clear();
        assert_eq!(write_u128_to(16383, &mut buf).unwrap(), 2);

        buf.clear();
        assert_eq!(write_u128_to(16384, &mut buf).unwrap(), 3);
    }

    #[test]
    fn malformed_varint_is_rejected() {
        let bytes = [0xff; 19];

        let err = read_u128_from(&mut Cursor::new(bytes)).unwrap_err();

        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn overflow_for_smaller_type() {
        let mut buf = Vec::new();

        write_u32_to(70000, &mut buf).unwrap();

        let err = read_u16_from(&mut Cursor::new(buf)).unwrap_err();

        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn generic_traits_work() {
        let mut buf = Vec::new();

        buf.write_varint(12345u32).unwrap();
        buf.write_varint(-567i32).unwrap();

        let mut cursor = Cursor::new(buf);

        let a: u32 = cursor.read_varint().unwrap();
        let b: i32 = cursor.read_varint().unwrap();

        assert_eq!(a, 12345);
        assert_eq!(b, -567);
    }
}