1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Stream windows build on top of async streams.
//! Implementations offered:
//! - tumbling window, waits till window of specified size is filled, emits, and starts a new window
//! - sliding window, waits till window of specified size is filled, emits, and slides down by one element
//! - periodic window, waits till window of specified size is filled, emits, and starts a new window on a clock tick

use async_stream::stream;
use futures::StreamExt;
use std::pin::Pin;
use tokio::select;
use tokio_stream::Stream;

/// Tumbling window, waits till window of specified size is filled, emits, and starts a new window.
pub fn to_tumbling_window<'a, T: Clone + 'a>(
    mut stream: impl Stream<Item = T> + Unpin + 'a,
    window_size: usize,
) -> impl Stream<Item = Vec<T>> + 'a {
    let mut buffer = Vec::with_capacity(window_size);
    stream! {
        while let Some(element) = stream.next().await {
            buffer.push(element);
            if buffer.len() == window_size {
                yield core::mem::take(&mut buffer);
            }
        }
    }
}

/// Sliding window, waits till window of specified size is filled, emits, and slides down by one element.
pub fn to_sliding_window<'a, T: Clone + 'a>(
    mut stream: impl Stream<Item = T> + Unpin + 'a,
    window_size: usize,
) -> impl Stream<Item = Vec<T>> + 'a {
    let mut buffer = Vec::with_capacity(window_size);
    stream! {
        while let Some(element) = stream.next().await {
            buffer.push(element);
            if buffer.len() == window_size {
                yield buffer.clone();
                // slide down
                buffer.remove(0);
            }
        }
    }
}

/// Periodic window, waits till window of specified size is filled, emits, and starts a new window on a clock tick.
pub fn to_periodic_window<'a, T: 'a, CT>(
    mut stream: impl Stream<Item = T> + Unpin + 'a,
    mut clock_stream: impl Stream<Item = CT> + Unpin + 'a,
    emit_last: bool,
) -> impl Stream<Item = Vec<T>> + 'a {
    let mut buffer = vec![];
    stream! {
        loop {
            select! {
                biased;

                _ = clock_stream.next() => {
                    yield std::mem::take(&mut buffer)
                }

                element = stream.next() => {
                    let Some(element) = element else {
                        if emit_last {
                            yield std::mem::take(&mut buffer)
                        }
                        break;
                    };

                    buffer.push(element);
                }
            }
        }
    }
}

/// Wrappers for actual window implementations, as an extension trait.
pub trait WindowExt: Stream {
    fn tumbling_window<'a>(
        self,
        window_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Unpin + Sized + 'a,
        Self::Item: Clone,
    {
        to_tumbling_window(self, window_size).boxed_local()
    }

    fn tumbling_window_unpin<'a>(
        self,
        window_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Sized + 'a,
        Self::Item: Clone,
    {
        to_tumbling_window(Box::pin(self), window_size).boxed_local()
    }

    fn sliding_window<'a>(
        self,
        window_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Unpin + Sized + 'a,
        Self::Item: Clone,
    {
        to_sliding_window(self, window_size).boxed_local()
    }

    fn sliding_window_unpin<'a>(
        self,
        window_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Sized + 'a,
        Self::Item: Clone,
    {
        to_sliding_window(Box::pin(self), window_size).boxed_local()
    }

    fn periodic_window<'a, CT>(
        self,
        clock_stream: impl Stream<Item = CT> + Unpin + 'a,
        emit_last: bool,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Unpin + Sized + 'a,
    {
        to_periodic_window(self, clock_stream, emit_last).boxed_local()
    }

    fn periodic_window_unpin<'a, CT>(
        self,
        clock_stream: impl Stream<Item = CT> + 'a,
        emit_last: bool,
    ) -> Pin<Box<dyn Stream<Item = Vec<Self::Item>> + 'a>>
    where
        Self: Sized + 'a,
    {
        to_periodic_window(Box::pin(self), Box::pin(clock_stream), emit_last).boxed_local()
    }
}

impl<S> WindowExt for S where S: Stream {}

#[cfg(test)]
mod tests;