Merge branch 'master' into fs-stream-find-map
commit
efb8415429
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct NthFuture<'a, S> {
|
||||
stream: &'a mut S,
|
||||
n: usize,
|
||||
}
|
||||
|
||||
impl<'a, S> NthFuture<'a, S> {
|
||||
pin_utils::unsafe_pinned!(stream: &'a mut S);
|
||||
pin_utils::unsafe_unpinned!(n: usize);
|
||||
|
||||
pub(crate) fn new(stream: &'a mut S, n: usize) -> Self {
|
||||
NthFuture { stream, n }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S> futures_core::future::Future for NthFuture<'a, S>
|
||||
where
|
||||
S: futures_core::stream::Stream + Unpin + Sized,
|
||||
{
|
||||
type Output = Option<S::Item>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
use futures_core::stream::Stream;
|
||||
|
||||
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||
match next {
|
||||
Some(v) => match self.n {
|
||||
0 => Poll::Ready(Some(v)),
|
||||
_ => {
|
||||
*self.as_mut().n() -= 1;
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
},
|
||||
None => Poll::Ready(None),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue