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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::pin::Pin;

use futures::{
    task::{Context, Poll},
    {Stream, StreamExt},
};

const DEFAULT_CAPACITY: usize = 1024;

/// A stream combinator aimed at improving the performance of decoder streams under load.
///
/// This is similar in spirit to `StreamExt::ready_chunks`, but built specifically for the
/// particular result tuple returned by decoding streams. The more general `FoldReady` is left as
/// an exercise to the reader.
pub struct ReadyFrames<T, U, E> {
    inner: T,
    enqueued: Vec<U>,
    enqueued_size: usize,
    error_slot: Option<E>,
    enqueued_limit: usize,
}

impl<T, U, E> ReadyFrames<T, U, E>
where
    T: Stream<Item = Result<(U, usize), E>> + Unpin,
    U: Unpin,
    E: Unpin,
{
    /// Create a new `ReadyChunks` by wrapping a decoder stream, most commonly a `FramedRead`.
    pub fn new(inner: T) -> Self {
        Self::with_capacity(inner, DEFAULT_CAPACITY)
    }

    /// Create a new `ReadyChunks` with a specified capacity by wrapping a decoder stream, most
    /// commonly a `FramedRead`.
    ///
    /// The specified capacity is a soft limit, and chunks may be returned that contain more than
    /// that number of items.
    pub fn with_capacity(inner: T, cap: usize) -> Self {
        Self {
            inner,
            enqueued: Vec::with_capacity(cap),
            enqueued_size: 0,
            error_slot: None,
            enqueued_limit: cap,
        }
    }

    /// Returns a reference to the underlying stream.
    pub const fn get_ref(&self) -> &T {
        &self.inner
    }

    /// Returns a mutable reference to the underlying stream.
    pub fn get_mut(&mut self) -> &mut T {
        &mut self.inner
    }

    fn flush(&mut self) -> (Vec<U>, usize) {
        let frames = std::mem::take(&mut self.enqueued);
        let size = self.enqueued_size;
        self.enqueued_size = 0;
        (frames, size)
    }
}

impl<T, U, E> Stream for ReadyFrames<T, U, E>
where
    T: Stream<Item = Result<(U, usize), E>> + Unpin,
    U: Unpin,
    E: Unpin,
{
    type Item = Result<(Vec<U>, usize), E>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if let Some(error) = self.error_slot.take() {
            return Poll::Ready(Some(Err(error)));
        }

        loop {
            match self.inner.poll_next_unpin(cx) {
                Poll::Ready(Some(Ok((frame, size)))) => {
                    self.enqueued.push(frame);
                    self.enqueued_size += size;
                    if self.enqueued.len() >= self.enqueued_limit {
                        return Poll::Ready(Some(Ok(self.flush())));
                    }
                }
                Poll::Ready(Some(Err(error))) => {
                    if self.enqueued.is_empty() {
                        return Poll::Ready(Some(Err(error)));
                    } else {
                        self.error_slot = Some(error);
                        return Poll::Ready(Some(Ok(self.flush())));
                    }
                }
                Poll::Ready(None) => {
                    if !self.enqueued.is_empty() {
                        return Poll::Ready(Some(Ok(self.flush())));
                    } else {
                        return Poll::Ready(None);
                    }
                }
                Poll::Pending => {
                    if !self.enqueued.is_empty() {
                        return Poll::Ready(Some(Ok(self.flush())));
                    } else {
                        return Poll::Pending;
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use futures::{channel::mpsc, poll, task::Poll, SinkExt, StreamExt};

    use super::ReadyFrames;

    #[tokio::test]
    async fn idle_passthrough() {
        let (mut tx, rx) = mpsc::channel::<Result<(&str, usize), &str>>(5);
        let mut rf = ReadyFrames::with_capacity(rx, 2);

        assert_eq!(Poll::Pending, poll!(rf.next()));

        tx.send(Ok(("foo", 1))).await.unwrap();

        assert_eq!(Poll::Ready(Some(Ok((vec!["foo"], 1)))), poll!(rf.next()));
        assert_eq!(Poll::Pending, poll!(rf.next()));
    }

    #[tokio::test]
    async fn limits_to_capacity() {
        let (mut tx, rx) = mpsc::channel::<Result<(&str, usize), &str>>(5);
        let mut rf = ReadyFrames::with_capacity(rx, 2);

        tx.send(Ok(("foo", 2))).await.unwrap();
        tx.send(Ok(("bar", 3))).await.unwrap();

        assert_eq!(
            Poll::Ready(Some(Ok((vec!["foo", "bar"], 5)))),
            poll!(rf.next())
        );
        assert_eq!(Poll::Pending, poll!(rf.next()));

        tx.send(Ok(("foo", 4))).await.unwrap();
        tx.send(Ok(("bar", 5))).await.unwrap();
        tx.send(Ok(("baz", 6))).await.unwrap();

        assert_eq!(
            Poll::Ready(Some(Ok((vec!["foo", "bar"], 9)))),
            poll!(rf.next())
        );
        assert_eq!(Poll::Ready(Some(Ok((vec!["baz"], 6)))), poll!(rf.next()));
        assert_eq!(Poll::Pending, poll!(rf.next()));
    }

    #[tokio::test]
    async fn error_passing() {
        let (mut tx, rx) = mpsc::channel::<Result<(&str, usize), &str>>(5);
        let mut rf = ReadyFrames::with_capacity(rx, 2);

        tx.send(Err("oops")).await.unwrap();

        assert_eq!(Poll::Ready(Some(Err("oops"))), poll!(rf.next()));
        assert_eq!(Poll::Pending, poll!(rf.next()));

        tx.send(Ok(("foo", 7))).await.unwrap();
        tx.send(Err("oops")).await.unwrap();

        assert_eq!(Poll::Ready(Some(Ok((vec!["foo"], 7)))), poll!(rf.next()));
        assert_eq!(Poll::Ready(Some(Err("oops"))), poll!(rf.next()));
        assert_eq!(Poll::Pending, poll!(rf.next()));
    }

    #[tokio::test]
    async fn closing() {
        let (mut tx, rx) = mpsc::channel::<Result<(&str, usize), &str>>(5);
        let mut rf = ReadyFrames::with_capacity(rx, 2);

        tx.send(Ok(("foo", 8))).await.unwrap();
        tx.send(Ok(("bar", 9))).await.unwrap();
        tx.send(Ok(("baz", 10))).await.unwrap();
        drop(tx);

        assert_eq!(
            Poll::Ready(Some(Ok((vec!["foo", "bar"], 17)))),
            poll!(rf.next())
        );
        assert_eq!(Poll::Ready(Some(Ok((vec!["baz"], 10)))), poll!(rf.next()));
        assert_eq!(Poll::Ready(None), poll!(rf.next()));
    }
}