use core::marker::PhantomData;
use std::io::Result;
use std::io::SeekFrom;
use qubit_codec::BigEndian;
use qubit_codec::ByteOrder;
use qubit_codec::ByteOrderSpec;
use qubit_codec::LittleEndian;
use qubit_codec::NativeEndian;
use qubit_codec_binary::BinaryCodec;
use qubit_io::ByteOutput;
use qubit_io::Output;
use qubit_io::Seekable;
use qubit_io::SeekableByteOutput;
use crate::util::checked_u16_len;
use crate::util::checked_u32_len;
use crate::util::encode_infallible_unchecked;
use crate::util::write_all;
pub struct BinaryWriter<W, O = BigEndian> {
inner: W,
buffer: [u8; 16],
marker: PhantomData<fn() -> O>,
}
impl<W, O> BinaryWriter<W, O>
where
O: ByteOrderSpec,
{
#[must_use]
#[inline]
pub const fn new(inner: W) -> Self {
Self {
inner,
buffer: [0; 16],
marker: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub const fn byte_order(&self) -> ByteOrder {
O::ORDER
}
#[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_value_write {
($order:ty, $method:ident, $ty:ty, $doc:literal) => {
#[doc = $doc]
#[doc = ""]
#[doc = "# Parameters"]
#[doc = ""]
#[doc = "- `value`: Scalar to encode and write."]
#[doc = ""]
#[doc = "# Returns"]
#[doc = ""]
#[doc = "Returns after all encoded bytes have been written."]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = "Returns an output error, including a write-zero error when \
the output stops making progress."]
#[inline]
pub fn $method(&mut self, value: $ty) -> Result<()> {
type Codec = BinaryCodec<$ty, $order>;
const LEN: usize = Codec::MAX_ENCODE_UNITS_PER_VALUE;
unsafe {
let _ = encode_infallible_unchecked::<Codec>(value, &mut self.buffer, 0);
}
write_all(&mut self.inner, &self.buffer[..LEN])
}
};
}
macro_rules! impl_for_order {
($order:ty) => {
impl<W> BinaryWriter<W, $order>
where
W: ByteOutput,
{
impl_value_write!($order, write_u8, u8, "Writes an unsigned 8-bit integer.");
impl_value_write!($order, write_i8, i8, "Writes a signed 8-bit integer.");
impl_value_write!($order, write_u16, u16, "Writes an unsigned 16-bit integer.");
impl_value_write!($order, write_u32, u32, "Writes an unsigned 32-bit integer.");
impl_value_write!($order, write_u64, u64, "Writes an unsigned 64-bit integer.");
impl_value_write!($order, write_u128, u128, "Writes an unsigned 128-bit integer.");
impl_value_write!($order, write_i16, i16, "Writes a signed 16-bit integer.");
impl_value_write!($order, write_i32, i32, "Writes a signed 32-bit integer.");
impl_value_write!($order, write_i64, i64, "Writes a signed 64-bit integer.");
impl_value_write!($order, write_i128, i128, "Writes a signed 128-bit integer.");
impl_value_write!($order, write_f32, f32, "Writes a 32-bit float.");
impl_value_write!($order, write_f64, f64, "Writes a 64-bit float.");
#[inline]
pub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()> {
self.write_u16(checked_u16_len(value.len())?)?;
let bytes = value.as_bytes();
write_all(&mut self.inner, bytes)
}
#[inline]
pub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()> {
self.write_u32(checked_u32_len(value.len())?)?;
let bytes = value.as_bytes();
write_all(&mut self.inner, bytes)
}
}
};
}
impl_for_order!(BigEndian);
impl_for_order!(LittleEndian);
impl_for_order!(NativeEndian);
impl<W, O> Output for BinaryWriter<W, O>
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, O> Seekable for BinaryWriter<W, O>
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!(BinaryWriter<W, O> where W: ByteOutput);
impl_seekable_methods!(BinaryWriter<W, O> where W: SeekableByteOutput);