diff options
Diffstat (limited to 'embassy-executor-macros/src/macros')
| -rw-r--r-- | embassy-executor-macros/src/macros/main.rs | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/embassy-executor-macros/src/macros/main.rs b/embassy-executor-macros/src/macros/main.rs index 24f61f30b..95722b19b 100644 --- a/embassy-executor-macros/src/macros/main.rs +++ b/embassy-executor-macros/src/macros/main.rs | |||
| @@ -16,42 +16,57 @@ enum Flavor { | |||
| 16 | pub(crate) struct Arch { | 16 | pub(crate) struct Arch { |
| 17 | default_entry: Option<&'static str>, | 17 | default_entry: Option<&'static str>, |
| 18 | flavor: Flavor, | 18 | flavor: Flavor, |
| 19 | executor_required: bool, | ||
| 19 | } | 20 | } |
| 20 | 21 | ||
| 21 | pub static ARCH_AVR: Arch = Arch { | 22 | pub static ARCH_AVR: Arch = Arch { |
| 22 | default_entry: Some("avr_device::entry"), | 23 | default_entry: Some("avr_device::entry"), |
| 23 | flavor: Flavor::Standard, | 24 | flavor: Flavor::Standard, |
| 25 | executor_required: false, | ||
| 24 | }; | 26 | }; |
| 25 | 27 | ||
| 26 | pub static ARCH_RISCV: Arch = Arch { | 28 | pub static ARCH_RISCV: Arch = Arch { |
| 27 | default_entry: Some("riscv_rt::entry"), | 29 | default_entry: Some("riscv_rt::entry"), |
| 28 | flavor: Flavor::Standard, | 30 | flavor: Flavor::Standard, |
| 31 | executor_required: false, | ||
| 29 | }; | 32 | }; |
| 30 | 33 | ||
| 31 | pub static ARCH_CORTEX_M: Arch = Arch { | 34 | pub static ARCH_CORTEX_M: Arch = Arch { |
| 32 | default_entry: Some("cortex_m_rt::entry"), | 35 | default_entry: Some("cortex_m_rt::entry"), |
| 33 | flavor: Flavor::Standard, | 36 | flavor: Flavor::Standard, |
| 37 | executor_required: false, | ||
| 34 | }; | 38 | }; |
| 35 | 39 | ||
| 36 | pub static ARCH_SPIN: Arch = Arch { | 40 | pub static ARCH_SPIN: Arch = Arch { |
| 37 | default_entry: None, | 41 | default_entry: None, |
| 38 | flavor: Flavor::Standard, | 42 | flavor: Flavor::Standard, |
| 43 | executor_required: false, | ||
| 39 | }; | 44 | }; |
| 40 | 45 | ||
| 41 | pub static ARCH_STD: Arch = Arch { | 46 | pub static ARCH_STD: Arch = Arch { |
| 42 | default_entry: None, | 47 | default_entry: None, |
| 43 | flavor: Flavor::Standard, | 48 | flavor: Flavor::Standard, |
| 49 | executor_required: false, | ||
| 44 | }; | 50 | }; |
| 45 | 51 | ||
| 46 | pub static ARCH_WASM: Arch = Arch { | 52 | pub static ARCH_WASM: Arch = Arch { |
| 47 | default_entry: Some("wasm_bindgen::prelude::wasm_bindgen(start)"), | 53 | default_entry: Some("wasm_bindgen::prelude::wasm_bindgen(start)"), |
| 48 | flavor: Flavor::Wasm, | 54 | flavor: Flavor::Wasm, |
| 55 | executor_required: false, | ||
| 56 | }; | ||
| 57 | |||
| 58 | pub static ARCH_UNSPECIFIED: Arch = Arch { | ||
| 59 | default_entry: None, | ||
| 60 | flavor: Flavor::Standard, | ||
| 61 | executor_required: true, | ||
| 49 | }; | 62 | }; |
| 50 | 63 | ||
| 51 | #[derive(Debug, FromMeta, Default)] | 64 | #[derive(Debug, FromMeta, Default)] |
| 52 | struct Args { | 65 | struct Args { |
| 53 | #[darling(default)] | 66 | #[darling(default)] |
| 54 | entry: Option<String>, | 67 | entry: Option<String>, |
| 68 | #[darling(default)] | ||
| 69 | executor: Option<String>, | ||
| 55 | } | 70 | } |
| 56 | 71 | ||
| 57 | pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { | 72 | pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { |
| @@ -112,9 +127,10 @@ pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { | |||
| 112 | error(&mut errors, &f.sig, "main function must have 1 argument: the spawner."); | 127 | error(&mut errors, &f.sig, "main function must have 1 argument: the spawner."); |
| 113 | } | 128 | } |
| 114 | 129 | ||
| 115 | let entry = match args.entry.as_deref().or(arch.default_entry) { | 130 | let entry = match (args.entry.as_deref(), arch.default_entry.as_deref()) { |
| 116 | None => TokenStream::new(), | 131 | (None, None) => TokenStream::new(), |
| 117 | Some(x) => match TokenStream::from_str(x) { | 132 | (Some(x), _) | (None, Some(x)) if x == "" => TokenStream::new(), |
| 133 | (Some(x), _) | (None, Some(x)) => match TokenStream::from_str(x) { | ||
| 118 | Ok(x) => quote!(#[#x]), | 134 | Ok(x) => quote!(#[#x]), |
| 119 | Err(e) => { | 135 | Err(e) => { |
| 120 | error(&mut errors, &f.sig, e); | 136 | error(&mut errors, &f.sig, e); |
| @@ -123,6 +139,28 @@ pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { | |||
| 123 | }, | 139 | }, |
| 124 | }; | 140 | }; |
| 125 | 141 | ||
| 142 | let executor = match (args.executor.as_deref(), arch.executor_required) { | ||
| 143 | (None, true) => { | ||
| 144 | error( | ||
| 145 | &mut errors, | ||
| 146 | &f.sig, | ||
| 147 | "\ | ||
| 148 | No architecture selected for embassy-executor. Make sure you've enabled one of the `arch-*` features in your Cargo.toml. | ||
| 149 | |||
| 150 | Alternatively, if you would like to use a custom executor implementation, specify it with the `executor` argument. | ||
| 151 | For example: `#[embassy_executor::main(entry = ..., executor = \"some_crate::Executor\")]", | ||
| 152 | ); | ||
| 153 | "" | ||
| 154 | } | ||
| 155 | (Some(x), _) => x, | ||
| 156 | (None, _) => "::embassy_executor::Executor", | ||
| 157 | }; | ||
| 158 | |||
| 159 | let executor = TokenStream::from_str(executor).unwrap_or_else(|e| { | ||
| 160 | error(&mut errors, &f.sig, e); | ||
| 161 | TokenStream::new() | ||
| 162 | }); | ||
| 163 | |||
| 126 | let f_body = f.body; | 164 | let f_body = f.body; |
| 127 | let out = &f.sig.output; | 165 | let out = &f.sig.output; |
| 128 | 166 | ||
| @@ -134,7 +172,7 @@ pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { | |||
| 134 | ::core::mem::transmute(t) | 172 | ::core::mem::transmute(t) |
| 135 | } | 173 | } |
| 136 | 174 | ||
| 137 | let mut executor = ::embassy_executor::Executor::new(); | 175 | let mut executor = #executor::new(); |
| 138 | let executor = unsafe { __make_static(&mut executor) }; | 176 | let executor = unsafe { __make_static(&mut executor) }; |
| 139 | executor.run(|spawner| { | 177 | executor.run(|spawner| { |
| 140 | spawner.must_spawn(__embassy_main(spawner)); | 178 | spawner.must_spawn(__embassy_main(spawner)); |
| @@ -144,7 +182,7 @@ pub fn run(args: TokenStream, item: TokenStream, arch: &Arch) -> TokenStream { | |||
| 144 | Flavor::Wasm => ( | 182 | Flavor::Wasm => ( |
| 145 | quote!(Result<(), wasm_bindgen::JsValue>), | 183 | quote!(Result<(), wasm_bindgen::JsValue>), |
| 146 | quote! { | 184 | quote! { |
| 147 | let executor = ::std::boxed::Box::leak(::std::boxed::Box::new(::embassy_executor::Executor::new())); | 185 | let executor = ::std::boxed::Box::leak(::std::boxed::Box::new(#executor::new())); |
| 148 | 186 | ||
| 149 | executor.start(|spawner| { | 187 | executor.start(|spawner| { |
| 150 | spawner.must_spawn(__embassy_main(spawner)); | 188 | spawner.must_spawn(__embassy_main(spawner)); |
