Skip to content

Commit 0a246be

Browse files
bnoordhuishawkw
authored andcommitted
macros: run runtime inside LocalSet when using macro (#4027)
1 parent 88ffea6 commit 0a246be

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

tests-build/tests/fail/macros_type_mismatch.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/macros_type_mismatch.rs:5:5
2+
--> $DIR/macros_type_mismatch.rs:5:7
33
|
44
5 | Ok(())
5-
| ^^^^^^ expected `()`, found enum `Result`
5+
| ^^^^ expected `()`, found enum `Result`
66
|
77
= note: expected unit type `()`
88
found enum `Result<(), _>`
@@ -16,12 +16,12 @@ help: try adding a return type
1616
| ^^^^^^^^^^^^^^^^
1717

1818
error[E0308]: mismatched types
19-
--> $DIR/macros_type_mismatch.rs:10:5
19+
--> $DIR/macros_type_mismatch.rs:10:18
2020
|
2121
9 | async fn missing_return_type() {
2222
| - help: try adding a return type: `-> Result<(), _>`
2323
10 | return Ok(());
24-
| ^^^^^^^^^^^^^^ expected `()`, found enum `Result`
24+
| ^ expected `()`, found enum `Result`
2525
|
2626
= note: expected unit type `()`
2727
found enum `Result<(), _>`

tokio-macros/src/entry.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,10 @@ fn parse_knobs(
339339
{
340340
let body = async #body;
341341
#[allow(clippy::expect_used)]
342-
#tail_return #rt
343-
.enable_all()
344-
.build()
345-
.expect("Failed building the Runtime")
346-
.block_on(body)#tail_semicolon
342+
#tail_return tokio::task::LocalSet::new().block_on(
343+
&#rt.enable_all().build().expect("Failed building the Runtime"),
344+
body,
345+
)#tail_semicolon
347346
}
348347
})
349348
.expect("Parsing failure");

tokio-macros/src/lib.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use proc_macro::TokenStream;
2727
/// helps set up a `Runtime` without requiring the user to use
2828
/// [Runtime](../tokio/runtime/struct.Runtime.html) or
2929
/// [Builder](../tokio/runtime/struct.Builder.html) directly.
30+
/// The function executes in the context of a
31+
/// [LocalSet](../tokio/task/struct.LocalSet.html), allowing calls to
32+
/// [spawn_local](../tokio/task/fn.spawn_local.html) without further setup.
3033
///
3134
/// Note: This macro is designed to be simplistic and targets applications that
3235
/// do not require a complex setup. If the provided functionality is not
@@ -84,13 +87,14 @@ use proc_macro::TokenStream;
8487
///
8588
/// ```rust
8689
/// fn main() {
87-
/// tokio::runtime::Builder::new_multi_thread()
90+
/// let ls = tokio::task::LocalSet::new();
91+
/// let rt = tokio::runtime::Builder::new_multi_thread()
8892
/// .enable_all()
8993
/// .build()
90-
/// .unwrap()
91-
/// .block_on(async {
92-
/// println!("Hello world");
93-
/// })
94+
/// .unwrap();
95+
/// ls.block_on(&rt, async {
96+
/// println!("Hello world");
97+
/// })
9498
/// }
9599
/// ```
96100
///
@@ -109,13 +113,14 @@ use proc_macro::TokenStream;
109113
///
110114
/// ```rust
111115
/// fn main() {
112-
/// tokio::runtime::Builder::new_current_thread()
116+
/// let ls = tokio::task::LocalSet::new();
117+
/// let rt = tokio::runtime::Builder::new_current_thread()
113118
/// .enable_all()
114119
/// .build()
115-
/// .unwrap()
116-
/// .block_on(async {
117-
/// println!("Hello world");
118-
/// })
120+
/// .unwrap();
121+
/// ls.block_on(&rt, async {
122+
/// println!("Hello world");
123+
/// })
119124
/// }
120125
/// ```
121126
///
@@ -132,14 +137,15 @@ use proc_macro::TokenStream;
132137
///
133138
/// ```rust
134139
/// fn main() {
135-
/// tokio::runtime::Builder::new_multi_thread()
140+
/// let ls = tokio::task::LocalSet::new();
141+
/// let rt = tokio::runtime::Builder::new_multi_thread()
136142
/// .worker_threads(2)
137143
/// .enable_all()
138144
/// .build()
139-
/// .unwrap()
140-
/// .block_on(async {
141-
/// println!("Hello world");
142-
/// })
145+
/// .unwrap();
146+
/// ls.block_on(&rt, async {
147+
/// println!("Hello world");
148+
/// })
143149
/// }
144150
/// ```
145151
///
@@ -156,14 +162,15 @@ use proc_macro::TokenStream;
156162
///
157163
/// ```rust
158164
/// fn main() {
159-
/// tokio::runtime::Builder::new_current_thread()
165+
/// let ls = tokio::task::LocalSet::new();
166+
/// let rt = tokio::runtime::Builder::new_current_thread()
160167
/// .enable_all()
161168
/// .start_paused(true)
162169
/// .build()
163-
/// .unwrap()
164-
/// .block_on(async {
165-
/// println!("Hello world");
166-
/// })
170+
/// .unwrap();
171+
/// ls.block_on(&rt, async {
172+
/// println!("Hello world");
173+
/// })
167174
/// }
168175
/// ```
169176
///
@@ -204,13 +211,14 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
204211
///
205212
/// ```rust
206213
/// fn main() {
207-
/// tokio::runtime::Builder::new_current_thread()
214+
/// let ls = tokio::task::LocalSet::new();
215+
/// let rt = tokio::runtime::Builder::new_current_thread()
208216
/// .enable_all()
209217
/// .build()
210-
/// .unwrap()
211-
/// .block_on(async {
212-
/// println!("Hello world");
213-
/// })
218+
/// .unwrap();
219+
/// ls.block_on(&rt, async {
220+
/// println!("Hello world");
221+
/// })
214222
/// }
215223
/// ```
216224
///

tokio/tests/task_local_set.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ use std::sync::atomic::Ordering::{self, SeqCst};
1616
use std::sync::atomic::{AtomicBool, AtomicUsize};
1717
use std::time::Duration;
1818

19+
#[tokio::test(flavor = "current_thread")]
20+
async fn localset_implicit_current_thread() {
21+
task::spawn_local(async {}).await.unwrap();
22+
}
23+
24+
#[tokio::test(flavor = "multi_thread")]
25+
async fn localset_implicit_multi_thread() {
26+
task::spawn_local(async {}).await.unwrap();
27+
}
28+
1929
#[tokio::test(flavor = "current_thread")]
2030
async fn local_basic_scheduler() {
2131
LocalSet::new()

0 commit comments

Comments
 (0)