forked from mirror/async-std
Merge #163
163: adds stream::filter_map combinator r=yoshuawuyts a=montekki Implements a `flat_map` combinator. Currently the same about `ret!` as in #162 . Also the naming should probably be `FilterMapStream`, but in that case `Take` stream should also change it's name i guess. Stdlib: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.flat_map Ref: #129 Co-authored-by: Fedor Sakharov <fedor.sakharov@gmail.com>staging
commit
6d1e71fb68
@ -0,0 +1,48 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
/// A stream that both filters and maps.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FilterMap<S, F, T, B> {
|
||||
stream: S,
|
||||
f: F,
|
||||
__from: PhantomData<T>,
|
||||
__to: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<S, F, T, B> FilterMap<S, F, T, B> {
|
||||
pin_utils::unsafe_pinned!(stream: S);
|
||||
pin_utils::unsafe_unpinned!(f: F);
|
||||
|
||||
pub(crate) fn new(stream: S, f: F) -> Self {
|
||||
FilterMap {
|
||||
stream,
|
||||
f,
|
||||
__from: PhantomData,
|
||||
__to: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F, B> futures_core::stream::Stream for FilterMap<S, F, S::Item, B>
|
||||
where
|
||||
S: futures_core::stream::Stream,
|
||||
F: FnMut(S::Item) -> Option<B>,
|
||||
{
|
||||
type Item = B;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||
match next {
|
||||
Some(v) => match (self.as_mut().f())(v) {
|
||||
Some(b) => Poll::Ready(Some(b)),
|
||||
None => {
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
},
|
||||
None => Poll::Ready(None),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue