use std::io::{
self,
SeekFrom,
};
use crate::{
Input,
Output,
Seekable,
SyncSeekTeeInput,
};
#[must_use]
#[derive(Debug)]
pub struct TeeInput<I, B> {
inner: I,
branch: B,
}
impl<I, B> TeeInput<I, B> {
#[inline(always)]
pub const fn new(inner: I, branch: B) -> Self {
Self { inner, branch }
}
#[inline(always)]
pub const fn with_sync_branch_seek(
inner: I,
branch: B,
) -> SyncSeekTeeInput<I, B> {
SyncSeekTeeInput::new(inner, branch)
}
#[inline(always)]
#[must_use]
pub const fn inner(&self) -> &I {
&self.inner
}
#[inline(always)]
#[must_use]
pub fn inner_mut(&mut self) -> &mut I {
&mut self.inner
}
#[inline(always)]
#[must_use]
pub const fn branch(&self) -> &B {
&self.branch
}
#[inline(always)]
#[must_use]
pub fn branch_mut(&mut self) -> &mut B {
&mut self.branch
}
#[inline(always)]
#[must_use]
pub fn into_parts(self) -> (I, B) {
(self.inner, self.branch)
}
}
impl<I, B> Input for TeeInput<I, B>
where
I: Input,
B: Output<Item = I::Item>,
{
type Item = I::Item;
#[inline(always)]
fn is_buffered(&self) -> bool {
self.inner.is_buffered()
}
#[inline]
unsafe fn read_unchecked(
&mut self,
output: &mut [Self::Item],
index: usize,
count: usize,
) -> io::Result<usize> {
let read = self.inner.read(&mut output[index..index + count])?;
self.branch.write_fully(&output[index..index + read])?;
Ok(read)
}
}
impl<I, B> Seekable for TeeInput<I, B>
where
I: Seekable,
{
type Unit = I::Unit;
#[inline(always)]
fn seek_to(&mut self, position: SeekFrom) -> io::Result<u64> {
self.inner.seek_to(position)
}
}