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

Update tokio-macros to new builder #2906

Closed
wants to merge 10 commits into from
Closed

Update tokio-macros to new builder #2906

wants to merge 10 commits into from

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Oct 3, 2020

Macro part of #2720 (comment).

Since the builder is not yet fully updated, this still generates code with the old builder syntax, but this is easily changed later. The rt-threaded feature is also not renamed to rt-work-stealing, but this is also easily changed later. Documentation is still a TODO.

@Darksonn Darksonn added C-enhancement Category: A PR with an enhancement or bugfix. A-tokio-macros Area: The tokio-macros crate A-tokio Area: The main tokio crate M-macros Module: macros in the main Tokio crate labels Oct 3, 2020
@Darksonn Darksonn self-assigned this Oct 3, 2020
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

Thanks a ton for getting this started!

"in_place" => Ok(RuntimeFlavor::InPlace),
"work_stealing" => Ok(RuntimeFlavor::WorkStealing),
"single_threaded" => Err(format!("The single threaded runtime flavor is called `in_place`.")),
"basic_scheduler" => Err(format!("The `basic_scheduler` runtime flavor has been renamed to `in_place`.")),
Copy link
Member

Choose a reason for hiding this comment

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

do we need to do this? I'd rather not leave "old" stuff in a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. I added it to ease the transition for users coming from v0.2, and because it wouldn't be a breaking change to remove later.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm this will just generate a compile failure right? Then I think that is fine.

"single_threaded" => Err(format!("The single threaded runtime flavor is called `in_place`.")),
"basic_scheduler" => Err(format!("The `basic_scheduler` runtime flavor has been renamed to `in_place`.")),
"threaded_scheduler" => Err(format!("The `threaded_scheduler` runtime flavor has been renamed to `work_stealing`.")),
_ => Err(format!("No such runtime flavor `{}`.", s)),
Copy link
Member

Choose a reason for hiding this comment

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

Should we list the valid options here?

num_workers: Option<(usize, Span)>,
max_threads: Option<(usize, Span)>,
}
impl Configuration {
Copy link
Member

Choose a reason for hiding this comment

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

Some new lines in this file would be nice 😄

"The `num_workers` option requires the `work_stealing` runtime flavor."
));
}
assert!(self.num_workers.is_none());
Copy link
Member

Choose a reason for hiding this comment

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

can we statically ensure this somehow? Maybe via enum values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could certainly statically ensure it in the FinalConfig, but that wouldn't remove this assert. I suppose we could turn Configuration into a state machine of some sort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though turning Configuration into a state machine is awkward. I chose the current design because it allows e.g. setting num_workers first, then setting the flavor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's one option:

match (flavor, self.num_workers) {
    (CurrentThread, Some((_, num_workers_span))) => {
        Err(syn::Error::new(
            num_workers_span.clone(),
            "The `num_workers` option requires the `threaded` runtime flavor."
        ))
    },
    (CurrentThread, None) => {
        Ok(FinalConfig {
            flavor,
            num_workers: None,
        })
    },
    (Threaded, num_workers) if self.rt_threaded_available => {
        Ok(FinalConfig {
            flavor,
            num_workers: num_workers.map(|(val, _span)| val),
        })
    },
    (Threaded, _) => {
        let msg = if self.flavor.is_none() {
            "The default runtime flavor is `threaded`, but the `rt-threaded` feature is disabled."
        } else {
            "The runtime flavor `threaded` requires the `rt-threaded` feature."
        };
        Err(syn::Error::new(Span::call_site(), msg))
    },
}

(None, Some((_, span))) => {
return Err(syn::Error::new(
span,
"`num_workers` must also be set when using `max_threads`"
Copy link
Member

Choose a reason for hiding this comment

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

not following why this is required?

(Some((num_workers, _)), Some((max_threads, span))) if max_threads <= num_workers => {
return Err(syn::Error::new(
span,
"`max_threads` must be larger than `num_workers`",
Copy link
Member

Choose a reason for hiding this comment

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

In this case, can we just default to max(core_threads, max_threads)? I think that is a better default and I think we should do the same in the builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allowing max_threads == core_threads seems dangerous and like an edge case that I'm not sure we should support from the macro.

Copy link
Member

Choose a reason for hiding this comment

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

we could do max_threads = core_threads + 1 at a min. That said, lets not do this here since I think this could also be a potential footgun. Honestly, should we even allow someone to set max threads here?

},
"core_threads" => {
Copy link
Member

Choose a reason for hiding this comment

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

if we are renaming lets just drop it.

pub fn main_fail(_args: TokenStream, _item: TokenStream) -> TokenStream {
syn::Error::new(
proc_macro2::Span::call_site(),
"The #[tokio::main] macro requires rt-core or rt-threaded.",
Copy link
Member

Choose a reason for hiding this comment

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

should we link to docs here?

@@ -28,7 +28,7 @@ async fn basic_blocking() {
}
}

#[tokio::test(threaded_scheduler)]
#[tokio::test(flavor = "work_stealing")]
Copy link
Member

Choose a reason for hiding this comment

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

personally, I prefer the old method rather than this change.

@LucioFranco
Copy link
Member

@Darksonn looks like some CI errors?

@Darksonn
Copy link
Contributor Author

Darksonn commented Oct 8, 2020

I managed to make it compile.

@carllerche
Copy link
Member

Thanks 👍 I merged this into #2876. I am going to close this PR.

@carllerche carllerche closed this Oct 12, 2020
@Darksonn Darksonn deleted the macro-new-builder branch October 12, 2020 07:41
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 A-tokio-macros Area: The tokio-macros crate C-enhancement Category: A PR with an enhancement or bugfix. M-macros Module: macros in the main Tokio crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants