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

macros: run current_thread inside LocalSet #4027

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tests-build/tests/fail/macros_type_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:5:5
--> $DIR/macros_type_mismatch.rs:5:7
|
5 | Ok(())
| ^^^^^^ expected `()`, found enum `Result`
| ^^^^ expected `()`, found enum `Result`
|
= note: expected unit type `()`
found enum `Result<(), _>`
Expand All @@ -16,12 +16,12 @@ help: try adding a return type
| ^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/macros_type_mismatch.rs:10:5
--> $DIR/macros_type_mismatch.rs:10:18
|
9 | async fn missing_return_type() {
| - help: try adding a return type: `-> Result<(), _>`
10 | return Ok(());
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
| ^ expected `()`, found enum `Result`
|
= note: expected unit type `()`
found enum `Result<(), _>`
Expand Down
9 changes: 4 additions & 5 deletions tokio-macros/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,10 @@ fn parse_knobs(
{
let body = async #body;
#[allow(clippy::expect_used)]
#tail_return #rt
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(body)#tail_semicolon
#tail_return tokio::task::LocalSet::new().block_on(
&#rt.enable_all().build().expect("Failed building the Runtime"),
body,
)#tail_semicolon
}
})
.expect("Parsing failure");
Expand Down
58 changes: 33 additions & 25 deletions tokio-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use proc_macro::TokenStream;
/// helps set up a `Runtime` without requiring the user to use
/// [Runtime](../tokio/runtime/struct.Runtime.html) or
/// [Builder](../tokio/runtime/struct.Builder.html) directly.
/// The function executes in the context of a
/// [LocalSet](../tokio/task/struct.LocalSet.html), allowing calls to
/// [spawn_local](../tokio/task/fn.spawn_local.html) without further setup.
///
/// Note: This macro is designed to be simplistic and targets applications that
/// do not require a complex setup. If the provided functionality is not
Expand Down Expand Up @@ -84,13 +87,14 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
/// tokio::runtime::Builder::new_multi_thread()
/// let ls = tokio::task::LocalSet::new();
/// let rt = tokio::runtime::Builder::new_multi_thread()
/// .enable_all()
/// .build()
/// .unwrap()
/// .block_on(async {
/// println!("Hello world");
/// })
/// .unwrap();
/// ls.block_on(&rt, async {
/// println!("Hello world");
/// })
/// }
/// ```
///
Expand All @@ -109,13 +113,14 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
/// tokio::runtime::Builder::new_current_thread()
/// let ls = tokio::task::LocalSet::new();
/// let rt = tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap()
/// .block_on(async {
/// println!("Hello world");
/// })
/// .unwrap();
/// ls.block_on(&rt, async {
/// println!("Hello world");
/// })
/// }
/// ```
///
Expand All @@ -132,14 +137,15 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
/// tokio::runtime::Builder::new_multi_thread()
/// let ls = tokio::task::LocalSet::new();
/// let rt = tokio::runtime::Builder::new_multi_thread()
/// .worker_threads(2)
/// .enable_all()
/// .build()
/// .unwrap()
/// .block_on(async {
/// println!("Hello world");
/// })
/// .unwrap();
/// ls.block_on(&rt, async {
/// println!("Hello world");
/// })
/// }
/// ```
///
Expand All @@ -156,14 +162,15 @@ use proc_macro::TokenStream;
///
/// ```rust
/// fn main() {
/// tokio::runtime::Builder::new_current_thread()
/// let ls = tokio::task::LocalSet::new();
/// let rt = tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .start_paused(true)
/// .build()
/// .unwrap()
/// .block_on(async {
/// println!("Hello world");
/// })
/// .unwrap();
/// ls.block_on(&rt, async {
/// println!("Hello world");
/// })
/// }
/// ```
///
Expand Down Expand Up @@ -204,13 +211,14 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
///
/// ```rust
/// fn main() {
/// tokio::runtime::Builder::new_current_thread()
/// let ls = tokio::task::LocalSet::new();
/// let rt = tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap()
/// .block_on(async {
/// println!("Hello world");
/// })
/// .unwrap();
/// ls.block_on(&rt, async {
/// println!("Hello world");
/// })
/// }
/// ```
///
Expand Down
10 changes: 10 additions & 0 deletions tokio/tests/task_local_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ use std::sync::atomic::Ordering::{self, SeqCst};
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::time::Duration;

#[tokio::test(flavor = "current_thread")]
async fn localset_implicit_current_thread() {
task::spawn_local(async {}).await.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn localset_implicit_multi_thread() {
task::spawn_local(async {}).await.unwrap();
}

#[tokio::test(flavor = "current_thread")]
async fn local_basic_scheduler() {
LocalSet::new()
Expand Down