aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-11-06 08:08:07 +0000
committerGitHub <[email protected]>2024-11-06 08:08:07 +0000
commit5bc4796b96be8d07b54892db513225fc129d5e50 (patch)
treec86e280cac469f18c7f50021974d61d37315d4d7
parent5b075077090ec1436437c82011554649e909c8ee (diff)
parent03adeeddc2abd1c02a659f428f114848cf83e5ac (diff)
Merge pull request #3503 from kaspar030/task-macro-path-override
executor: allow overriding `embassy_executor` path in `task` macro
-rw-r--r--embassy-executor-macros/src/macros/task.rs17
1 files changed, 13 insertions, 4 deletions
diff --git a/embassy-executor-macros/src/macros/task.rs b/embassy-executor-macros/src/macros/task.rs
index 2f2aeda76..e8134c6a9 100644
--- a/embassy-executor-macros/src/macros/task.rs
+++ b/embassy-executor-macros/src/macros/task.rs
@@ -1,3 +1,5 @@
1use std::str::FromStr;
2
1use darling::export::NestedMeta; 3use darling::export::NestedMeta;
2use darling::FromMeta; 4use darling::FromMeta;
3use proc_macro2::{Span, TokenStream}; 5use proc_macro2::{Span, TokenStream};
@@ -11,6 +13,9 @@ use crate::util::*;
11struct Args { 13struct Args {
12 #[darling(default)] 14 #[darling(default)]
13 pool_size: Option<syn::Expr>, 15 pool_size: Option<syn::Expr>,
16 /// Use this to override the `embassy_executor` crate path. Defaults to `::embassy_executor`.
17 #[darling(default)]
18 embassy_executor: Option<syn::Expr>,
14} 19}
15 20
16pub fn run(args: TokenStream, item: TokenStream) -> TokenStream { 21pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
@@ -42,6 +47,10 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
42 lit: Lit::Int(LitInt::new("1", Span::call_site())), 47 lit: Lit::Int(LitInt::new("1", Span::call_site())),
43 })); 48 }));
44 49
50 let embassy_executor = args
51 .embassy_executor
52 .unwrap_or(Expr::Verbatim(TokenStream::from_str("::embassy_executor").unwrap()));
53
45 if f.sig.asyncness.is_none() { 54 if f.sig.asyncness.is_none() {
46 error(&mut errors, &f.sig, "task functions must be async"); 55 error(&mut errors, &f.sig, "task functions must be async");
47 } 56 }
@@ -131,13 +140,13 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
131 } 140 }
132 141
133 const POOL_SIZE: usize = #pool_size; 142 const POOL_SIZE: usize = #pool_size;
134 static POOL: ::embassy_executor::raw::TaskPool<<() as _EmbassyInternalTaskTrait>::Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new(); 143 static POOL: #embassy_executor::raw::TaskPool<<() as _EmbassyInternalTaskTrait>::Fut, POOL_SIZE> = #embassy_executor::raw::TaskPool::new();
135 unsafe { POOL._spawn_async_fn(move || <() as _EmbassyInternalTaskTrait>::construct(#(#full_args,)*)) } 144 unsafe { POOL._spawn_async_fn(move || <() as _EmbassyInternalTaskTrait>::construct(#(#full_args,)*)) }
136 }; 145 };
137 #[cfg(not(feature = "nightly"))] 146 #[cfg(not(feature = "nightly"))]
138 let mut task_outer_body = quote! { 147 let mut task_outer_body = quote! {
139 const POOL_SIZE: usize = #pool_size; 148 const POOL_SIZE: usize = #pool_size;
140 static POOL: ::embassy_executor::_export::TaskPoolRef = ::embassy_executor::_export::TaskPoolRef::new(); 149 static POOL: #embassy_executor::_export::TaskPoolRef = #embassy_executor::_export::TaskPoolRef::new();
141 unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#full_args,)*)) } 150 unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#full_args,)*)) }
142 }; 151 };
143 152
@@ -146,7 +155,7 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
146 if !errors.is_empty() { 155 if !errors.is_empty() {
147 task_outer_body = quote! { 156 task_outer_body = quote! {
148 #![allow(unused_variables, unreachable_code)] 157 #![allow(unused_variables, unreachable_code)]
149 let _x: ::embassy_executor::SpawnToken<()> = ::core::todo!(); 158 let _x: #embassy_executor::SpawnToken<()> = ::core::todo!();
150 _x 159 _x
151 }; 160 };
152 } 161 }
@@ -164,7 +173,7 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
164 #task_inner 173 #task_inner
165 174
166 #(#task_outer_attrs)* 175 #(#task_outer_attrs)*
167 #visibility fn #task_ident #generics (#fargs) -> ::embassy_executor::SpawnToken<impl Sized> #where_clause{ 176 #visibility fn #task_ident #generics (#fargs) -> #embassy_executor::SpawnToken<impl Sized> #where_clause{
168 #task_outer_body 177 #task_outer_body
169 } 178 }
170 179