use std::mem;
use crate::anyhow::Result;
use crate::runtime::Block;
use crate::runtime::BlockMeta;
use crate::runtime::BlockMetaBuilder;
use crate::runtime::Kernel;
use crate::runtime::MessageIo;
use crate::runtime::MessageIoBuilder;
use crate::runtime::StreamIo;
use crate::runtime::StreamIoBuilder;
use crate::runtime::WorkIo;
#[allow(clippy::type_complexity)]
pub struct ApplyNM<F, A, B, const N: usize, const M: usize>
where
F: FnMut(&[A], &mut [B]) + Send + 'static,
A: Send + 'static,
B: Send + 'static,
{
f: F,
_p1: std::marker::PhantomData<A>,
_p2: std::marker::PhantomData<B>,
}
impl<F, A, B, const N: usize, const M: usize> ApplyNM<F, A, B, N, M>
where
F: FnMut(&[A], &mut [B]) + Send + 'static,
A: Send + 'static,
B: Send + 'static,
{
pub fn new(f: F) -> Block {
Block::new(
BlockMetaBuilder::new(format!("ApplyNM {} {}", N, M)).build(),
StreamIoBuilder::new()
.add_input("in", mem::size_of::<A>())
.add_output("out", mem::size_of::<B>())
.build(),
MessageIoBuilder::<Self>::new().build(),
ApplyNM {
f,
_p1: std::marker::PhantomData,
_p2: std::marker::PhantomData,
},
)
}
}
#[doc(hidden)]
#[async_trait]
impl<F, A, B, const N: usize, const M: usize> Kernel for ApplyNM<F, A, B, N, M>
where
F: FnMut(&[A], &mut [B]) + Send + 'static,
A: Send + 'static,
B: Send + 'static,
{
async fn work(
&mut self,
io: &mut WorkIo,
sio: &mut StreamIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
) -> Result<()> {
let i = sio.input(0).slice::<A>();
let o = sio.output(0).slice::<B>();
let m = std::cmp::min(i.len() / N, o.len() / M);
if m > 0 {
for (v, r) in i.chunks_exact(N).zip(o.chunks_exact_mut(M)) {
(self.f)(v, r);
}
sio.input(0).consume(N * m);
sio.output(0).produce(M * m);
}
if sio.input(0).finished() && m == i.len() {
io.finished = true;
}
Ok(())
}
}