diff options
| author | Oleksandr Babak <alexanderbabak@proton.me> | 2025-03-22 11:57:13 +0100 |
|---|---|---|
| committer | Emil Fresk <emil.fresk@gmail.com> | 2025-03-27 14:47:11 +0000 |
| commit | ff3b1ad5273d16714f7d786d48b3bd06d433a9e6 (patch) | |
| tree | 32ddff0caaf05ae35773dc8359ae28cc2fbea332 /rtic-macros/src | |
| parent | 267ed9dafd820eca276f027d1763bc53f588cf50 (diff) | |
feat: allow diverding software tasks with `'static` context (they never return)
Diffstat (limited to 'rtic-macros/src')
| -rw-r--r-- | rtic-macros/src/codegen/software_tasks.rs | 36 | ||||
| -rw-r--r-- | rtic-macros/src/syntax/ast.rs | 3 | ||||
| -rw-r--r-- | rtic-macros/src/syntax/parse/software_task.rs | 13 |
3 files changed, 37 insertions, 15 deletions
diff --git a/rtic-macros/src/codegen/software_tasks.rs b/rtic-macros/src/codegen/software_tasks.rs index 34fc851..71caec0 100644 --- a/rtic-macros/src/codegen/software_tasks.rs +++ b/rtic-macros/src/codegen/software_tasks.rs @@ -31,24 +31,38 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { mod_app.push(constructor); } - if !&task.is_extern { + if !task.is_extern { let context = &task.context; let attrs = &task.attrs; let cfgs = &task.cfgs; let stmts = &task.stmts; let inputs = &task.inputs; - user_tasks.push(quote!( - #(#attrs)* - #(#cfgs)* - #[allow(non_snake_case)] - async fn #name<'a>(#context: #name::Context<'a> #(,#inputs)*) { - use rtic::Mutex as _; - use rtic::mutex::prelude::*; + user_tasks.push(if task.is_bottom { + quote!( + #(#attrs)* + #(#cfgs)* + #[allow(non_snake_case)] + async fn #name(#context: #name::Context<'static> #(,#inputs)*) { + use rtic::Mutex as _; + use rtic::mutex::prelude::*; - #(#stmts)* - } - )); + #(#stmts)* + } + ) + } else { + quote!( + #(#attrs)* + #(#cfgs)* + #[allow(non_snake_case)] + async fn #name<'a>(#context: #name::Context<'a> #(,#inputs)*) { + use rtic::Mutex as _; + use rtic::mutex::prelude::*; + + #(#stmts)* + } + ) + }); } root.push(module::codegen(Context::SoftwareTask(name), app, analysis)); diff --git a/rtic-macros/src/syntax/ast.rs b/rtic-macros/src/syntax/ast.rs index bc892e3..ad73a89 100644 --- a/rtic-macros/src/syntax/ast.rs +++ b/rtic-macros/src/syntax/ast.rs @@ -236,6 +236,9 @@ pub struct SoftwareTask { /// The task is declared externally pub is_extern: bool, + + /// The task will never return `Poll::Ready` + pub is_bottom: bool, } /// Software task metadata diff --git a/rtic-macros/src/syntax/parse/software_task.rs b/rtic-macros/src/syntax/parse/software_task.rs index 769aa65..175769c 100644 --- a/rtic-macros/src/syntax/parse/software_task.rs +++ b/rtic-macros/src/syntax/parse/software_task.rs @@ -8,8 +8,9 @@ use crate::syntax::{ impl SoftwareTask { pub(crate) fn parse(args: SoftwareTaskArgs, item: ItemFn) -> parse::Result<Self> { + let is_bottom = util::type_is_bottom(&item.sig.output); let valid_signature = util::check_fn_signature(&item, true) - && util::type_is_unit(&item.sig.output) + && (util::type_is_unit(&item.sig.output) || is_bottom) && item.sig.asyncness.is_some(); let span = item.sig.ident.span(); @@ -28,13 +29,14 @@ impl SoftwareTask { inputs, stmts: item.block.stmts, is_extern: false, + is_bottom, }); } } Err(parse::Error::new( span, - format!("this task handler must have type signature `async fn({name}::Context, ..)`"), + format!("this task handler must have type signature `async fn({name}::Context, ..)` or `async fn({name}::Context, ..) -> !`"), )) } } @@ -44,8 +46,10 @@ impl SoftwareTask { args: SoftwareTaskArgs, item: ForeignItemFn, ) -> parse::Result<Self> { + let is_bottom = util::type_is_bottom(&item.sig.output); + let valid_signature = util::check_foreign_fn_signature(&item, true) - && util::type_is_unit(&item.sig.output) + && (util::type_is_unit(&item.sig.output) || is_bottom) && item.sig.asyncness.is_some(); let span = item.sig.ident.span(); @@ -64,13 +68,14 @@ impl SoftwareTask { inputs, stmts: Vec::<Stmt>::new(), is_extern: true, + is_bottom, }); } } Err(parse::Error::new( span, - format!("this task handler must have type signature `async fn({name}::Context, ..)`"), + format!("this task handler must have type signature `async fn({name}::Context, ..)` or `async fn({name}::Context, ..) -> !`"), )) } } |
