-
Notifications
You must be signed in to change notification settings - Fork 340
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
Merged
add Stream::last #347
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
504ef07
add stream::LastFuture (not compiling)
starsheriff 041abb4
Merge branch 'master' into stream/last
starsheriff d120c58
fix type signature -> still cannot assign
starsheriff 54aaf29
remove unused bound
starsheriff 5055821
add doctest
starsheriff cd5e439
unpin LastFuture.last
starsheriff 5445b2b
RustFmt
starsheriff 6b9043a
add static lifetime
starsheriff 4b60659
remove redundant lifetime
starsheriff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,6 +36,7 @@ mod fuse; | |||||
mod ge; | ||||||
mod gt; | ||||||
mod inspect; | ||||||
mod last; | ||||||
mod le; | ||||||
mod lt; | ||||||
mod map; | ||||||
|
@@ -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; | ||||||
|
@@ -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>> + 'static [LastFuture<Self, Self::Item>] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
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 | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.