musli_common/
buffered_writer.rsuse musli::{Buf, Context};
use crate::fixed::FixedBytes;
use crate::writer::Writer;
pub struct BufferedWriter<const N: usize, W> {
buf: FixedBytes<N>,
writer: W,
}
impl<const N: usize, W> BufferedWriter<N, W>
where
W: Writer,
{
pub fn new(writer: W) -> Self {
Self {
buf: FixedBytes::new(),
writer,
}
}
pub fn finish<C>(mut self, cx: &C) -> Result<(), C::Error>
where
C: ?Sized + Context,
{
if !self.buf.is_empty() {
self.writer.write_bytes(cx, self.buf.as_slice())?;
}
Ok(())
}
}
impl<const N: usize, W> Writer for BufferedWriter<N, W>
where
W: Writer,
{
type Mut<'this> = &'this mut Self where Self: 'this;
#[inline]
fn borrow_mut(&mut self) -> Self::Mut<'_> {
self
}
#[inline]
fn write_buffer<C, B>(&mut self, cx: &C, buffer: B) -> Result<(), C::Error>
where
C: ?Sized + Context,
B: Buf,
{
self.write_bytes(cx, buffer.as_slice())
}
#[inline]
fn write_bytes<C>(&mut self, cx: &C, bytes: &[u8]) -> Result<(), C::Error>
where
C: ?Sized + Context,
{
if self.buf.remaining() < bytes.len() {
self.writer.write_bytes(cx, self.buf.as_slice())?;
self.buf.clear();
}
self.buf.write_bytes(cx, bytes)
}
}