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

tokio::select! with only an "else branch" #6338

Closed
GilShoshan94 opened this issue Feb 11, 2024 · 1 comment · Fixed by #6339
Closed

tokio::select! with only an "else branch" #6338

GilShoshan94 opened this issue Feb 11, 2024 · 1 comment · Fixed by #6339
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-macros Module: macros in the main Tokio crate

Comments

@GilShoshan94
Copy link
Sponsor Contributor

Hi, I wrote a procedural macro and in it I use a tokio::select! where the amount of branches are dependent on the proc macro's input.
I always have the falback else branch.

In my use case, my proc macro uses the user function signature as input and the amount of arguments will determine the amount of branches in the tokio::select! call.

And it is totally valid to have a no argment function that will translate in a select! with 0 branch and only the else fallback. as such:

...
let foo = tokio::select! {
   else => 1,
};
...

But it doesn't compile.

Describe the solution you'd like

A clear and concise description of what you want to happen.
I would like this edge case to be valid. While I understand that this is not very useful by itself, we are talking here in the context of proc macro code.
Also, the else branch exists as a fallback for when all branches are disabled. And no branches at all is kinda equivalent to all branches disabled.

Describe alternatives you've considered

  • Alternative 1: I write my proc macro a bit more complicate and take into account the special case when I have 0 branches and skip the tokio::select! altogether.

  • Alternative 2: I added a permanent dummy branch always disabled (with a never type ! return type) in the expension of my proc macro. As such:

...
let foo = tokio::select! {
   _ = async {} , if false => unreachable!(),
   else => 1,
};
...
  • Alternative 3: a PR to tokio to enhance the select! macro, the simplest is basically to implement "Alternative 2" directly in the macro_rules!.

  • Alternative 4: a PR to tokio to enhance the select! macro but done more elegantly than "Alternative 3" but will required more complex changes in the macro and suporting files such tokio\tokio-macros\src\select.rs (because here it doesn't work I think when variants is an empty Vec).

Additional notes
I already implemented Alternative 3 and add 2 tests to cover it in tokio\tokio\tests\macros_select.rs.
I am ready to submit the PR if it is desired by the Tokio team.

Thank you.

@GilShoshan94 GilShoshan94 added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Feb 11, 2024
@Darksonn Darksonn added the M-macros Module: macros in the main Tokio crate label Feb 11, 2024
@GilShoshan94
Copy link
Sponsor Contributor Author

Alternative 5, as suggested by @Darksonn : PR that add a simple rule that expand to to the $else exp immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-macros Module: macros in the main Tokio crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants