abstract and encapsulate the awkward OptionFuture into Stream pattern
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
25
src/core/utils/future/option_stream.rs
Normal file
25
src/core/utils/future/option_stream.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use futures::{Future, FutureExt, Stream, StreamExt, future::OptionFuture};
|
||||
|
||||
use super::super::IterStream;
|
||||
|
||||
pub trait OptionStream<T> {
|
||||
fn stream(self) -> impl Stream<Item = T> + Send;
|
||||
}
|
||||
|
||||
impl<T, O, S, Fut> OptionStream<T> for OptionFuture<Fut>
|
||||
where
|
||||
Fut: Future<Output = (O, S)> + Send,
|
||||
S: Stream<Item = T> + Send,
|
||||
O: IntoIterator<Item = T> + Send,
|
||||
<O as IntoIterator>::IntoIter: Send,
|
||||
T: Send,
|
||||
{
|
||||
#[inline]
|
||||
fn stream(self) -> impl Stream<Item = T> + Send {
|
||||
self.map(|opt| opt.map(|(curr, next)| curr.into_iter().stream().chain(next)))
|
||||
.map(Option::into_iter)
|
||||
.map(IterStream::stream)
|
||||
.flatten_stream()
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user