diff options
| author | Kaspar Schleiser <[email protected]> | 2024-11-04 23:02:30 +0100 |
|---|---|---|
| committer | Kaspar Schleiser <[email protected]> | 2024-11-04 23:02:30 +0100 |
| commit | 03adeeddc2abd1c02a659f428f114848cf83e5ac (patch) | |
| tree | ea7cd6d965a02e9009426d56d9395dbe99031508 /embassy-executor-macros/src/macros/task.rs | |
| parent | f55ebef380b49d109216e2cd6821fa1c35202748 (diff) | |
executor: allow overriding `embassy_executor` path in `task` macro
Diffstat (limited to 'embassy-executor-macros/src/macros/task.rs')
| -rw-r--r-- | embassy-executor-macros/src/macros/task.rs | 17 |
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 @@ | |||
| 1 | use std::str::FromStr; | ||
| 2 | |||
| 1 | use darling::export::NestedMeta; | 3 | use darling::export::NestedMeta; |
| 2 | use darling::FromMeta; | 4 | use darling::FromMeta; |
| 3 | use proc_macro2::{Span, TokenStream}; | 5 | use proc_macro2::{Span, TokenStream}; |
| @@ -11,6 +13,9 @@ use crate::util::*; | |||
| 11 | struct Args { | 13 | struct 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 | ||
| 16 | pub fn run(args: TokenStream, item: TokenStream) -> TokenStream { | 21 | pub 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 | ||
