use crate::Sample;
use crate::stream::{ReadStream, WriteStream};
#[derive(rustradio_macros::Block)]
#[rustradio(crate, new, sync)]
pub struct Tee<T: Sample> {
#[rustradio(in)]
src: ReadStream<T>,
#[rustradio(out)]
dst1: WriteStream<T>,
#[rustradio(out)]
dst2: WriteStream<T>,
}
impl<T: Sample> Tee<T> {
fn process_sync(&self, s: T) -> (T, T) {
(s, s)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::borrow::Cow;
use crate::block::{Block, BlockRet};
use crate::blocks::{VectorSink, VectorSource};
use crate::stream::Tag;
use crate::{Float, Result};
#[derive(rustradio_macros::Block)]
#[rustradio(crate, new, sync_tag)]
struct Tee<T: Sample> {
#[rustradio(in)]
src: ReadStream<T>,
#[rustradio(out)]
dst1: WriteStream<T>,
#[rustradio(out)]
dst2: WriteStream<T>,
}
impl<T: Sample> Tee<T> {
fn process_sync_tags<'a>(
&self,
s: T,
ts: &'a [Tag],
) -> (T, Cow<'a, [Tag]>, T, Cow<'a, [Tag]>) {
(s, Cow::Borrowed(ts), s, Cow::Owned(vec![]))
}
}
#[test]
fn simple() -> Result<()> {
let samps: Vec<_> = (0..10).map(|i| i as Float).collect();
let (mut iblock, i) = VectorSource::new(samps.clone());
iblock.work()?;
let (mut tee, out1, out2) = Tee::new(i);
let ret = tee.work()?;
assert!(matches![ret, BlockRet::WaitForStream(_, 1)], "{ret:?}");
{
let mut o = VectorSink::new(out1, 100);
o.work()?;
assert_eq!(o.hook().data().samples(), samps);
}
{
let mut o = VectorSink::new(out2, 100);
o.work()?;
assert_eq!(o.hook().data().samples(), samps);
}
Ok(())
}
}