Skip to content

Commit bc425d8

Browse files
committed
macros: add "local" runtime flavor
1 parent ab8d7b8 commit bc425d8

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

tests-integration/tests/macros_main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ async fn spawning() -> usize {
1616
join.await.unwrap()
1717
}
1818

19+
#[cfg(tokio_unstable)]
20+
#[tokio::main(flavor = "local")]
21+
async fn local_main() -> usize {
22+
let join = tokio::task::spawn_local(async { 1 });
23+
join.await.unwrap()
24+
}
25+
1926
#[test]
2027
fn main_with_spawn() {
2128
assert_eq!(1, spawning());
@@ -24,5 +31,8 @@ fn main_with_spawn() {
2431
#[test]
2532
fn shell() {
2633
assert_eq!(1, basic_main());
27-
assert_eq!(bool::default(), generic_fun::<bool>())
34+
assert_eq!(bool::default(), generic_fun::<bool>());
35+
36+
#[cfg(tokio_unstable)]
37+
assert_eq!(1, local_main());
2838
}

tokio-macros/src/entry.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>;
1010
enum RuntimeFlavor {
1111
CurrentThread,
1212
Threaded,
13+
Local,
1314
}
1415

1516
impl RuntimeFlavor {
1617
fn from_str(s: &str) -> Result<RuntimeFlavor, String> {
1718
match s {
1819
"current_thread" => Ok(RuntimeFlavor::CurrentThread),
1920
"multi_thread" => Ok(RuntimeFlavor::Threaded),
21+
"local" => Ok(RuntimeFlavor::Local),
2022
"single_thread" => Err("The single threaded runtime flavor is called `current_thread`.".to_string()),
2123
"basic_scheduler" => Err("The `basic_scheduler` runtime flavor has been renamed to `current_thread`.".to_string()),
2224
"threaded_scheduler" => Err("The `threaded_scheduler` runtime flavor has been renamed to `multi_thread`.".to_string()),
@@ -178,14 +180,14 @@ impl Configuration {
178180

179181
let flavor = self.flavor.unwrap_or(self.default_flavor);
180182
let worker_threads = match (flavor, self.worker_threads) {
181-
(F::CurrentThread, Some((_, worker_threads_span))) => {
183+
(F::CurrentThread | F::Local, Some((_, worker_threads_span))) => {
182184
let msg = format!(
183185
"The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[{}(flavor = \"multi_thread\")]`",
184186
self.macro_name(),
185187
);
186188
return Err(syn::Error::new(worker_threads_span, msg));
187189
}
188-
(F::CurrentThread, None) => None,
190+
(F::CurrentThread | F::Local, None) => None,
189191
(F::Threaded, worker_threads) if self.rt_multi_thread_available => {
190192
worker_threads.map(|(val, _span)| val)
191193
}
@@ -207,7 +209,7 @@ impl Configuration {
207209
);
208210
return Err(syn::Error::new(start_paused_span, msg));
209211
}
210-
(F::CurrentThread, Some((start_paused, _))) => Some(start_paused),
212+
(F::CurrentThread | F::Local, Some((start_paused, _))) => Some(start_paused),
211213
(_, None) => None,
212214
};
213215

@@ -219,7 +221,7 @@ impl Configuration {
219221
);
220222
return Err(syn::Error::new(unhandled_panic_span, msg));
221223
}
222-
(F::CurrentThread, Some((unhandled_panic, _))) => Some(unhandled_panic),
224+
(F::CurrentThread | F::Local, Some((unhandled_panic, _))) => Some(unhandled_panic),
223225
(_, None) => None,
224226
};
225227

@@ -408,13 +410,22 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
408410
.unwrap_or_else(|| Ident::new("tokio", last_stmt_start_span).into_token_stream());
409411

410412
let mut rt = match config.flavor {
411-
RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=>
412-
#crate_path::runtime::Builder::new_current_thread()
413-
},
413+
RuntimeFlavor::CurrentThread | RuntimeFlavor::Local => {
414+
quote_spanned! {last_stmt_start_span=>
415+
#crate_path::runtime::Builder::new_current_thread()
416+
}
417+
}
414418
RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=>
415419
#crate_path::runtime::Builder::new_multi_thread()
416420
},
417421
};
422+
423+
let build = if let RuntimeFlavor::Local = config.flavor {
424+
quote_spanned! {last_stmt_start_span=> build_local(Default::default())}
425+
} else {
426+
quote_spanned! {last_stmt_start_span=> build()}
427+
};
428+
418429
if let Some(v) = config.worker_threads {
419430
rt = quote_spanned! {last_stmt_start_span=> #rt.worker_threads(#v) };
420431
}
@@ -441,7 +452,7 @@ fn parse_knobs(mut input: ItemFn, is_test: bool, config: FinalConfig) -> TokenSt
441452
{
442453
return #rt
443454
.enable_all()
444-
.build()
455+
.#build
445456
.expect("Failed building the Runtime")
446457
.block_on(#body_ident);
447458
}

0 commit comments

Comments
 (0)