diff options
| author | Ulf Lilleengen <[email protected]> | 2022-11-22 09:35:42 +0100 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-11-22 13:57:41 +0100 |
| commit | f47481787279ce0809cf983deb272815c3654b85 (patch) | |
| tree | f49e7a2251fe451e8651cd782944944426ee94cd | |
| parent | f13639e78c1815e612a868558154d32a1bd8dc4a (diff) | |
doc: add README to embassy-macro
Documents the main and task macros.
| -rw-r--r-- | embassy-macros/README.md | 21 | ||||
| -rw-r--r-- | embassy-macros/src/lib.rs | 49 |
2 files changed, 70 insertions, 0 deletions
diff --git a/embassy-macros/README.md b/embassy-macros/README.md new file mode 100644 index 000000000..d1d6f4cc4 --- /dev/null +++ b/embassy-macros/README.md | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | # embassy-macros | ||
| 2 | |||
| 3 | An [Embassy](https://embassy.dev) project. | ||
| 4 | |||
| 5 | Macros for creating the main entry point and tasks that can be spawned by `embassy-executor`. | ||
| 6 | |||
| 7 | NOTE: The macros are re-exported by the `embassy-executor` crate which should be used instead of adding a direct dependency on the `embassy-macros` crate. | ||
| 8 | |||
| 9 | ## Minimum supported Rust version (MSRV) | ||
| 10 | |||
| 11 | The `task` and `main` macros require the type alias impl trait (TAIT) nightly feature in order to compile. | ||
| 12 | |||
| 13 | ## License | ||
| 14 | |||
| 15 | This work is licensed under either of | ||
| 16 | |||
| 17 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or | ||
| 18 | <http://www.apache.org/licenses/LICENSE-2.0>) | ||
| 19 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) | ||
| 20 | |||
| 21 | at your option. | ||
diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index ec8498f9f..f5df2a269 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #![doc = include_str!("../README.md")] | ||
| 1 | extern crate proc_macro; | 2 | extern crate proc_macro; |
| 2 | 3 | ||
| 3 | use proc_macro::TokenStream; | 4 | use proc_macro::TokenStream; |
| @@ -6,6 +7,36 @@ mod macros; | |||
| 6 | mod util; | 7 | mod util; |
| 7 | use macros::*; | 8 | use macros::*; |
| 8 | 9 | ||
| 10 | /// Declares an async task that can be run by `embassy-executor`. The optional `pool_size` parameter can be used to specify how | ||
| 11 | /// many concurrent tasks can be spawned (default is 1) for the function. | ||
| 12 | /// | ||
| 13 | /// | ||
| 14 | /// The following restrictions apply: | ||
| 15 | /// | ||
| 16 | /// * The function must be declared `async`. | ||
| 17 | /// * The function must not use generics. | ||
| 18 | /// * The optional `pool_size` attribute must be 1 or greater. | ||
| 19 | /// | ||
| 20 | /// | ||
| 21 | /// ## Examples | ||
| 22 | /// | ||
| 23 | /// Declaring a task taking no arguments: | ||
| 24 | /// | ||
| 25 | /// ``` rust | ||
| 26 | /// #[embassy_executor::task] | ||
| 27 | /// async fn mytask() { | ||
| 28 | /// // Function body | ||
| 29 | /// } | ||
| 30 | /// ``` | ||
| 31 | /// | ||
| 32 | /// Declaring a task with a given pool size: | ||
| 33 | /// | ||
| 34 | /// ``` rust | ||
| 35 | /// #[embassy_executor::task(pool_size = 4)] | ||
| 36 | /// async fn mytask() { | ||
| 37 | /// // Function body | ||
| 38 | /// } | ||
| 39 | /// ``` | ||
| 9 | #[proc_macro_attribute] | 40 | #[proc_macro_attribute] |
| 10 | pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { | 41 | pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { |
| 11 | let args = syn::parse_macro_input!(args as syn::AttributeArgs); | 42 | let args = syn::parse_macro_input!(args as syn::AttributeArgs); |
| @@ -14,6 +45,24 @@ pub fn task(args: TokenStream, item: TokenStream) -> TokenStream { | |||
| 14 | task::run(args, f).unwrap_or_else(|x| x).into() | 45 | task::run(args, f).unwrap_or_else(|x| x).into() |
| 15 | } | 46 | } |
| 16 | 47 | ||
| 48 | /// Creates a new `executor` instance and declares an application entry point spawning the corresponding function body as an async task. | ||
| 49 | /// | ||
| 50 | /// The following restrictions apply: | ||
| 51 | /// | ||
| 52 | /// * The function must accept exactly 1 parameter, an `embassy_executor::Spawner` handle that it can use to spawn additional tasks. | ||
| 53 | /// * The function must be declared `async`. | ||
| 54 | /// * The function must not use generics. | ||
| 55 | /// * Only a single `main` task may be declared. | ||
| 56 | /// | ||
| 57 | /// ## Examples | ||
| 58 | /// Spawning a task: | ||
| 59 | /// | ||
| 60 | /// ``` rust | ||
| 61 | /// #[embassy_executor::main] | ||
| 62 | /// async fn main(_s: embassy_executor::Spawner) { | ||
| 63 | /// // Function body | ||
| 64 | /// } | ||
| 65 | /// ``` | ||
| 17 | #[proc_macro_attribute] | 66 | #[proc_macro_attribute] |
| 18 | pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { | 67 | pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { |
| 19 | let args = syn::parse_macro_input!(args as syn::AttributeArgs); | 68 | let args = syn::parse_macro_input!(args as syn::AttributeArgs); |
