-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(macros): add an attribute macro for init functions
- Loading branch information
1 parent
ff59493
commit 574ca7b
Showing
7 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ mod utils; | |
|
||
use proc_macro::TokenStream; | ||
|
||
include!("main_macro.rs"); | ||
include!("thread.rs"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/// Registers a function acting as an entrypoint for an application. | ||
/// | ||
/// Peripheral groups defined with the `define_peripherals!` macro can be obtained in that function | ||
/// by taking them as parameters. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// None. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This macro panics when the `riot-rs` crate cannot be found as a dependency of the crate where | ||
/// this macro is used. | ||
#[proc_macro_attribute] | ||
pub fn main(_args: TokenStream, item: TokenStream) -> TokenStream { | ||
use std::iter; | ||
|
||
use quote::quote; | ||
|
||
let task_function = syn::parse_macro_input!(item as syn::ItemFn); | ||
let function_param_count = task_function.sig.inputs.len(); | ||
|
||
// Add an extractor for each function parameter, so that the function receives the requested | ||
// peripherals | ||
let extractors = | ||
iter::repeat(quote! { peripherals.into_peripherals() }).take(function_param_count); | ||
|
||
let riot_rs_crate = utils::riot_rs_crate(); | ||
|
||
let expanded = quote! { | ||
#[#riot_rs_crate::embassy::distributed_slice(#riot_rs_crate::embassy::EMBASSY_TASKS)] | ||
#[linkme(crate = #riot_rs_crate::embassy::linkme)] | ||
fn __main(spawner: &Spawner, mut peripherals: &mut #riot_rs_crate::embassy::arch::OptionalPeripherals) { | ||
use #riot_rs_crate::define_peripherals::IntoPeripherals; | ||
spawner.spawn(main(#(#extractors),*)).unwrap(); | ||
} | ||
|
||
#[embassy_executor::task] | ||
#task_function | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters