2022-11-22 09:35:42 +01:00
|
|
|
#![doc = include_str!("../README.md")]
|
2020-10-19 21:15:24 +02:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
2023-06-25 22:24:48 +02:00
|
|
|
use darling::ast::NestedMeta;
|
2021-05-12 01:00:43 +02:00
|
|
|
use proc_macro::TokenStream;
|
2021-04-14 09:25:10 +02:00
|
|
|
|
2022-02-12 00:11:15 +01:00
|
|
|
mod macros;
|
|
|
|
mod util;
|
|
|
|
use macros::*;
|
2023-06-25 22:24:48 +02:00
|
|
|
use syn::parse::{Parse, ParseBuffer};
|
|
|
|
use syn::punctuated::Punctuated;
|
|
|
|
use syn::Token;
|
|
|
|
|
|
|
|
struct Args {
|
|
|
|
meta: Vec<NestedMeta>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Args {
|
|
|
|
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
|
|
|
|
let meta = Punctuated::<NestedMeta, Token![,]>::parse_terminated(input)?;
|
|
|
|
Ok(Args {
|
|
|
|
meta: meta.into_iter().collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2022-11-22 09:35:42 +01:00
|
|
|
/// Declares an async task that can be run by `embassy-executor`. The optional `pool_size` parameter can be used to specify how
|
|
|
|
/// many concurrent tasks can be spawned (default is 1) for the function.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// The following restrictions apply:
|
|
|
|
///
|
|
|
|
/// * The function must be declared `async`.
|
|
|
|
/// * The function must not use generics.
|
|
|
|
/// * The optional `pool_size` attribute must be 1 or greater.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
///
|
|
|
|
/// Declaring a task taking no arguments:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::task]
|
|
|
|
/// async fn mytask() {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Declaring a task with a given pool size:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::task(pool_size = 4)]
|
|
|
|
/// async fn mytask() {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-10-19 21:15:24 +02:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn task(args: TokenStream, item: TokenStream) -> TokenStream {
|
2023-06-25 22:24:48 +02:00
|
|
|
let args = syn::parse_macro_input!(args as Args);
|
2022-02-12 00:11:15 +01:00
|
|
|
let f = syn::parse_macro_input!(item as syn::ItemFn);
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2023-06-25 22:24:48 +02:00
|
|
|
task::run(&args.meta, f).unwrap_or_else(|x| x).into()
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
2020-12-29 00:05:52 +01:00
|
|
|
|
2022-11-22 22:04:42 +01:00
|
|
|
/// Creates a new `executor` instance and declares an application entry point for Cortex-M spawning the corresponding function body as an async task.
|
2022-11-22 09:35:42 +01:00
|
|
|
///
|
|
|
|
/// The following restrictions apply:
|
|
|
|
///
|
|
|
|
/// * The function must accept exactly 1 parameter, an `embassy_executor::Spawner` handle that it can use to spawn additional tasks.
|
|
|
|
/// * The function must be declared `async`.
|
|
|
|
/// * The function must not use generics.
|
|
|
|
/// * Only a single `main` task may be declared.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
/// Spawning a task:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::main]
|
|
|
|
/// async fn main(_s: embassy_executor::Spawner) {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-05-12 01:00:43 +02:00
|
|
|
#[proc_macro_attribute]
|
2022-11-22 22:04:42 +01:00
|
|
|
pub fn main_cortex_m(args: TokenStream, item: TokenStream) -> TokenStream {
|
2023-06-25 22:24:48 +02:00
|
|
|
let args = syn::parse_macro_input!(args as Args);
|
2022-02-12 00:11:15 +01:00
|
|
|
let f = syn::parse_macro_input!(item as syn::ItemFn);
|
2023-06-25 22:24:48 +02:00
|
|
|
main::run(&args.meta, f, main::cortex_m()).unwrap_or_else(|x| x).into()
|
2022-11-22 22:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `executor` instance and declares an application entry point for RISC-V spawning the corresponding function body as an async task.
|
|
|
|
///
|
|
|
|
/// The following restrictions apply:
|
|
|
|
///
|
|
|
|
/// * The function must accept exactly 1 parameter, an `embassy_executor::Spawner` handle that it can use to spawn additional tasks.
|
|
|
|
/// * The function must be declared `async`.
|
|
|
|
/// * The function must not use generics.
|
|
|
|
/// * Only a single `main` task may be declared.
|
|
|
|
///
|
2023-05-16 18:34:38 +02:00
|
|
|
/// A user-defined entry macro can be optionally provided via the `entry` argument to override the default of `riscv_rt::entry`.
|
|
|
|
///
|
2022-11-22 22:04:42 +01:00
|
|
|
/// ## Examples
|
|
|
|
/// Spawning a task:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::main]
|
|
|
|
/// async fn main(_s: embassy_executor::Spawner) {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-05-16 18:34:38 +02:00
|
|
|
///
|
|
|
|
/// Spawning a task using a custom entry macro:
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::main(entry = "esp_riscv_rt::entry")]
|
|
|
|
/// async fn main(_s: embassy_executor::Spawner) {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-11-22 22:04:42 +01:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn main_riscv(args: TokenStream, item: TokenStream) -> TokenStream {
|
2023-06-25 22:24:48 +02:00
|
|
|
let args = syn::parse_macro_input!(args as Args);
|
2022-11-22 22:04:42 +01:00
|
|
|
let f = syn::parse_macro_input!(item as syn::ItemFn);
|
2023-06-25 22:24:48 +02:00
|
|
|
main::run(&args.meta, f, main::riscv(&args.meta))
|
2023-05-16 18:34:38 +02:00
|
|
|
.unwrap_or_else(|x| x)
|
|
|
|
.into()
|
2022-11-22 22:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `executor` instance and declares an application entry point for STD spawning the corresponding function body as an async task.
|
|
|
|
///
|
|
|
|
/// The following restrictions apply:
|
|
|
|
///
|
|
|
|
/// * The function must accept exactly 1 parameter, an `embassy_executor::Spawner` handle that it can use to spawn additional tasks.
|
|
|
|
/// * The function must be declared `async`.
|
|
|
|
/// * The function must not use generics.
|
|
|
|
/// * Only a single `main` task may be declared.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
/// Spawning a task:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::main]
|
|
|
|
/// async fn main(_s: embassy_executor::Spawner) {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn main_std(args: TokenStream, item: TokenStream) -> TokenStream {
|
2023-06-25 22:24:48 +02:00
|
|
|
let args = syn::parse_macro_input!(args as Args);
|
2022-11-22 22:04:42 +01:00
|
|
|
let f = syn::parse_macro_input!(item as syn::ItemFn);
|
2023-06-25 22:24:48 +02:00
|
|
|
main::run(&args.meta, f, main::std()).unwrap_or_else(|x| x).into()
|
2022-11-22 22:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `executor` instance and declares an application entry point for WASM spawning the corresponding function body as an async task.
|
|
|
|
///
|
|
|
|
/// The following restrictions apply:
|
|
|
|
///
|
|
|
|
/// * The function must accept exactly 1 parameter, an `embassy_executor::Spawner` handle that it can use to spawn additional tasks.
|
|
|
|
/// * The function must be declared `async`.
|
|
|
|
/// * The function must not use generics.
|
|
|
|
/// * Only a single `main` task may be declared.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
/// Spawning a task:
|
|
|
|
///
|
|
|
|
/// ``` rust
|
|
|
|
/// #[embassy_executor::main]
|
|
|
|
/// async fn main(_s: embassy_executor::Spawner) {
|
|
|
|
/// // Function body
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn main_wasm(args: TokenStream, item: TokenStream) -> TokenStream {
|
2023-06-25 22:24:48 +02:00
|
|
|
let args = syn::parse_macro_input!(args as Args);
|
2022-11-22 22:04:42 +01:00
|
|
|
let f = syn::parse_macro_input!(item as syn::ItemFn);
|
2023-06-25 22:24:48 +02:00
|
|
|
main::run(&args.meta, f, main::wasm()).unwrap_or_else(|x| x).into()
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|