use std::io::Result;
use std::io::SeekFrom;
use qubit_codec_binary::Leb128Codec;
use qubit_codec_binary::NonStrict;
use qubit_io::ByteOutput;
use qubit_io::Output;
use qubit_io::Seekable;
use qubit_io::SeekableByteOutput;
use crate::util::MIN_CODEC_BUFFER_CAPACITY;
use crate::util::checked_u64_len;
use crate::util::encode_infallible_unchecked;
use crate::util::write_all;
pub struct Leb128Writer<W> {
inner: W,
buffer: [u8; MIN_CODEC_BUFFER_CAPACITY],
}
impl<W> Leb128Writer<W> {
#[must_use]
#[inline]
pub const fn new(inner: W) -> Self {
Self {
inner,
buffer: [0; MIN_CODEC_BUFFER_CAPACITY],
}
}
#[must_use]
#[inline(always)]
pub const fn inner(&self) -> &W {
&self.inner
}
#[must_use]
#[inline(always)]
pub fn inner_mut(&mut self) -> &mut W {
&mut self.inner
}
#[must_use]
#[inline(always)]
pub fn into_inner(self) -> W {
self.inner
}
}
macro_rules! impl_write_value {
($method:ident, $ty:ty, $doc:literal) => {
#[doc = $doc]
#[doc = ""]
#[doc = "# Parameters"]
#[doc = ""]
#[doc = "- `value`: Integer to encode and write."]
#[doc = ""]
#[doc = "# Returns"]
#[doc = ""]
#[doc = "Returns after the canonical payload has been written."]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = "Returns an output error, including a write-zero error when \
the output stops making progress."]
#[inline(always)]
pub fn $method(&mut self, value: $ty) -> Result<()> {
type Codec = Leb128Codec<$ty, NonStrict>;
self.write_leb128::<$ty, { Codec::MAX_ENCODE_UNITS_PER_VALUE }, _>(value, |bytes, value| unsafe {
encode_infallible_unchecked::<Codec>(value, bytes, 0)
})
}
};
}
impl<W> Leb128Writer<W>
where
W: ByteOutput,
{
impl_write_value!(write_u8, u8, "Writes an unsigned LEB128 `u8`.");
impl_write_value!(write_u16, u16, "Writes an unsigned LEB128 `u16`.");
impl_write_value!(write_u32, u32, "Writes an unsigned LEB128 `u32`.");
impl_write_value!(write_u64, u64, "Writes an unsigned LEB128 `u64`.");
impl_write_value!(write_u128, u128, "Writes an unsigned LEB128 `u128`.");
impl_write_value!(write_usize, usize, "Writes an unsigned LEB128 `usize`.");
impl_write_value!(write_i8, i8, "Writes a signed LEB128 `i8`.");
impl_write_value!(write_i16, i16, "Writes a signed LEB128 `i16`.");
impl_write_value!(write_i32, i32, "Writes a signed LEB128 `i32`.");
impl_write_value!(write_i64, i64, "Writes a signed LEB128 `i64`.");
impl_write_value!(write_i128, i128, "Writes a signed LEB128 `i128`.");
impl_write_value!(write_isize, isize, "Writes a signed LEB128 `isize`.");
#[inline]
pub fn write_utf8_string_usize(&mut self, value: &str) -> Result<()> {
self.write_usize(value.len())?;
let bytes = value.as_bytes();
write_all(&mut self.inner, bytes)
}
#[inline]
pub fn write_utf8_string_u64(&mut self, value: &str) -> Result<()> {
self.write_u64(checked_u64_len(value.len())?)?;
let bytes = value.as_bytes();
write_all(&mut self.inner, bytes)
}
#[inline]
fn write_leb128<T, const N: usize, F>(&mut self, value: T, encode: F) -> Result<()>
where
F: FnOnce(&mut [u8; MIN_CODEC_BUFFER_CAPACITY], T) -> usize,
{
let len = encode(&mut self.buffer, value);
write_all(&mut self.inner, &self.buffer[..len])
}
}
impl<W> Output for Leb128Writer<W>
where
W: ByteOutput,
{
type Item = u8;
#[inline(always)]
fn is_buffered(&self) -> bool {
self.inner.is_buffered()
}
#[inline(always)]
unsafe fn write_unchecked(&mut self, input: &[u8], index: usize, count: usize) -> Result<usize> {
unsafe { self.inner.write_unchecked(input, index, count) }
}
#[inline(always)]
fn flush(&mut self) -> Result<()> {
Output::flush(&mut self.inner)
}
}
impl<W> Seekable for Leb128Writer<W>
where
W: SeekableByteOutput,
{
type Unit = u8;
#[inline(always)]
fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
self.inner.seek_to(position)
}
}
impl_output_methods!(Leb128Writer<W> where W: ByteOutput);
impl_seekable_methods!(Leb128Writer<W> where W: SeekableByteOutput);