use crate::simulation::stream::*;
use dvcompute_utils::grc::Grc;
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct StreamFn<T> where T: 'static {
gen: Grc<Box<dyn Fn() -> Stream<T>>>
}
impl<T> StreamFn<T> {
#[inline]
pub fn new<F>(f: F) -> Self
where F: Fn() -> Stream<T> + 'static
{
StreamFn {
gen: Grc::new(Box::new(move || { f() }))
}
}
#[inline]
pub fn next(&self) -> Stream<T> {
(self.gen)()
}
pub fn take(&self, n: isize) -> Self
where T: 'static
{
let gen = self.gen.clone();
StreamFn {
gen: Grc::new(Box::new(move || { gen().take(n) }))
}
}
pub fn drop(&self, n: isize) -> Self
where T: 'static
{
let gen = self.gen.clone();
StreamFn {
gen: Grc::new(Box::new(move || { gen().take(n) }))
}
}
}