Skip to content
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

Merged
merged 2 commits into from
Sep 6, 2019
Merged

add stream::min_by method #146

merged 2 commits into from
Sep 6, 2019

Conversation

montekki
Copy link
Member

@montekki montekki commented Sep 5, 2019

Implements Stream::min_by.

Not sure if:

  1. Should live in a separate file, but it looks like adding all those methods to stream.rs will bloat at some point.
  2. It can be effectively reused for Stream::min
  3. Other min* and max* should be added to this PR.

@yoshuawuyts
Copy link
Contributor

Stdlib: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.min_by

Ref: #129


  1. I agree. We should do this in a separate PR probably though.
  2. Yay!
  3. Yayay! Though doing these PRs one at the time is also welcome; it allows us to review and land them at a faster pace which might be nice for everyone!

src/stream/stream.rs Outdated Show resolved Hide resolved

impl<S, F> Future for MinBy<S, F>
where
S: futures_core::stream::Stream + Unpin,
Copy link
Contributor

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.

Copy link
Member Author

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 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes indeed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still unresolved?

Copy link
Contributor

@yoshuawuyts yoshuawuyts left a 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 <yoshuawuyts+github@gmail.com>
@montekki
Copy link
Member Author

montekki commented Sep 5, 2019

1. I agree. We should do this in a separate PR probably though.

ok

2. Yay!

Those were questions prefixed by "Not sure if" actually :) So at the point I have no idea about this point

3. Yayay! Though doing these PRs one at the time is also welcome; it allows us to review and land them at a faster pace which might be nice for everyone!

yep, sure

Copy link
Contributor

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks heaps!

@yoshuawuyts yoshuawuyts merged commit 7e3599a into async-rs:master Sep 6, 2019
/// ```
fn min_by<F>(self, compare: F) -> MinBy<Self, F>
where
Self: Sized + Unpin,
Copy link

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> {
Copy link

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.

Copy link

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 :)

Copy link
Contributor

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> {
Copy link

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();
Copy link

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> {
Copy link

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>
Copy link

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.

Copy link
Member Author

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

  1. Futures that take self instead of &mut self and thus should have no lifetime parameters
  2. Streams
    ?

use std::pin::Pin;

use cfg_if::cfg_if;

use super::min_by::MinBy;
Copy link

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).

ghost pushed a commit that referenced this pull request Sep 6, 2019
@ghost
Copy link

ghost commented Sep 6, 2019

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?

@montekki
Copy link
Member Author

montekki commented Sep 6, 2019

yep, sure, Ok to do it in a followup to the #150?

@ghost
Copy link

ghost commented Sep 6, 2019

yeah, sounds good! :)

bors bot added a commit that referenced this pull request Sep 8, 2019
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 <yoshuawuyts@gmail.com>
Co-authored-by: Stjepan Glavina <stjepang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants