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
use crate::bytes::{NumBytes, Read, ReadError, Write, WriteError};

/// Variable Length Unsigned Integer. This provides more efficient
/// serialization of 32-bit unsigned int. It serialuzes a 32-bit unsigned
/// integer in as few bytes as possible. `UnsignedInt` is unsigned and uses
/// [VLQ or Base-128 encoding](https://en.wikipedia.org/wiki/Variable-length_quantity)
/// <https://github.com/EOSIO/eosio.cdt/blob/4985359a30da1f883418b7133593f835927b8046/libraries/eosiolib/core/eosio/varint.hpp#L15-L237>
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Hash, Default)]
pub struct UnsignedInt(u32);

impl From<usize> for UnsignedInt {
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    fn from(v: usize) -> Self {
        Self(v as u32)
    }
}

impl From<UnsignedInt> for usize {
    #[must_use]
    fn from(v: UnsignedInt) -> Self {
        v.0 as Self
    }
}

impl From<u32> for UnsignedInt {
    #[must_use]
    fn from(v: u32) -> Self {
        Self(v)
    }
}

impl From<u16> for UnsignedInt {
    #[must_use]
    fn from(v: u16) -> Self {
        Self(v.into())
    }
}

impl From<u8> for UnsignedInt {
    #[must_use]
    fn from(v: u8) -> Self {
        Self(v.into())
    }
}

impl NumBytes for UnsignedInt {
    #[inline]
    #[must_use]
    fn num_bytes(&self) -> usize {
        let mut val = u64::from(self.0);
        let mut bytes = 0_usize;
        loop {
            val >>= 7;
            bytes += 1;
            if val == 0 {
                break;
            }
        }
        bytes
    }
}

impl Read for UnsignedInt {
    #[inline]
    #[allow(clippy::cast_possible_truncation)]
    fn read(bytes: &[u8], pos: &mut usize) -> Result<Self, ReadError> {
        let mut v = 0_u64;
        let mut by = 0_u8;
        loop {
            let b = u8::read(bytes, pos)?;
            v |= u64::from(u32::from(b & 0x7f) << by);
            by += 7;
            if b & 0x80 == 0 {
                break;
            }
        }
        Ok(Self(v as u32))
    }
}

impl Write for UnsignedInt {
    #[inline]
    #[allow(clippy::cast_possible_truncation)]
    fn write(
        &self,
        bytes: &mut [u8],
        pos: &mut usize,
    ) -> Result<(), WriteError> {
        let mut val = u64::from(self.0);
        loop {
            let mut b = (val as u8) & 0x7f;
            val >>= 7;
            b |= ((val > 0) as u8) << 7;
            b.write(bytes, pos)?;
            if val == 0 {
                break;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod unsigned_int_tests {
    use super::UnsignedInt;
    use crate::bytes::{NumBytes, Read, Write};

    macro_rules! write_read_tests {
        ($($i:ident, $v:expr, $n:expr)*) => ($(
            #[test]
            fn $i() {
                let mut bytes = [0_u8; 10];
                let mut write_pos = 0;
                let varint: UnsignedInt = $v.into();
                assert_eq!(varint.num_bytes(), $n);

                varint.write(&mut bytes, &mut write_pos).unwrap();
                assert_eq!(write_pos, $n);
                let mut read_pos = 0;
                let result = UnsignedInt::read(&bytes, &mut read_pos).unwrap();
                assert_eq!(result, varint);
                assert_eq!(read_pos, write_pos);
            }
        )*)
    }

    write_read_tests! {
        read_write_1, 1_u32, 1
        read_write_50, 50_u32, 1
        read_write_u8_min, u8::min_value(), 1
        read_write_u8_max, u8::max_value(), 2
        read_write_u16_min, u16::min_value(), 1
        read_write_u16_max, u16::max_value(), 3
        read_write_u32_min, u32::min_value(), 1
        read_write_u32_max, u32::max_value(), 5
    }
}