//! Lazy streams (`packages/zql/src/ivm/stream.ts`). //! //! JS streams are generators, whose bodies start running on the first //! `next()`. [`lazy`] matches that: its closure runs when the first element //! is requested, not when the stream is created. Pipelines rely on this, //! because a fetch reads operator and source state when it starts. //! //! The JS `'yield'` tokens, which only hand control back to a scheduler, have //! no counterpart: the Rust engine hydrates synchronously. use super::Node; /// A stream of values, pulled once. pub type Stream = Box>; /// A stream of nodes: what a fetch returns. pub type NodeStream = Stream; /// A stream whose elements `make` produces, called on the first `next()`. pub fn lazy(make: F) -> Stream where T: 'static, I: IntoIterator, I::IntoIter: 'static, F: FnOnce() -> I + 'static, { Box::new(Lazy { make: Some(make), iter: None, }) } /// A stream of `elements`. pub fn from_vec(elements: Vec) -> Stream { Box::new(elements.into_iter()) } /// A stream with no elements. pub fn empty() -> Stream { Box::new(std::iter::empty()) } struct Lazy { make: Option, iter: Option, } impl Iterator for Lazy where I: IntoIterator, F: FnOnce() -> I, { type Item = T; fn next(&mut self) -> Option { if let Some(make) = self.make.take() { self.iter = Some(make().into_iter()); } self.iter.as_mut()?.next() } } #[cfg(test)] mod tests { use super::*; use std::cell::Cell; use std::rc::Rc; #[test] fn lazy_runs_on_the_first_next() { let ran = Rc::new(Cell::new(false)); let r = ran.clone(); let mut s = lazy(move || { r.set(true); vec![1, 2] }); assert!(!ran.get()); assert_eq!(s.next(), Some(1)); assert!(ran.get()); assert_eq!(s.collect::>(), vec![2]); } }