Skip to main content

zero_client/ivm/
stream.rs

1//! Lazy streams (`packages/zql/src/ivm/stream.ts`).
2//!
3//! JS streams are generators, whose bodies start running on the first
4//! `next()`. [`lazy`] matches that: its closure runs when the first element
5//! is requested, not when the stream is created. Pipelines rely on this,
6//! because a fetch reads operator and source state when it starts.
7//!
8//! The JS `'yield'` tokens, which only hand control back to a scheduler, have
9//! no counterpart: the Rust engine hydrates synchronously.
10
11use super::Node;
12
13/// A stream of values, pulled once.
14pub type Stream<T> = Box<dyn Iterator<Item = T>>;
15
16/// A stream of nodes: what a fetch returns.
17pub type NodeStream = Stream<Node>;
18
19/// A stream whose elements `make` produces, called on the first `next()`.
20pub fn lazy<T, I, F>(make: F) -> Stream<T>
21where
22    T: 'static,
23    I: IntoIterator<Item = T>,
24    I::IntoIter: 'static,
25    F: FnOnce() -> I + 'static,
26{
27    Box::new(Lazy {
28        make: Some(make),
29        iter: None,
30    })
31}
32
33/// A stream of `elements`.
34pub fn from_vec<T: 'static>(elements: Vec<T>) -> Stream<T> {
35    Box::new(elements.into_iter())
36}
37
38/// A stream with no elements.
39pub fn empty<T: 'static>() -> Stream<T> {
40    Box::new(std::iter::empty())
41}
42
43struct Lazy<F, I> {
44    make: Option<F>,
45    iter: Option<I>,
46}
47
48impl<T, I, F> Iterator for Lazy<F, I::IntoIter>
49where
50    I: IntoIterator<Item = T>,
51    F: FnOnce() -> I,
52{
53    type Item = T;
54
55    fn next(&mut self) -> Option<T> {
56        if let Some(make) = self.make.take() {
57            self.iter = Some(make().into_iter());
58        }
59        self.iter.as_mut()?.next()
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use std::cell::Cell;
67    use std::rc::Rc;
68
69    #[test]
70    fn lazy_runs_on_the_first_next() {
71        let ran = Rc::new(Cell::new(false));
72        let r = ran.clone();
73        let mut s = lazy(move || {
74            r.set(true);
75            vec![1, 2]
76        });
77        assert!(!ran.get());
78        assert_eq!(s.next(), Some(1));
79        assert!(ran.get());
80        assert_eq!(s.collect::<Vec<_>>(), vec![2]);
81    }
82}