use crate::{Operator, TickValue, Tickable};
use arrayvec::ArrayVec;
#[derive(Debug, Clone)]
pub struct Array<I, P, const LEN: usize> {
ops: ArrayVec<P, LEN>,
_input: core::marker::PhantomData<fn() -> I>,
}
impl<I, T, P, const LEN: usize> Operator<I> for Array<I, P, LEN>
where
I: Tickable<Value = [T; LEN]>,
P: Operator<TickValue<T>>,
P::Output: Tickable,
{
type Output = TickValue<[<P::Output as Tickable>::Value; LEN]>;
fn next(&mut self, input: I) -> Self::Output {
let TickValue { tick, value } = input.into_tick_value();
let mut idx = 0;
let res = value.map(|x| {
let x = TickValue { tick, value: x };
let y = self.ops[idx].next(x).into_tick_value().value;
idx += 1;
y
});
TickValue { tick, value: res }
}
}
pub fn array_t<F, I, T, P, const LEN: usize>(mut f: F) -> Array<I, P, LEN>
where
I: Tickable<Value = [T; LEN]>,
P: Operator<TickValue<T>>,
P::Output: Tickable,
F: FnMut() -> P,
{
let mut ops = ArrayVec::new();
for _ in 0..LEN {
ops.push((f)())
}
Array {
ops,
_input: core::marker::PhantomData,
}
}