use std::io::{
Result,
Seek,
SeekFrom,
Write,
};
pub struct TeeWriter<P, B> {
primary: P,
branch: B,
}
impl<P, B> TeeWriter<P, B> {
#[inline]
pub fn new(primary: P, branch: B) -> Self {
Self { primary, branch }
}
#[inline]
pub fn primary_ref(&self) -> &P {
&self.primary
}
#[inline]
pub fn primary_mut(&mut self) -> &mut P {
&mut self.primary
}
#[inline]
pub fn branch_ref(&self) -> &B {
&self.branch
}
#[inline]
pub fn branch_mut(&mut self) -> &mut B {
&mut self.branch
}
#[inline]
pub fn into_inner(self) -> (P, B) {
(self.primary, self.branch)
}
}
impl<P, B> Write for TeeWriter<P, B>
where
P: Write,
B: Write,
{
fn write(&mut self, buffer: &[u8]) -> Result<usize> {
let count = self.primary.write(buffer)?;
self.branch.write_all(&buffer[..count])?;
Ok(count)
}
fn flush(&mut self) -> Result<()> {
self.primary.flush()?;
self.branch.flush()
}
}
impl<P, B> Seek for TeeWriter<P, B>
where
P: Seek,
B: Seek,
{
fn seek(&mut self, position: SeekFrom) -> Result<u64> {
let primary_position = self.primary.seek(position)?;
self.branch.seek(SeekFrom::Start(primary_position))?;
Ok(primary_position)
}
}