diff options
| author | Frostie314159 <[email protected]> | 2024-03-31 20:48:05 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-31 20:48:05 +0200 |
| commit | 67c9cc2c4b886e6962ecdd6eff8794b14c1accdc (patch) | |
| tree | f176ab269949d26f48e04c950cebc5489bae8c56 /embassy-executor-macros | |
| parent | a2f9aa592ec61beb247065003016515f0d423c13 (diff) | |
| parent | 6634cc90bcd3eb25b64712688920f383584b2964 (diff) | |
Merge branch 'embassy-rs:main' into ticker_send_sync
Diffstat (limited to 'embassy-executor-macros')
| -rw-r--r-- | embassy-executor-macros/Cargo.toml | 5 | ||||
| -rw-r--r-- | embassy-executor-macros/README.md | 10 | ||||
| -rw-r--r-- | embassy-executor-macros/src/lib.rs | 7 | ||||
| -rw-r--r-- | embassy-executor-macros/src/macros/main.rs | 14 | ||||
| -rw-r--r-- | embassy-executor-macros/src/macros/task.rs | 34 | ||||
| -rw-r--r-- | embassy-executor-macros/src/util/ctxt.rs | 1 |
6 files changed, 52 insertions, 19 deletions
diff --git a/embassy-executor-macros/Cargo.toml b/embassy-executor-macros/Cargo.toml index 6ea7ddb71..2953e7ccc 100644 --- a/embassy-executor-macros/Cargo.toml +++ b/embassy-executor-macros/Cargo.toml | |||
| @@ -1,10 +1,11 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "embassy-executor-macros" | 2 | name = "embassy-executor-macros" |
| 3 | version = "0.4.0" | 3 | version = "0.4.1" |
| 4 | edition = "2021" | 4 | edition = "2021" |
| 5 | license = "MIT OR Apache-2.0" | 5 | license = "MIT OR Apache-2.0" |
| 6 | description = "macros for creating the entry point and tasks for embassy-executor" | 6 | description = "macros for creating the entry point and tasks for embassy-executor" |
| 7 | repository = "https://github.com/embassy-rs/embassy" | 7 | repository = "https://github.com/embassy-rs/embassy" |
| 8 | documentation = "https://docs.embassy.dev/embassy-executor-macros" | ||
| 8 | categories = [ | 9 | categories = [ |
| 9 | "embedded", | 10 | "embedded", |
| 10 | "no-std", | 11 | "no-std", |
| @@ -21,4 +22,4 @@ proc-macro2 = "1.0.29" | |||
| 21 | proc-macro = true | 22 | proc-macro = true |
| 22 | 23 | ||
| 23 | [features] | 24 | [features] |
| 24 | nightly = [] \ No newline at end of file | 25 | nightly = [] |
diff --git a/embassy-executor-macros/README.md b/embassy-executor-macros/README.md index a959c85d5..3a8d49aa1 100644 --- a/embassy-executor-macros/README.md +++ b/embassy-executor-macros/README.md | |||
| @@ -3,13 +3,3 @@ | |||
| 3 | An [Embassy](https://embassy.dev) project. | 3 | An [Embassy](https://embassy.dev) project. |
| 4 | 4 | ||
| 5 | NOTE: Do not use this crate directly. The macros are re-exported by `embassy-executor`. | 5 | NOTE: Do not use this crate directly. The macros are re-exported by `embassy-executor`. |
| 6 | |||
| 7 | ## License | ||
| 8 | |||
| 9 | This work is licensed under either of | ||
| 10 | |||
| 11 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or | ||
| 12 | <http://www.apache.org/licenses/LICENSE-2.0>) | ||
| 13 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) | ||
| 14 | |||
| 15 | at your option. | ||
diff --git a/embassy-executor-macros/src/lib.rs b/embassy-executor-macros/src/lib.rs index c9d58746a..5461fe04c 100644 --- a/embassy-executor-macros/src/lib.rs +++ b/embassy-executor-macros/src/lib.rs | |||
| @@ -62,6 +62,13 @@ pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 62 | task::run(&args.meta, f).unwrap_or_else(|x| x).into() | 62 | task::run(&args.meta, f).unwrap_or_else(|x| x).into() |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | #[proc_macro_attribute] | ||
| 66 | pub fn main_avr(args: TokenStream, item: TokenStream) -> TokenStream { | ||
| 67 | let args = syn::parse_macro_input!(args as Args); | ||
| 68 | let f = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 69 | main::run(&args.meta, f, main::avr()).unwrap_or_else(|x| x).into() | ||
| 70 | } | ||
| 71 | |||
| 65 | /// Creates a new `executor` instance and declares an application entry point for Cortex-M spawning the corresponding function body as an async task. | 72 | /// Creates a new `executor` instance and declares an application entry point for Cortex-M spawning the corresponding function body as an async task. |
| 66 | /// | 73 | /// |
| 67 | /// The following restrictions apply: | 74 | /// The following restrictions apply: |
diff --git a/embassy-executor-macros/src/macros/main.rs b/embassy-executor-macros/src/macros/main.rs index 3c0d58567..088e64d1c 100644 --- a/embassy-executor-macros/src/macros/main.rs +++ b/embassy-executor-macros/src/macros/main.rs | |||
| @@ -12,6 +12,20 @@ struct Args { | |||
| 12 | entry: Option<String>, | 12 | entry: Option<String>, |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | pub fn avr() -> TokenStream { | ||
| 16 | quote! { | ||
| 17 | #[avr_device::entry] | ||
| 18 | fn main() -> ! { | ||
| 19 | let mut executor = ::embassy_executor::Executor::new(); | ||
| 20 | let executor = unsafe { __make_static(&mut executor) }; | ||
| 21 | |||
| 22 | executor.run(|spawner| { | ||
| 23 | spawner.must_spawn(__embassy_main(spawner)); | ||
| 24 | }) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 15 | pub fn riscv(args: &[NestedMeta]) -> TokenStream { | 29 | pub fn riscv(args: &[NestedMeta]) -> TokenStream { |
| 16 | let maybe_entry = match Args::from_list(args) { | 30 | let maybe_entry = match Args::from_list(args) { |
| 17 | Ok(args) => args.entry, | 31 | Ok(args) => args.entry, |
diff --git a/embassy-executor-macros/src/macros/task.rs b/embassy-executor-macros/src/macros/task.rs index 5161e1020..96c6267b2 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,35 @@ 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 | trait _EmbassyInternalTaskTrait { |
| 97 | type Fut: ::core::future::Future + 'static; | ||
| 98 | fn construct(#fargs) -> Self::Fut; | ||
| 99 | } | ||
| 100 | |||
| 101 | impl _EmbassyInternalTaskTrait for () { | ||
| 102 | type Fut = impl core::future::Future + 'static; | ||
| 103 | fn construct(#fargs) -> Self::Fut { | ||
| 104 | #task_inner_ident(#(#full_args,)*) | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 86 | const POOL_SIZE: usize = #pool_size; | 108 | const POOL_SIZE: usize = #pool_size; |
| 87 | static POOL: ::embassy_executor::raw::TaskPool<Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new(); | 109 | static POOL: ::embassy_executor::raw::TaskPool<<() as _EmbassyInternalTaskTrait>::Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new(); |
| 88 | unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } | 110 | unsafe { POOL._spawn_async_fn(move || <() as _EmbassyInternalTaskTrait>::construct(#(#full_args,)*)) } |
| 89 | } | 111 | } |
| 90 | }; | 112 | }; |
| 91 | #[cfg(not(feature = "nightly"))] | 113 | #[cfg(not(feature = "nightly"))] |
| @@ -93,7 +115,7 @@ pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStre | |||
| 93 | #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { | 115 | #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { |
| 94 | const POOL_SIZE: usize = #pool_size; | 116 | const POOL_SIZE: usize = #pool_size; |
| 95 | static POOL: ::embassy_executor::_export::TaskPoolRef = ::embassy_executor::_export::TaskPoolRef::new(); | 117 | 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,)*)) } | 118 | unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#full_args,)*)) } |
| 97 | } | 119 | } |
| 98 | }; | 120 | }; |
| 99 | 121 | ||
diff --git a/embassy-executor-macros/src/util/ctxt.rs b/embassy-executor-macros/src/util/ctxt.rs index 74c872c3c..9c78cda01 100644 --- a/embassy-executor-macros/src/util/ctxt.rs +++ b/embassy-executor-macros/src/util/ctxt.rs | |||
| @@ -7,7 +7,6 @@ use std::thread; | |||
| 7 | 7 | ||
| 8 | use proc_macro2::TokenStream; | 8 | use proc_macro2::TokenStream; |
| 9 | use quote::{quote, ToTokens}; | 9 | use quote::{quote, ToTokens}; |
| 10 | use syn; | ||
| 11 | 10 | ||
| 12 | /// A type to collect errors together and format them. | 11 | /// A type to collect errors together and format them. |
| 13 | /// | 12 | /// |
