azure_speech/
stream_ext.rs1use crate::callback::Callback;
2use core::fmt;
3use core::pin::Pin;
4use core::task::{Context, Poll};
5use pin_project_lite::pin_project;
6use std::future::Future;
7use std::pin::pin;
8use tokio_stream::{Stream, StreamExt as _};
9
10pin_project! {
11#[must_use = "streams do nothing unless polled"]
13 pub struct StopAfter<St, F> {
14 #[pin]
15 stream: St,
16 predicate: F,
17 done: bool,
18 }
19}
20
21impl<St, F> StopAfter<St, F> {
22 pub(super) fn new(stream: St, predicate: F) -> Self {
23 Self {
24 stream,
25 predicate,
26 done: false,
27 }
28 }
29}
30
31impl<St, F> fmt::Debug for StopAfter<St, F>
32where
33 St: fmt::Debug,
34{
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("StopAfter")
37 .field("stream", &self.stream)
38 .field("done", &self.done)
39 .finish()
40 }
41}
42
43impl<St, F> Stream for StopAfter<St, F>
44where
45 St: Stream,
46 F: FnMut(&St::Item) -> bool,
47{
48 type Item = St::Item;
49
50 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
51 if !*self.as_mut().project().done {
52 self.as_mut().project().stream.poll_next(cx).map(|ready| {
53 let ready = ready.map(|item| {
54 if (self.as_mut().project().predicate)(&item) {
55 *self.as_mut().project().done = true;
56 }
57 item
58 });
59 ready
60 })
61 } else {
62 Poll::Ready(None)
63 }
64 }
65
66 fn size_hint(&self) -> (usize, Option<usize>) {
67 if self.done {
68 return (0, Some(0));
69 }
70
71 let (_, upper) = self.stream.size_hint();
72
73 (0, upper)
74 }
75}
76
77pub trait StreamExt: Stream
79where
80 Self: 'static,
81{
82 fn stop_after<F>(self, f: F) -> StopAfter<Self, F>
113 where
114 F: FnMut(&Self::Item) -> bool,
115 Self: Sized,
116 {
117 StopAfter::new(self, f)
118 }
119
120 fn use_callbacks<C>(self, callback: C) -> impl Future<Output = ()>
122 where
123 Self: Sized + Send + Sync,
124 C: Callback<Item = Self::Item> + 'static,
125 {
126 async move {
127 let mut _self = pin!(self);
128 while let Some(event) = _self.next().await {
129 callback.on_event(event).await;
130 }
131 }
132 }
133}
134
135impl<St: ?Sized + 'static> StreamExt for St where St: Stream {}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140 use crate::callback::Callback;
141 use std::sync::{Arc, Mutex};
142
143 struct CollectCallback(Arc<Mutex<Vec<i32>>>);
144
145 impl Callback for CollectCallback {
146 type Item = i32;
147 fn on_event(&self, item: Self::Item) -> impl Future<Output = ()> {
148 let data = self.0.clone();
149 async move {
150 data.lock().unwrap().push(item);
151 }
152 }
153 }
154
155 #[tokio::test]
156 async fn test_stop_after_includes_trigger() {
157 let stream = tokio_stream::iter(1..=5).stop_after(|&x| x >= 3);
158 let collected: Vec<_> = stream.collect().await;
159 assert_eq!(collected, vec![1, 2, 3]);
160 }
161
162 #[tokio::test]
163 async fn test_use_callbacks_collects_items() {
164 let store = Arc::new(Mutex::new(Vec::new()));
165 let cb = CollectCallback(store.clone());
166 tokio_stream::iter(1..=3).use_callbacks(cb).await;
167 assert_eq!(*store.lock().unwrap(), vec![1, 2, 3]);
168 }
169}