Skip to content

add Stream::last #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
9 commits merged into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/stream/stream/last.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::pin::Pin;

use crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll};

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct LastFuture<S, T> {
stream: S,
last: Option<T>,
}

impl<S, T> LastFuture<S, T> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(last: Option<T>);

pub(crate) fn new(stream: S) -> Self {
LastFuture { stream, last: None }
}
}

impl<S> Future for LastFuture<S, S::Item>
where
S: Stream + Unpin + Sized,
S::Item: Copy,
{
type Output = Option<S::Item>;

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(new) => {
cx.waker().wake_by_ref();
*self.as_mut().last() = Some(new);
Poll::Pending
}
None => Poll::Ready(self.last),
}
}
}
50 changes: 50 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod fuse;
mod ge;
mod gt;
mod inspect;
mod last;
mod le;
mod lt;
mod map;
Expand Down Expand Up @@ -64,6 +65,7 @@ use fold::FoldFuture;
use for_each::ForEachFuture;
use ge::GeFuture;
use gt::GtFuture;
use last::LastFuture;
use le::LeFuture;
use lt::LtFuture;
use min_by::MinByFuture;
Expand Down Expand Up @@ -456,6 +458,54 @@ extension_trait! {
Inspect::new(self, f)
}

#[doc = r#"
Returns the last element of the stream.

# Examples

Basic usage:

```
# fn main() { async_std::task::block_on(async {
#
use std::collections::VecDeque;

use async_std::prelude::*;

let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();

let last = s.last().await;
assert_eq!(last, Some(3));
#
# }) }
```

An empty stream will return `None:
```
# fn main() { async_std::task::block_on(async {
#
use std::collections::VecDeque;

use async_std::prelude::*;

let s: VecDeque<usize> = vec![].into_iter().collect();

let last = s.last().await;
assert_eq!(last, None);
#
# }) }
```

"#]
fn last(
self,
) -> impl Future<Output = Option<Self::Item>> [LastFuture<Self, Self::Item>]
where
Self: Sized,
{
LastFuture::new(self)
}

#[doc = r#"
Transforms this `Stream` into a "fused" `Stream` such that after the first time
`poll` returns `Poll::Ready(None)`, all future calls to `poll` will also return
Expand Down