-
Notifications
You must be signed in to change notification settings - Fork 341
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
add stream::min_by method #146
Conversation
Stdlib: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min_by Ref: #129
|
|
||
impl<S, F> Future for MinBy<S, F> | ||
where | ||
S: futures_core::stream::Stream + Unpin, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this bound may need to be updated to just be Stream
as #140 was merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, you probably mean #145 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes indeed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still unresolved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few nits, but overall this looks great! -- thanks so much!
Co-Authored-By: Yoshua Wuyts <[email protected]>
ok
Those were questions prefixed by "Not sure if" actually :) So at the point I have no idea about this point
yep, sure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks heaps!
/// ``` | ||
fn min_by<F>(self, compare: F) -> MinBy<Self, F> | ||
where | ||
Self: Sized + Unpin, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Unpin
bound here is not necessary because we're taking ownership of self
in this method.
|
||
/// A future that yields the minimum item in a stream by a given comparison function. | ||
#[derive(Clone, Debug)] | ||
pub struct MinBy<S: Stream, F> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since MinBy
is here in a separate file, it'd be a good idea to move Take
and others into their own files, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can do that in a follow-up PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #150!
|
||
impl<S: Stream + Unpin, F> Unpin for MinBy<S, F> {} | ||
|
||
impl<S: Stream + Unpin, F> MinBy<S, F> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of requiring Unpin
, we should use unsafe_pinner
like for the Take
struct.
|
||
match next { | ||
Some(new) => { | ||
cx.waker().wake_by_ref(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line needed at all? I believe it can be deleted.
|
||
/// A future that yields the minimum item in a stream by a given comparison function. | ||
#[derive(Clone, Debug)] | ||
pub struct MinBy<S: Stream, F> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a future, it should be named MinByFuture
just like AllFuture
and AnyFuture
.
/// # | ||
/// # }) } | ||
/// ``` | ||
fn min_by<F>(self, compare: F) -> MinBy<Self, F> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type here should use ret!
like in fn any
, for example. This function would ideally be an async fn
if Rust supported that syntax in traits today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is ret!
supposed to work for
- Futures that take
self
instead of&mut self
and thus should have no lifetime parameters - Streams
?
use std::pin::Pin; | ||
|
||
use cfg_if::cfg_if; | ||
|
||
use super::min_by::MinBy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since min_by
should ideally be an async fn
, let's make MinBy
a hidden type and not even implement Debug
(because async fn
desugaring doesn't implement Debug
for its future).
Sorry for taking so long to review this! :( Since there's no way to revert and re-open this PR, could you please address my comment in a follow-up PR? |
yep, sure, Ok to do it in a followup to the #150? |
yeah, sounds good! :) |
149: update deps r=stjepang a=yoshuawuyts Updates all deps. Thanks! 150: split stream into multiple files r=stjepang a=yoshuawuyts This splits `stream/mod.rs`'s combinators into multiple files, making it easier to contribute combinators. Additionally we've renamed `MinBy` to `MinByFuture` to make it the same as the other private futures. Ref #146 #129. Thanks! Co-authored-by: Yoshua Wuyts <[email protected]> Co-authored-by: Stjepan Glavina <[email protected]>
Implements
Stream::min_by
.Not sure if:
stream.rs
will bloat at some point.Stream::min
min*
andmax*
should be added to this PR.