Skip to content

Commit

Permalink
feat(thread-macro): accept Expr as macro parameters (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 authored Jun 14, 2024
2 parents 9226d1c + ebec469 commit c4d866c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 35 deletions.
1 change: 1 addition & 0 deletions examples/threading/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn thread0() {
println!("Hello from thread 0");
}

// `stacksize` and `priority` can be arbitrary expressions.
#[riot_rs::thread(autostart, stacksize = 4096, priority = 2)]
fn thread1() {
println!("Hello from thread 1");
Expand Down
43 changes: 9 additions & 34 deletions src/riot-rs-macros/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/// This starts a thread with a stack size of 1024 bytes and a priority of 2:
///
/// ```ignore
/// // `stacksize` and `priority` can be arbitrary expressions.
/// #[riot_rs::thread(stacksize = 1024, priority = 2)]
/// fn print_hello_world() {
/// println!("Hello world!");
Expand Down Expand Up @@ -84,16 +85,16 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {

mod thread {
pub struct Parameters {
pub stack_size: u64,
pub priority: u8,
pub stack_size: syn::Expr,
pub priority: syn::Expr,
}

impl Default for Parameters {
fn default() -> Self {
// TODO: proper values
Self {
stack_size: 2048,
priority: 1,
stack_size: syn::parse_quote!{ 2048 },
priority: syn::parse_quote!{ 1 },
}
}
}
Expand All @@ -102,13 +103,8 @@ mod thread {
fn from(attrs: Attributes) -> Self {
let default = Self::default();

let stack_size = attrs.stack_size.map_or(default.stack_size, |l| {
parse_base10_or_panic(&l, "stack_size")
});

let priority = attrs
.priority
.map_or(default.priority, |l| parse_base10_or_panic(&l, "priority"));
let stack_size = attrs.stack_size.unwrap_or(default.stack_size);
let priority = attrs.priority.unwrap_or(default.priority);

Self {
stack_size,
Expand All @@ -117,32 +113,11 @@ mod thread {
}
}

/// Parse a base-10 integer literal.
///
/// # Panics
///
/// Panics if parsing fails.
fn parse_base10_or_panic<I>(lit_int: &syn::LitInt, attr: &str) -> I
where
I: core::str::FromStr,
<I as core::str::FromStr>::Err: std::fmt::Display,
{
if let Ok(int) = lit_int.base10_parse() {
assert!(
lit_int.suffix().is_empty(),
"`{attr}` must be a base-10 integer without a suffix",
);
int
} else {
panic!("`{attr}` must be a base-10 integer");
}
}

#[derive(Default)]
pub struct Attributes {
pub autostart: bool,
pub stack_size: Option<syn::LitInt>,
pub priority: Option<syn::LitInt>,
pub stack_size: Option<syn::Expr>,
pub priority: Option<syn::Expr>,
pub no_mangle: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion src/riot-rs-threads/src/autostart_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// The thread is given a `stacksize`-byte stack, and has priority `priority`.
#[macro_export]
macro_rules! autostart_thread {
($fn_name:ident, stacksize = $stacksize:literal, priority = $priority:literal) => {
($fn_name:ident, stacksize = $stacksize:expr, priority = $priority:expr) => {
$crate::macro_reexports::paste::paste! {
#[$crate::macro_reexports::linkme::distributed_slice($crate::THREAD_FNS)]
#[linkme(crate = $crate::macro_reexports::linkme)]
Expand Down

0 comments on commit c4d866c

Please sign in to comment.