forked from mirror/async-std
adds stream::fold combinator
This commit is contained in:
parent
6f9ec665a2
commit
5b720ab1e2
2 changed files with 91 additions and 0 deletions
61
src/stream/stream/fold.rs
Normal file
61
src/stream/stream/fold.rs
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use crate::future::Future;
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct FoldFuture<S, F, T, B> {
|
||||||
|
stream: S,
|
||||||
|
f: F,
|
||||||
|
acc: Option<B>,
|
||||||
|
__t: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, T, B> FoldFuture<S, F, T, B> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: S);
|
||||||
|
pin_utils::unsafe_unpinned!(f: F);
|
||||||
|
pin_utils::unsafe_unpinned!(acc: Option<B>);
|
||||||
|
|
||||||
|
pub(super) fn new(stream: S, init: B, f: F) -> Self {
|
||||||
|
FoldFuture {
|
||||||
|
stream,
|
||||||
|
f,
|
||||||
|
acc: Some(init),
|
||||||
|
__t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, B> Future for FoldFuture<S, F, S::Item, B>
|
||||||
|
where
|
||||||
|
S: futures_core::stream::Stream + Unpin + Sized,
|
||||||
|
F: FnMut(B, S::Item) -> B,
|
||||||
|
{
|
||||||
|
type Output = B;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
|
||||||
|
match next {
|
||||||
|
Some(v) => {
|
||||||
|
cx.waker().wake_by_ref();
|
||||||
|
let old = self
|
||||||
|
.as_mut()
|
||||||
|
.acc()
|
||||||
|
.take()
|
||||||
|
.expect("FoldFuture should never contain None");
|
||||||
|
let new = (self.as_mut().f())(old, v);
|
||||||
|
*self.as_mut().acc() = Some(new);
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
None => Poll::Ready(
|
||||||
|
self.as_mut()
|
||||||
|
.acc()
|
||||||
|
.take()
|
||||||
|
.expect("FoldFuture should never contain None"),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,6 +25,7 @@ mod all;
|
||||||
mod any;
|
mod any;
|
||||||
mod filter_map;
|
mod filter_map;
|
||||||
mod find_map;
|
mod find_map;
|
||||||
|
mod fold;
|
||||||
mod min_by;
|
mod min_by;
|
||||||
mod next;
|
mod next;
|
||||||
mod nth;
|
mod nth;
|
||||||
|
@ -36,6 +37,7 @@ use all::AllFuture;
|
||||||
use any::AnyFuture;
|
use any::AnyFuture;
|
||||||
use filter_map::FilterMap;
|
use filter_map::FilterMap;
|
||||||
use find_map::FindMapFuture;
|
use find_map::FindMapFuture;
|
||||||
|
use fold::FoldFuture;
|
||||||
use min_by::MinByFuture;
|
use min_by::MinByFuture;
|
||||||
use next::NextFuture;
|
use next::NextFuture;
|
||||||
use nth::NthFuture;
|
use nth::NthFuture;
|
||||||
|
@ -344,6 +346,34 @@ pub trait Stream {
|
||||||
FindMapFuture::new(self, f)
|
FindMapFuture::new(self, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A combinator that applies a function to every element in a stream
|
||||||
|
/// producing a single, final value.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Basic usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use async_std::prelude::*;
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
///
|
||||||
|
/// let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
|
||||||
|
/// let sum = s.fold(0, |acc, x| acc + x).await;
|
||||||
|
///
|
||||||
|
/// assert_eq!(sum, 6);
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, Self::Item, B>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(B, Self::Item) -> B,
|
||||||
|
{
|
||||||
|
FoldFuture::new(self, init, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Tests if any element of the stream matches a predicate.
|
/// Tests if any element of the stream matches a predicate.
|
||||||
///
|
///
|
||||||
/// `any()` takes a closure that returns `true` or `false`. It applies
|
/// `any()` takes a closure that returns `true` or `false`. It applies
|
||||||
|
|
Loading…
Reference in a new issue