diff --git a/embassy-macros/src/macros/task.rs b/embassy-macros/src/macros/task.rs index 0cf6b423..c37ad212 100644 --- a/embassy-macros/src/macros/task.rs +++ b/embassy-macros/src/macros/task.rs @@ -10,12 +10,10 @@ struct Args { #[darling(default)] pool_size: Option, #[darling(default)] - send: bool, - #[darling(default)] embassy_prefix: ModulePrefix, } -pub fn run(args: syn::AttributeArgs, mut f: syn::ItemFn) -> Result { +pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result { let args = Args::from_list(&args).map_err(|e| e.write_errors())?; let embassy_prefix = args.embassy_prefix.append("embassy"); @@ -35,24 +33,27 @@ pub fn run(args: syn::AttributeArgs, mut f: syn::ItemFn) -> Result = - syn::punctuated::Punctuated::new(); + let mut arg_types = Vec::new(); + let mut arg_names = Vec::new(); + let mut arg_indexes = Vec::new(); let mut fargs = f.sig.inputs.clone(); - for arg in fargs.iter_mut() { + for (i, arg) in fargs.iter_mut().enumerate() { match arg { syn::FnArg::Receiver(_) => { ctxt.error_spanned_by(arg, "task functions must not have receiver arguments"); } syn::FnArg::Typed(t) => match t.pat.as_mut() { - syn::Pat::Ident(i) => { - arg_names.push(i.ident.clone()); - i.mutability = None; + 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; } _ => { ctxt.error_spanned_by( arg, - "pattern matching in task arguments is not yet supporteds", + "pattern matching in task arguments is not yet supported", ); } }, @@ -63,40 +64,38 @@ pub fn run(args: syn::AttributeArgs, mut f: syn::ItemFn) -> Result #spawn_token<#future_ident> { - #f + mod #mod_ident { + use #embassy_path::executor::SpawnToken; + use #embassy_path::executor::raw::TaskStorage; + + type Fut = impl ::core::future::Future + 'static; - #[allow(non_upper_case_globals)] #[allow(clippy::declare_interior_mutable_const)] - const #new_ts_ident: #task_storage<#future_ident> = #task_storage::new(); + const NEW_TS: TaskStorage = TaskStorage::new(); - #[allow(non_upper_case_globals)] - static #pool_ident: [#task_storage<#future_ident>; #pool_size] = [#new_ts_ident; #pool_size]; + static POOL: [TaskStorage; #pool_size] = [NEW_TS; #pool_size]; - unsafe { #task_storage::spawn_pool(&#pool_ident, move || #task_inner_ident(#arg_names)) } + pub(super) fn task(args: super::#args_ident) -> SpawnToken { + unsafe { TaskStorage::spawn_pool(&POOL, move || super::#task_inner_ident(#(args.#arg_indexes),*)) } + } + } + + #visibility fn #task_ident(#fargs) -> #embassy_path::executor::SpawnToken { + #mod_ident::task((#(#arg_names,)*)) } };