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

Replace select!/try_select! with Future::{race,try_race} #405

Merged
11 commits merged into from
Nov 2, 2019
Merged

Conversation

yoshuawuyts
Copy link
Contributor

@yoshuawuyts yoshuawuyts commented Oct 28, 2019

This changes the select and try_select macros into methods on Future. Resolves #387. Thanks!

Motivation

The output of join and try_join is polymorphic (n-sized tuple) which requires a macro. select and try_select don't require polymorphism which makes more sense for it to be a method.

Docs issues

Currently the only problem with this patch is that, uhh, the docs no longer build. There's something in the macro that's not working, and I'm not sure what. cc/ @stjepang maybe you have a clue?

Future work

I think we should consider adding join and try_join as methods too, in addition to the existing macros. That way there's polymorphic + non-polymorphic versions of both available, and the APIs become symmetrical again for the non-polymorphic case. Besides, it seems likely that joining 2 futures is the most common case, which can be a nice shorthand.

Screenshot

Screenshot_2019-10-28 async_std future - Rust

@yoshuawuyts yoshuawuyts added the enhancement New feature or request label Oct 28, 2019
@yoshuawuyts
Copy link
Contributor Author

okay yay, got the docs to compile! -- needed to set the params to std::future::Future. While not great, at least it works! 😁

@yoshuawuyts yoshuawuyts requested a review from a user October 28, 2019 23:23
@yoshuawuyts yoshuawuyts added the api design Open design questions label Oct 28, 2019
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

I like having select as a function rather than macro.

This reminds me of cmp::min()/cmp::max()/Ord::min()/Ord::max(). Those functions take two parameters even though sometimes we want to take a minimum/maximum of more than two things. They work fine in practice.

I wonder what you think about having select and try_select as methods on FutureExt versus standalone functions in the future module. We currently have future::timeout() function that could've been Future::timeout() method.

Looking at the methods on Iterator and functions in the std::iter module, it seems to me some of the rules are this:

  • Iterator constructors go into the std::iter module.
  • Iterator transformers and consumers are methods on the Iterator trait.

However, since there are both cmp::min() and Ord::min(), it seems there is also a third rule:

  • If a.foo(b) and b.foo(a) are equivalent, then foo is handy as both a standalone function and as a trait method.

By that logic, select should indeed be a trait method, although probably it wouldn't hurt to also have it as a standalone function. But I'm not insisting, just thinking out loud. Not sure what to make of this :)

Leaving down here just a few minor comments, otherwise 👍

@yoshuawuyts
Copy link
Contributor Author

By that logic, select should indeed be a trait method, although probably it wouldn't hurt to also have it as a standalone function. But I'm not insisting, just thinking out loud. Not sure what to make of this :)

I think that's quite reasonable! Especially for join / try_join. But since they're macros, I wonder if select / try_select should also be macros? This is a bit tricky because there's not really any precedent in the stdlib for this either. Good questions tho; not sure either!

@yoshuawuyts
Copy link
Contributor Author

Implemented all feedback!

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the feedback! Feel free to merge when CI passes. Looks like Clippy is broken again :(

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
@yoshuawuyts yoshuawuyts changed the title Unmacro select Replace select!/try_select! with Future::{race,try_race} Nov 1, 2019
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
@yoshuawuyts yoshuawuyts force-pushed the unmacro-select branch 2 times, most recently from fb376ac to 1dc2057 Compare November 1, 2019 01:01
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
@yoshuawuyts
Copy link
Contributor Author

Tests are currently broken, filed #431 to fix it.

@ghost ghost merged commit 735fa69 into master Nov 2, 2019
@ghost ghost deleted the unmacro-select branch November 2, 2019 22:00
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api design Open design questions enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not use async block or async fn in async_std::future::select!.
1 participant