diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-04-12 14:18:04 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-04-12 14:18:04 +0200 |
| commit | 85c7f4e7ef43bb782356696d5bc695c8807af928 (patch) | |
| tree | 4296682362c9becce91a2e49353ad0bc86faf388 | |
| parent | 75337fdf39e1806a0ad325b4e450163fe9fe4a92 (diff) | |
| parent | 7671605452658cedd6e460bb7c86ef450fe6a04c (diff) | |
Merge pull request #138 from lulf/std-main-macro
Add std version of embassy::main
| -rw-r--r-- | embassy-macros/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-macros/src/lib.rs | 66 | ||||
| -rw-r--r-- | embassy-std/Cargo.toml | 3 |
3 files changed, 69 insertions, 1 deletions
diff --git a/embassy-macros/Cargo.toml b/embassy-macros/Cargo.toml index 20dc7c381..6da2a5868 100644 --- a/embassy-macros/Cargo.toml +++ b/embassy-macros/Cargo.toml | |||
| @@ -17,3 +17,4 @@ proc-macro = true | |||
| 17 | stm32 = [] | 17 | stm32 = [] |
| 18 | nrf = [] | 18 | nrf = [] |
| 19 | rp = [] | 19 | rp = [] |
| 20 | std = [] | ||
diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index 885eac118..8f35cc9ef 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs | |||
| @@ -280,3 +280,69 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 280 | }; | 280 | }; |
| 281 | result.into() | 281 | result.into() |
| 282 | } | 282 | } |
| 283 | |||
| 284 | #[cfg(feature = "std")] | ||
| 285 | #[proc_macro_attribute] | ||
| 286 | pub fn main(_: TokenStream, item: TokenStream) -> TokenStream { | ||
| 287 | let task_fn = syn::parse_macro_input!(item as syn::ItemFn); | ||
| 288 | |||
| 289 | let mut fail = false; | ||
| 290 | if task_fn.sig.asyncness.is_none() { | ||
| 291 | task_fn | ||
| 292 | .sig | ||
| 293 | .span() | ||
| 294 | .unwrap() | ||
| 295 | .error("task functions must be async") | ||
| 296 | .emit(); | ||
| 297 | fail = true; | ||
| 298 | } | ||
| 299 | if !task_fn.sig.generics.params.is_empty() { | ||
| 300 | task_fn | ||
| 301 | .sig | ||
| 302 | .span() | ||
| 303 | .unwrap() | ||
| 304 | .error("main function must not be generic") | ||
| 305 | .emit(); | ||
| 306 | fail = true; | ||
| 307 | } | ||
| 308 | |||
| 309 | let args = task_fn.sig.inputs.clone(); | ||
| 310 | |||
| 311 | if args.len() != 1 { | ||
| 312 | task_fn | ||
| 313 | .sig | ||
| 314 | .span() | ||
| 315 | .unwrap() | ||
| 316 | .error("main function must have one argument") | ||
| 317 | .emit(); | ||
| 318 | fail = true; | ||
| 319 | } | ||
| 320 | |||
| 321 | if fail { | ||
| 322 | return TokenStream::new(); | ||
| 323 | } | ||
| 324 | |||
| 325 | let task_fn_body = task_fn.block.clone(); | ||
| 326 | |||
| 327 | let result = quote! { | ||
| 328 | #[embassy::task] | ||
| 329 | async fn __embassy_main(#args) { | ||
| 330 | #task_fn_body | ||
| 331 | } | ||
| 332 | |||
| 333 | fn main() -> ! { | ||
| 334 | unsafe fn make_static<T>(t: &mut T) -> &'static mut T { | ||
| 335 | ::core::mem::transmute(t) | ||
| 336 | } | ||
| 337 | |||
| 338 | let mut executor = ::embassy_std::Executor::new(); | ||
| 339 | let executor = unsafe { make_static(&mut executor) }; | ||
| 340 | |||
| 341 | executor.run(|spawner| { | ||
| 342 | spawner.spawn(__embassy_main(spawner)).unwrap(); | ||
| 343 | }) | ||
| 344 | |||
| 345 | } | ||
| 346 | }; | ||
| 347 | result.into() | ||
| 348 | } | ||
diff --git a/embassy-std/Cargo.toml b/embassy-std/Cargo.toml index 0a59999c0..2a95838e8 100644 --- a/embassy-std/Cargo.toml +++ b/embassy-std/Cargo.toml | |||
| @@ -6,4 +6,5 @@ edition = "2018" | |||
| 6 | 6 | ||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | embassy = { version = "0.1.0", path = "../embassy", features = ["std"] } | 8 | embassy = { version = "0.1.0", path = "../embassy", features = ["std"] } |
| 9 | lazy_static = "1.4.0" \ No newline at end of file | 9 | embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["std"]} |
| 10 | lazy_static = "1.4.0" | ||
