-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
macros: add "local" runtime flavor #7375
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
base: master
Are you sure you want to change the base?
Conversation
Can you please make sure that CI passes? There's a rustfmt failure. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the delay in review.
let msg = format!( | ||
"The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[{}(flavor = \"multi_thread\")]`", | ||
self.macro_name(), | ||
); | ||
return Err(syn::Error::new(worker_threads_span, msg)); | ||
} | ||
(F::CurrentThread, None) => None, | ||
(F::CurrentThread | F::Local, None) => None, | ||
(F::Threaded, worker_threads) if self.rt_multi_thread_available => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add logic to rt_multi_thread_available
to detect whether --cfg tokio_unstable
is set and emit a hard error if it is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this comment. Did you mean to add the logic to RuntimeFlavor::from_str
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error should be similar to this error:
tokio/tokio-macros/src/entry.rs
Lines 193 to 197 in 2506c9f
let msg = if self.flavor.is_none() { | |
"The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled." | |
} else { | |
"The runtime flavor `multi_thread` requires the `rt-multi-thread` feature." | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you meant a0ca77e; if I've misinterpreted please feel free to suggest changes :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I just saw your comment. Is 4363203 more what you had in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both are okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is harder than it appears because RUSTFLAGS
is not passed to proc macros. I guess we have to emit a cfg
check, so it actually can't live in either of these places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
44a31b1 seems to work in my local tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that also trigger other unrelated errors when it fails?
What we do today is essentially define the main
macro two times to pass different values to the rt_multi_thread
boolean, and have lib.rs
do this:
#[cfg(feature = "rt-multi-thread")]
pub use tokio_macros::main;
#[cfg(not(feature = "rt-multi-thread"))]
pub use tokio_macros::main_rt as main;
This way, the macro knows based on the cfgs that apply in the main Tokio crate. We could use the same approach and have four macros.
This looks fine to me. I'm happy to merge this when @Darksonn is. Are there docs? My main concern is explaining to users the difference between flavor local and flavor current_thread and how to pick between them. |
Is there a reason to use |
I'm happy enough to not change the default runtime — but I'm not really clear on what I should write in the documentation about when to choose |
I'm not certain I've done the right thing but I've done my best :) Please feel free to suggest further changes if I didn't understand something correctly. |
tokio-macros/src/lib.rs
Outdated
/// | ||
/// Equivalent code not using `#[tokio::main]` | ||
/// | ||
/// ```ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?
/// ```ignore | |
/// ``` | |
/// #![cfg(tokio_unstable)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it will be executed. We could use text
but then we lose Rust highlighting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can execute it, then I think we should. Why can't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do b040de5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't execute it because the LocalExecutor
doesn't exist if we don't have tokio_unstable
, which we don't usually have in tests. But maybe it's clearer this way anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we add a cfg(tokio_unstable)
to the test so it only actually runs under tokio_unstable
? I believe we run tests both with and without the flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's b040de5 :)
b1f3a19
to
44a31b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The permission of this file is updated too.
Motivation
We would like to be able to create a local runtime from the
tokio::main
attribute in order to be able to usespawn_local
in functions that rely ontokio::main
ortokio::test
.Solution
Add a
local
flavor totokio::main
which behaves likecurrent-thread
, but callsbuild_local(Default::default())
instead ofbuild()
.