diff --git a/src/stream/double_ended/mod.rs b/src/stream/double_ended/mod.rs index c2372d2..a3dbafd 100644 --- a/src/stream/double_ended/mod.rs +++ b/src/stream/double_ended/mod.rs @@ -1,8 +1,10 @@ +mod next_back; mod nth_back; mod rfind; mod rfold; mod try_rfold; +use next_back::NextBackFuture; use nth_back::NthBackFuture; use rfind::RFindFuture; use rfold::RFoldFuture; @@ -28,6 +30,37 @@ extension_trait! { Something else "#] pub trait DoubleEndedStreamExt: crate::stream::DoubleEndedStream { + #[doc = r#" + Advances the stream and returns the next value. + + Returns [`None`] when iteration is finished. Individual stream implementations may + choose to resume iteration, and so calling `next()` again may or may not eventually + start returning more values. + + [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None + + # Examples + + ``` + # fn main() { async_std::task::block_on(async { + # + use async_std::stream::Sample; + use async_std::stream::double_ended::DoubleEndedStreamExt; + + let mut s = Sample::from(vec![7u8]); + + assert_eq!(s.next().await, Some(7)); + assert_eq!(s.next().await, None); + # + # }) } + ``` + "#] + fn next(&mut self) -> impl Future> + '_ [NextBackFuture<'_, Self>] + where + Self: Unpin, + { + NextBackFuture { stream: self } + } #[doc = r#" Returns the nth element from the back of the stream. diff --git a/src/stream/double_ended/next_back.rs b/src/stream/double_ended/next_back.rs new file mode 100644 index 0000000..aa642d0 --- /dev/null +++ b/src/stream/double_ended/next_back.rs @@ -0,0 +1,19 @@ +use std::pin::Pin; +use std::future::Future; + +use crate::stream::DoubleEndedStream; +use crate::task::{Context, Poll}; + +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct NextBackFuture<'a, T: Unpin + ?Sized> { + pub(crate) stream: &'a mut T, +} + +impl Future for NextBackFuture<'_, T> { + type Output = Option; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + Pin::new(&mut *self.stream).poll_next_back(cx) + } +}