diff options
author | 2023-06-08 13:44:49 +0200 | |
---|---|---|
committer | 2023-06-08 14:14:36 +0200 | |
commit | a7653cc05092aa5b009172c531e4f729c678a858 (patch) | |
tree | 3ef77dd6af020d06cc37b25faf277834f94ef093 /rtic-macros/src/syntax/parse/init.rs | |
parent | 599793829377a64603e93a1136360f17d3bade93 (diff) | |
download | rtic-a7653cc05092aa5b009172c531e4f729c678a858.tar.gz rtic-a7653cc05092aa5b009172c531e4f729c678a858.tar.zst rtic-a7653cc05092aa5b009172c531e4f729c678a858.zip |
allow init and idle to be externed
Diffstat (limited to 'rtic-macros/src/syntax/parse/init.rs')
-rw-r--r-- | rtic-macros/src/syntax/parse/init.rs | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/rtic-macros/src/syntax/parse/init.rs b/rtic-macros/src/syntax/parse/init.rs index 0aea20bd..59d00937 100644 --- a/rtic-macros/src/syntax/parse/init.rs +++ b/rtic-macros/src/syntax/parse/init.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream as TokenStream2; -use syn::{parse, ItemFn}; +use syn::{parse, ForeignItemFn, ItemFn, Stmt}; use crate::syntax::{ ast::{Init, InitArgs}, @@ -35,6 +35,44 @@ impl Init { stmts: item.block.stmts, user_shared_struct, user_local_struct, + is_extern: false, + }); + } + } + } + } + + Err(parse::Error::new( + span, + format!( + "the `#[init]` function must have signature `fn({name}::Context) -> (Shared resources struct, Local resources struct)`" + ), + )) + } + + pub(crate) fn parse_foreign(args: InitArgs, item: ForeignItemFn) -> parse::Result<Self> { + let valid_signature = + util::check_foreign_fn_signature(&item, false) && item.sig.inputs.len() == 1; + + let span = item.sig.ident.span(); + + let name = item.sig.ident.to_string(); + + if valid_signature { + if let Ok((user_shared_struct, user_local_struct)) = + util::type_is_init_return(&item.sig.output) + { + if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) { + if rest.is_empty() { + return Ok(Init { + args, + attrs: item.attrs, + context, + name: item.sig.ident, + stmts: Vec::<Stmt>::new(), + user_shared_struct, + user_local_struct, + is_extern: true, }); } } |