Merge pull request #132 from shekohex/stream-all-method

Stream::all implementation
pull/136/head
James Munns 5 years ago committed by GitHub
commit e99eafe64f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,6 +27,7 @@ use cfg_if::cfg_if;
use crate::future::Future; use crate::future::Future;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
use std::marker::PhantomData;
cfg_if! { cfg_if! {
if #[cfg(feature = "docs")] { if #[cfg(feature = "docs")] {
@ -111,6 +112,63 @@ pub trait Stream {
remaining: n, remaining: n,
} }
} }
/// Tests if every element of the stream matches a predicate.
///
/// `all()` takes a closure that returns `true` or `false`. It applies
/// this closure to each element of the stream, and if they all return
/// `true`, then so does `all()`. If any of them return `false`, it
/// returns `false`.
///
/// `all()` is short-circuiting; in other words, it will stop processing
/// as soon as it finds a `false`, given that no matter what else happens,
/// the result will also be `false`.
///
/// An empty stream returns `true`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::stream;
///
/// let mut s = stream::repeat::<u32>(42).take(3);
/// assert!(s.all(|x| x == 42).await);
///
/// #
/// # }) }
/// ```
///
/// Empty stream:
///
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use async_std::stream;
///
/// let mut s = stream::empty::<u32>();
/// assert!(s.all(|_| false).await);
/// #
/// # }) }
/// ```
#[inline]
fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F, Self::Item>
where
Self: Sized,
F: FnMut(Self::Item) -> bool,
{
AllFuture {
stream: self,
result: true,
__item: PhantomData,
f,
}
}
} }
impl<T: futures::Stream + Unpin + ?Sized> Stream for T { impl<T: futures::Stream + Unpin + ?Sized> Stream for T {
@ -168,3 +226,50 @@ impl<S: futures::Stream> futures::Stream for Take<S> {
} }
} }
} }
#[derive(Debug)]
pub struct AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
stream: &'a mut S,
f: F,
result: bool,
__item: PhantomData<T>,
}
impl<'a, S, F, T> AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(result: bool);
pin_utils::unsafe_unpinned!(f: F);
}
impl<S, F> Future for AllFuture<'_, S, F, S::Item>
where
S: futures::Stream + Unpin + Sized,
F: FnMut(S::Item) -> bool,
{
type Output = bool;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures::Stream;
let next = futures::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(v) => {
let result = (self.as_mut().f())(v);
*self.as_mut().result() = result;
if result {
// don't forget to wake this task again to pull the next item from stream
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(false)
}
}
None => Poll::Ready(self.result),
}
}
}

Loading…
Cancel
Save