Add next_back

split-by-pattern
Felipe Sere 5 years ago
parent 55194edbf7
commit ee2f52f3ce

@ -1,8 +1,10 @@
mod next_back;
mod nth_back; mod nth_back;
mod rfind; mod rfind;
mod rfold; mod rfold;
mod try_rfold; mod try_rfold;
use next_back::NextBackFuture;
use nth_back::NthBackFuture; use nth_back::NthBackFuture;
use rfind::RFindFuture; use rfind::RFindFuture;
use rfold::RFoldFuture; use rfold::RFoldFuture;
@ -28,6 +30,37 @@ extension_trait! {
Something else Something else
"#] "#]
pub trait DoubleEndedStreamExt: crate::stream::DoubleEndedStream { 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<Output = Option<Self::Item>> + '_ [NextBackFuture<'_, Self>]
where
Self: Unpin,
{
NextBackFuture { stream: self }
}
#[doc = r#" #[doc = r#"
Returns the nth element from the back of the stream. Returns the nth element from the back of the stream.

@ -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<T: DoubleEndedStream + Unpin + ?Sized> Future for NextBackFuture<'_, T> {
type Output = Option<T::Item>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut *self.stream).poll_next_back(cx)
}
}
Loading…
Cancel
Save