aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-executor-macros/src/macros/task.rs19
-rw-r--r--embassy-executor/tests/test.rs14
2 files changed, 29 insertions, 4 deletions
diff --git a/embassy-executor-macros/src/macros/task.rs b/embassy-executor-macros/src/macros/task.rs
index 5161e1020..1efb2788b 100644
--- a/embassy-executor-macros/src/macros/task.rs
+++ b/embassy-executor-macros/src/macros/task.rs
@@ -49,7 +49,7 @@ pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStre
49 }, 49 },
50 } 50 }
51 51
52 let mut arg_names = Vec::new(); 52 let mut args = Vec::new();
53 let mut fargs = f.sig.inputs.clone(); 53 let mut fargs = f.sig.inputs.clone();
54 54
55 for arg in fargs.iter_mut() { 55 for arg in fargs.iter_mut() {
@@ -59,8 +59,8 @@ pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStre
59 } 59 }
60 syn::FnArg::Typed(t) => match t.pat.as_mut() { 60 syn::FnArg::Typed(t) => match t.pat.as_mut() {
61 syn::Pat::Ident(id) => { 61 syn::Pat::Ident(id) => {
62 arg_names.push(id.ident.clone());
63 id.mutability = None; 62 id.mutability = None;
63 args.push((id.clone(), t.attrs.clone()));
64 } 64 }
65 _ => { 65 _ => {
66 ctxt.error_spanned_by(arg, "pattern matching in task arguments is not yet supported"); 66 ctxt.error_spanned_by(arg, "pattern matching in task arguments is not yet supported");
@@ -79,13 +79,24 @@ pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStre
79 task_inner.vis = syn::Visibility::Inherited; 79 task_inner.vis = syn::Visibility::Inherited;
80 task_inner.sig.ident = task_inner_ident.clone(); 80 task_inner.sig.ident = task_inner_ident.clone();
81 81
82 // assemble the original input arguments,
83 // including any attributes that may have
84 // been applied previously
85 let mut full_args = Vec::new();
86 for (arg, cfgs) in args {
87 full_args.push(quote!(
88 #(#cfgs)*
89 #arg
90 ));
91 }
92
82 #[cfg(feature = "nightly")] 93 #[cfg(feature = "nightly")]
83 let mut task_outer: ItemFn = parse_quote! { 94 let mut task_outer: ItemFn = parse_quote! {
84 #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { 95 #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> {
85 type Fut = impl ::core::future::Future + 'static; 96 type Fut = impl ::core::future::Future + 'static;
86 const POOL_SIZE: usize = #pool_size; 97 const POOL_SIZE: usize = #pool_size;
87 static POOL: ::embassy_executor::raw::TaskPool<Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new(); 98 static POOL: ::embassy_executor::raw::TaskPool<Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new();
88 unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } 99 unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#full_args,)*)) }
89 } 100 }
90 }; 101 };
91 #[cfg(not(feature = "nightly"))] 102 #[cfg(not(feature = "nightly"))]
@@ -93,7 +104,7 @@ pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStre
93 #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { 104 #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> {
94 const POOL_SIZE: usize = #pool_size; 105 const POOL_SIZE: usize = #pool_size;
95 static POOL: ::embassy_executor::_export::TaskPoolRef = ::embassy_executor::_export::TaskPoolRef::new(); 106 static POOL: ::embassy_executor::_export::TaskPoolRef = ::embassy_executor::_export::TaskPoolRef::new();
96 unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } 107 unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#full_args,)*)) }
97 } 108 }
98 }; 109 };
99 110
diff --git a/embassy-executor/tests/test.rs b/embassy-executor/tests/test.rs
index 0dbd391e8..2c2441dd5 100644
--- a/embassy-executor/tests/test.rs
+++ b/embassy-executor/tests/test.rs
@@ -135,3 +135,17 @@ fn executor_task_self_wake_twice() {
135 ] 135 ]
136 ) 136 )
137} 137}
138
139#[test]
140fn executor_task_cfg_args() {
141 // simulate cfg'ing away argument c
142 #[task]
143 async fn task1(a: u32, b: u32, #[cfg(any())] c: u32) {
144 let (_, _) = (a, b);
145 }
146
147 #[task]
148 async fn task2(a: u32, b: u32, #[cfg(all())] c: u32) {
149 let (_, _, _) = (a, b, c);
150 }
151}