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

Format tokio::select! with rustfmt #5441

Closed
dbrgn opened this issue Feb 9, 2023 · 6 comments
Closed

Format tokio::select! with rustfmt #5441

dbrgn opened this issue Feb 9, 2023 · 6 comments
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

@dbrgn
Copy link
Contributor

dbrgn commented Feb 9, 2023

Is your feature request related to a problem? Please describe.

The tokio::select! macro is something used quite often in real-world code. However, rustfmt cannot format code inside that call.

Describe the solution you'd like

Maybe it would be possible to adjust the macro so that the code can be formatted with rustfmt?

I found a project here which plays with alternative syntax to enable this. (Pinging @jkelleyrtp)

Describe alternatives you've considered

Alternatively, maybe rustfmt could be changed to support the current syntax? Not sure if that's possible, since macros can have quite diverse syntax.

Related issue: rust-lang/rustfmt#8

@dbrgn dbrgn added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Feb 9, 2023
@Darksonn Darksonn added the M-macros Module: macros in the main Tokio crate label Feb 9, 2023
@Darksonn
Copy link
Contributor

Darksonn commented Feb 9, 2023

Although this would be very nice, we cannot change the syntax used by the macro.

@Darksonn
Copy link
Contributor

I'm going to close this. As it currently stands, this is not actionable for us. Feel free to reopen or open a new issue if a change in rustfmt makes this possible in the future.

@Darksonn Darksonn closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2023
@dbrgn
Copy link
Contributor Author

dbrgn commented Feb 12, 2023

The best workaround is probably to move the branch code into a standalone function, and call it from the macro. That way, the function can be formatted.

@Darksonn
Copy link
Contributor

I'm not sure I understand your suggestion.

@dbrgn
Copy link
Contributor Author

dbrgn commented Feb 12, 2023

The problem is that in this example:

    tokio::select! {
        val = rx1 => {
            println!("rx1 completed first with {:?}", val);
            // do some other stuff with rx1
        }
        val = rx2 => {
            println!("rx2 completed first with {:?}", val);
            // do some other stuff with rx2
        }
    }

...the code in the branches is not formatted by rustfmt, because it is inside the select! macro.

By moving the code into separate functions outside the macro:

    tokio::select! {
        val = rx1 => {
            println!("rx1 completed first with {:?}", val);
            handle_rx1(val)
        }
        val = rx2 => {
            println!("rx2 completed first with {:?}", val);
            handle_rx2(val)
        }
    }

    fn handle_rx1(val: ...): void {
        // do some stuff with rx1
    }

    fn handle_rx2(val: ...): void {
        // do some stuff with rx2
    }

...the functions can be formatted correctly.

@lfpraca
Copy link

lfpraca commented Sep 7, 2023

I found that a decent solution is to put #[rustfmt::skip] on top of the tokio::select! macro and format the code inside the macro using functionality from a code editor

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

No branches or pull requests

3 participants