-
Notifications
You must be signed in to change notification settings - Fork 340
add stream::min_by_key method #385
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
020eb85
add stream::min_by_key method
yjhmelody f5a0a0b
fmt
yjhmelody c9e6d3a
use pin_project_lite
yjhmelody 5a4fdeb
Update src/stream/stream/min_by_key.rs
yjhmelody fb78ed1
Update src/stream/stream/min_by_key.rs
yjhmelody 7cfec4e
use take and remove Copy
yjhmelody 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,58 @@ | ||
use std::cmp::Ordering; | ||
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 MinByKeyFuture<S, T, K> { | ||
stream: S, | ||
min: Option<T>, | ||
key_by: K, | ||
} | ||
|
||
impl<S, T, K> MinByKeyFuture<S, T, K> { | ||
pin_utils::unsafe_pinned!(stream: S); | ||
pin_utils::unsafe_unpinned!(min: Option<T>); | ||
pin_utils::unsafe_unpinned!(key_by: K); | ||
|
||
pub(super) fn new(stream: S, key_by: K) -> Self { | ||
MinByKeyFuture { | ||
stream, | ||
min: None, | ||
key_by, | ||
} | ||
} | ||
} | ||
|
||
impl<S, K> Future for MinByKeyFuture<S, S::Item, K> | ||
where | ||
S: Stream + Unpin + Sized, | ||
K: FnMut(&S::Item) -> S::Item, | ||
S::Item: Ord + Copy, | ||
yjhmelody marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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) => { | ||
let new = self.as_mut().key_by()(&new); | ||
cx.waker().wake_by_ref(); | ||
match self.as_mut().min().take() { | ||
None => *self.as_mut().min() = Some(new), | ||
|
||
Some(old) => match new.cmp(&old) { | ||
Ordering::Less => *self.as_mut().min() = Some(new), | ||
_ => *self.as_mut().min() = Some(old), | ||
yjhmelody marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
} | ||
Poll::Pending | ||
} | ||
None => Poll::Ready(self.min), | ||
} | ||
} | ||
} |
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
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.