use std::io::{
self,
SeekFrom,
};
use crate::{
Input,
Output,
Seekable,
};
#[must_use]
#[derive(Debug)]
pub struct SyncSeekTeeInput<I, B> {
inner: I,
branch: B,
}
impl<I, B> SyncSeekTeeInput<I, B> {
#[inline(always)]
pub const fn new(inner: I, branch: B) -> Self {
Self { 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 SyncSeekTeeInput<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 SyncSeekTeeInput<I, B>
where
I: Seekable,
B: Seekable<Unit = I::Unit>,
{
type Unit = I::Unit;
#[inline]
fn seek_to(&mut self, position: SeekFrom) -> io::Result<u64> {
let position = self.inner.seek_to(position)?;
self.branch.seek_to(SeekFrom::Start(position))?;
Ok(position)
}
}