use core::marker::PhantomData;
use std::collections::TryReserveError;
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::TranscodeEncodeOutput;
use qubit_codec_binary::BinaryCodec;
use qubit_io::Buffer;
use qubit_io::Output;
use qubit_io::Seekable;
use crate::util::MIN_CODEC_BUFFER_CAPACITY;
use crate::util::checked_u16_len;
use crate::util::checked_u32_len;
use crate::util::write_all;
use super::internal::TranscodeEncodeOutputExt;
pub struct BufferedBinaryWriter<W, O = BigEndian>
where
W: Output<Item = u8>,
{
output: TranscodeEncodeOutput<W>,
marker: PhantomData<fn() -> O>,
}
impl<W, O> BufferedBinaryWriter<W, O>
where
W: Output<Item = u8>,
O: ByteOrderSpec,
{
#[must_use]
#[inline]
pub fn new(inner: W) -> Self {
Self {
output: TranscodeEncodeOutput::new(inner),
marker: PhantomData,
}
}
#[must_use]
#[inline]
pub fn with_capacity(inner: W, capacity: usize) -> Self {
Self {
output: TranscodeEncodeOutput::with_capacity(
inner,
capacity.max(MIN_CODEC_BUFFER_CAPACITY),
),
marker: PhantomData,
}
}
#[inline]
pub fn try_with_capacity(
inner: W,
capacity: usize,
) -> std::result::Result<Self, TryReserveError> {
Ok(Self {
output: TranscodeEncodeOutput::try_with_capacity(
inner,
capacity.max(MIN_CODEC_BUFFER_CAPACITY),
)?,
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.output.inner()
}
#[must_use = "the returned inner writer and pending buffer must be handled"]
#[inline(always)]
pub fn into_parts(self) -> (W, Buffer<u8>) {
self.output.into_parts()
}
}
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 buffer."]
#[doc = ""]
#[doc = "# Returns"]
#[doc = ""]
#[doc = "Returns after the encoded bytes have been accepted."]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = "Returns an output error encountered while making buffer \
space."]
#[inline(always)]
pub fn $method(&mut self, value: $ty) -> Result<()> {
type Codec = BinaryCodec<$ty, $order>;
self.output.write_encoded::<Codec>(value)
}
};
}
macro_rules! impl_for_order {
($order:ty) => {
impl<W> BufferedBinaryWriter<W, $order>
where
W: Output<Item = u8>,
{
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.");
pub fn write_string_with_u16_len(
&mut self,
value: &str,
) -> Result<()> {
self.write_u16(checked_u16_len(value.len())?)?;
write_all(self, value.as_bytes())
}
pub fn write_string_with_u32_len(
&mut self,
value: &str,
) -> Result<()> {
self.write_u32(checked_u32_len(value.len())?)?;
write_all(self, value.as_bytes())
}
}
};
}
impl_for_order!(BigEndian);
impl_for_order!(LittleEndian);
impl_for_order!(NativeEndian);
impl<W, O> Output for BufferedBinaryWriter<W, O>
where
W: Output<Item = u8>,
{
type Item = u8;
#[inline(always)]
fn is_buffered(&self) -> bool {
true
}
#[inline(always)]
unsafe fn write_unchecked(
&mut self,
input: &[u8],
index: usize,
count: usize,
) -> Result<usize> {
unsafe { self.output.write_unchecked(input, index, count) }
}
#[inline(always)]
fn flush(&mut self) -> Result<()> {
self.output.flush()
}
}
impl<W, O> Seekable for BufferedBinaryWriter<W, O>
where
W: Output<Item = u8> + Seekable<Unit = u8>,
{
type Unit = u8;
#[inline(always)]
fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
self.output.seek(position)
}
}