use super::{CONTINUATION_BIT, low_bits_of_u64};
use std::io;
use bytes::{BytesMut, BufMut};
pub trait LEB128Write {
fn write_signed(&mut self, val: i64) -> Result<usize, io::Error>;
fn write_unsigned(&mut self, val: u64) -> Result<usize, io::Error>;
}
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;
val >>= 6;
let done = val == 0 || val == -1;
if done {
byte &= !CONTINUATION_BIT;
} else {
val >>= 1;
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 {
byte |= CONTINUATION_BIT;
}
let buf = [byte];
self.put_u8(byte);
bytes_written += 1;
if val == 0 {
return Ok(bytes_written);
}
}
}
}