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
use super::{CONTINUATION_BIT, low_bits_of_u64};
use std::io;
#[cfg(feature = "use-bytes")]
use bytes::{BytesMut, BufMut};

/// Trait for writing signed and unsigned LEB128 encoded numbers
pub trait LEB128Write {
    /// Write the given signed number using the LEB128 encoding to the given
    /// `std::io::Write`able. Returns the number of bytes written to `w`, or an
    /// error if writing failed.
    fn write_signed(&mut self, val: i64) -> Result<usize, io::Error>;

    /// Write the given unsigned number using the LEB128 encoding to the given
    /// `std::io::Write`able. Returns the number of bytes written to `w`, or an
    /// error if writing failed.
    fn write_unsigned(&mut self, val: u64) -> Result<usize, io::Error>;
}

#[cfg(feature = "std")]
impl<W> LEB128Write for W
    where W: io::Write
{
    fn write_signed(&mut self, mut val: i64) -> Result<usize, io::Error> {
        let mut bytes_written = 0;
        loop {
            let mut byte = val as u8;
            // Keep the sign bit for testing
            val >>= 6;
            let done = val == 0 || val == -1;
            if done {
                byte &= !CONTINUATION_BIT;
            } else {
                // Remove the sign bit
                val >>= 1;
                // More bytes to come, so set the continuation bit.
                byte |= CONTINUATION_BIT;
            }

            let buf = [byte];
            self.write_all(&buf)?;
            bytes_written += 1;

            if done {
                return Ok(bytes_written);
            }
        }
    }

    fn write_unsigned(&mut self, mut val: u64) -> Result<usize, io::Error> {
        let mut bytes_written = 0;
        loop {
            let mut byte = low_bits_of_u64(val);
            val >>= 7;
            if val != 0 {
                // More bytes to come, so set the continuation bit.
                byte |= CONTINUATION_BIT;
            }

            let buf = [byte];
            self.write_all(&buf)?;
            bytes_written += 1;

            if val == 0 {
                return Ok(bytes_written);
            }
        }
    }
}

#[cfg(feature = "use-bytes")]
impl<W> LEB128Write for W
    where W: BufMut
{
    fn write_signed(&mut self, mut val: i64) -> Result<usize, io::Error> {
        let mut bytes_written = 0;
        loop {
            let mut byte = val as u8;
            // Keep the sign bit for testing
            val >>= 6;
            let done = val == 0 || val == -1;
            if done {
                byte &= !CONTINUATION_BIT;
            } else {
                // Remove the sign bit
                val >>= 1;
                // More bytes to come, so set the continuation bit.
                byte |= CONTINUATION_BIT;
            }

            self.put_u8(byte);
            bytes_written += 1;

            if done {
                return Ok(bytes_written);
            }
        }
    }

    fn write_unsigned(&mut self, mut val: u64) -> Result<usize, io::Error> {
        let mut bytes_written = 0;
        loop {
            let mut byte = low_bits_of_u64(val);
            val >>= 7;
            if val != 0 {
                // More bytes to come, so set the continuation bit.
                byte |= CONTINUATION_BIT;
            }

            let buf = [byte];
            self.put_u8(byte);
            bytes_written += 1;

            if val == 0 {
                return Ok(bytes_written);
            }
        }
    }
}