diff options
author | 2023-01-10 21:03:10 +0100 | |
---|---|---|
committer | 2023-03-01 00:33:30 +0100 | |
commit | d6d58b0eb88242cf63724e1420bd29f8a4489916 (patch) | |
tree | d797b67b381947cc375d9c89cdc15d0cc98dee1a /macros/src/codegen/util.rs | |
parent | cd790a94286cdc307d399b7f7a43e305e90de5bf (diff) | |
download | rtic-d6d58b0eb88242cf63724e1420bd29f8a4489916.tar.gz rtic-d6d58b0eb88242cf63724e1420bd29f8a4489916.tar.zst rtic-d6d58b0eb88242cf63724e1420bd29f8a4489916.zip |
Async tasks can now take arguments at spawn again
Diffstat (limited to 'macros/src/codegen/util.rs')
-rw-r--r-- | macros/src/codegen/util.rs | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index 0558d9d1..e121487c 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -1,9 +1,8 @@ -use core::sync::atomic::{AtomicUsize, Ordering}; - use crate::syntax::{ast::App, Context}; +use core::sync::atomic::{AtomicUsize, Ordering}; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::quote; -use syn::{Attribute, Ident}; +use syn::{Attribute, Ident, PatType}; const RTIC_INTERNAL: &str = "__rtic_internal"; @@ -94,6 +93,55 @@ pub fn link_section_uninit() -> TokenStream2 { quote!(#[link_section = #section]) } +/// Regroups the inputs of a task +/// +/// `inputs` could be &[`input: Foo`] OR &[`mut x: i32`, `ref y: i64`] +pub fn regroup_inputs( + inputs: &[PatType], +) -> ( + // args e.g. &[`_0`], &[`_0: i32`, `_1: i64`] + Vec<TokenStream2>, + // tupled e.g. `_0`, `(_0, _1)` + TokenStream2, + // untupled e.g. &[`_0`], &[`_0`, `_1`] + Vec<TokenStream2>, + // ty e.g. `Foo`, `(i32, i64)` + TokenStream2, +) { + if inputs.len() == 1 { + let ty = &inputs[0].ty; + + ( + vec![quote!(_0: #ty)], + quote!(_0), + vec![quote!(_0)], + quote!(#ty), + ) + } else { + let mut args = vec![]; + let mut pats = vec![]; + let mut tys = vec![]; + + for (i, input) in inputs.iter().enumerate() { + let i = Ident::new(&format!("_{}", i), Span::call_site()); + let ty = &input.ty; + + args.push(quote!(#i: #ty)); + + pats.push(quote!(#i)); + + tys.push(quote!(#ty)); + } + + let tupled = { + let pats = pats.clone(); + quote!((#(#pats,)*)) + }; + let ty = quote!((#(#tys,)*)); + (args, tupled, pats, ty) + } +} + /// Get the ident for the name of the task pub fn get_task_name(ctxt: Context, app: &App) -> Ident { let s = match ctxt { |