From 96d917992ca591def3d7be87bc9f03486549b2a0 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 26 Apr 2022 18:36:38 +0200 Subject: [PATCH] macros: simplify task macro using "TAIT laundering". --- embassy-macros/src/macros/task.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/embassy-macros/src/macros/task.rs b/embassy-macros/src/macros/task.rs index c37ad212..396ce18f 100644 --- a/embassy-macros/src/macros/task.rs +++ b/embassy-macros/src/macros/task.rs @@ -33,12 +33,10 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result { ctxt.error_spanned_by(arg, "task functions must not have receiver arguments"); @@ -46,8 +44,6 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result match t.pat.as_mut() { syn::Pat::Ident(id) => { arg_names.push(id.ident.clone()); - arg_types.push(t.ty.clone()); - arg_indexes.push(syn::Index::from(i)); id.mutability = None; } _ => { @@ -64,8 +60,6 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result Result #embassy_path::executor::SpawnToken { + use ::core::future::Future; use #embassy_path::executor::SpawnToken; use #embassy_path::executor::raw::TaskStorage; - type Fut = impl ::core::future::Future + 'static; + type Fut = impl Future + 'static; #[allow(clippy::declare_interior_mutable_const)] const NEW_TS: TaskStorage = TaskStorage::new(); static POOL: [TaskStorage; #pool_size] = [NEW_TS; #pool_size]; - pub(super) fn task(args: super::#args_ident) -> SpawnToken { - unsafe { TaskStorage::spawn_pool(&POOL, move || super::#task_inner_ident(#(args.#arg_indexes),*)) } + // Opaque type laundering, to obscure its origin! + // Workaround for "opaque type's hidden type cannot be another opaque type from the same scope" + // https://github.com/rust-lang/rust/issues/96406 + fn launder_tait(token: SpawnToken) -> SpawnToken { + token } - } - #visibility fn #task_ident(#fargs) -> #embassy_path::executor::SpawnToken { - #mod_ident::task((#(#arg_names,)*)) + launder_tait(unsafe { TaskStorage::spawn_pool(&POOL, move || #task_inner_ident(#(#arg_names,)*)) }) } };