mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
stream: add StreamExt::then (#4355)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use core::future::Future;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
|
||||||
mod all;
|
mod all;
|
||||||
@@ -39,15 +40,18 @@ use skip::Skip;
|
|||||||
mod skip_while;
|
mod skip_while;
|
||||||
use skip_while::SkipWhile;
|
use skip_while::SkipWhile;
|
||||||
|
|
||||||
mod try_next;
|
|
||||||
use try_next::TryNext;
|
|
||||||
|
|
||||||
mod take;
|
mod take;
|
||||||
use take::Take;
|
use take::Take;
|
||||||
|
|
||||||
mod take_while;
|
mod take_while;
|
||||||
use take_while::TakeWhile;
|
use take_while::TakeWhile;
|
||||||
|
|
||||||
|
mod then;
|
||||||
|
use then::Then;
|
||||||
|
|
||||||
|
mod try_next;
|
||||||
|
use try_next::TryNext;
|
||||||
|
|
||||||
cfg_time! {
|
cfg_time! {
|
||||||
mod timeout;
|
mod timeout;
|
||||||
use timeout::Timeout;
|
use timeout::Timeout;
|
||||||
@@ -197,6 +201,51 @@ pub trait StreamExt: Stream {
|
|||||||
Map::new(self, f)
|
Map::new(self, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Maps this stream's items asynchronously to a different type, returning a
|
||||||
|
/// new stream of the resulting type.
|
||||||
|
///
|
||||||
|
/// The provided closure is executed over all elements of this stream as
|
||||||
|
/// they are made available, and the returned future is executed. Only one
|
||||||
|
/// future is executed at the time.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the stream passed into it and returns a
|
||||||
|
/// wrapped version of it, similar to the existing `then` methods in the
|
||||||
|
/// standard library.
|
||||||
|
///
|
||||||
|
/// Be aware that if the future is not `Unpin`, then neither is the `Stream`
|
||||||
|
/// returned by this method. To handle this, you can use `tokio::pin!` as in
|
||||||
|
/// the example below or put the stream in a `Box` with `Box::pin(stream)`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[tokio::main]
|
||||||
|
/// # async fn main() {
|
||||||
|
/// use tokio_stream::{self as stream, StreamExt};
|
||||||
|
///
|
||||||
|
/// async fn do_async_work(value: i32) -> i32 {
|
||||||
|
/// value + 3
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let stream = stream::iter(1..=3);
|
||||||
|
/// let stream = stream.then(do_async_work);
|
||||||
|
///
|
||||||
|
/// tokio::pin!(stream);
|
||||||
|
///
|
||||||
|
/// assert_eq!(stream.next().await, Some(4));
|
||||||
|
/// assert_eq!(stream.next().await, Some(5));
|
||||||
|
/// assert_eq!(stream.next().await, Some(6));
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
|
||||||
|
where
|
||||||
|
F: FnMut(Self::Item) -> Fut,
|
||||||
|
Fut: Future,
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
Then::new(self, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Combine two streams into one by interleaving the output of both as it
|
/// Combine two streams into one by interleaving the output of both as it
|
||||||
/// is produced.
|
/// is produced.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
use crate::Stream;
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
use core::future::Future;
|
||||||
|
use core::pin::Pin;
|
||||||
|
use core::task::{Context, Poll};
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
|
/// Stream for the [`then`](super::StreamExt::then) method.
|
||||||
|
#[must_use = "streams do nothing unless polled"]
|
||||||
|
pub struct Then<St, Fut, F> {
|
||||||
|
#[pin]
|
||||||
|
stream: St,
|
||||||
|
#[pin]
|
||||||
|
future: Option<Fut>,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, Fut, F> fmt::Debug for Then<St, Fut, F>
|
||||||
|
where
|
||||||
|
St: fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("Then")
|
||||||
|
.field("stream", &self.stream)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, Fut, F> Then<St, Fut, F> {
|
||||||
|
pub(super) fn new(stream: St, f: F) -> Self {
|
||||||
|
Then {
|
||||||
|
stream,
|
||||||
|
future: None,
|
||||||
|
f,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, F, Fut> Stream for Then<St, Fut, F>
|
||||||
|
where
|
||||||
|
St: Stream,
|
||||||
|
Fut: Future,
|
||||||
|
F: FnMut(St::Item) -> Fut,
|
||||||
|
{
|
||||||
|
type Item = Fut::Output;
|
||||||
|
|
||||||
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Fut::Output>> {
|
||||||
|
let mut me = self.project();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Some(future) = me.future.as_mut().as_pin_mut() {
|
||||||
|
match future.poll(cx) {
|
||||||
|
Poll::Ready(item) => {
|
||||||
|
me.future.set(None);
|
||||||
|
return Poll::Ready(Some(item));
|
||||||
|
}
|
||||||
|
Poll::Pending => return Poll::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match me.stream.as_mut().poll_next(cx) {
|
||||||
|
Poll::Ready(Some(item)) => {
|
||||||
|
me.future.set(Some((me.f)(item)));
|
||||||
|
}
|
||||||
|
Poll::Ready(None) => return Poll::Ready(None),
|
||||||
|
Poll::Pending => return Poll::Pending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
let future_len = if self.future.is_some() { 1 } else { 0 };
|
||||||
|
let (lower, upper) = self.stream.size_hint();
|
||||||
|
|
||||||
|
let lower = lower.saturating_add(future_len);
|
||||||
|
let upper = upper.and_then(|upper| upper.checked_add(future_len));
|
||||||
|
|
||||||
|
(lower, upper)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user