From b2bd7635b4ce1eb8f8f02d25913b6d1a7071f603 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 24 May 2024 11:10:07 +0200 Subject: [PATCH 1/2] riot-rs-macros: factor out `find_crate()` from `riot_rs_crate()` --- src/riot-rs-macros/src/utils.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/riot-rs-macros/src/utils.rs b/src/riot-rs-macros/src/utils.rs index 4f4df38fc..a7400b699 100644 --- a/src/riot-rs-macros/src/utils.rs +++ b/src/riot-rs-macros/src/utils.rs @@ -10,16 +10,27 @@ const RIOT_RS_CRATE_NAME: &str = "riot-rs"; /// this function is called. /// - Panics if `riot-rs` is used as a dependency of itself. pub fn riot_rs_crate() -> syn::Ident { - let riot_rs_crate = proc_macro_crate::crate_name(RIOT_RS_CRATE_NAME) - .unwrap_or_else(|_| panic!("{RIOT_RS_CRATE_NAME} should be present in `Cargo.toml`")); + find_crate(RIOT_RS_CRATE_NAME) + .unwrap_or_else(|| panic!("{RIOT_RS_CRATE_NAME} should be present in `Cargo.toml`")) +} - match riot_rs_crate { - proc_macro_crate::FoundCrate::Itself => { - panic!( - "{} cannot be used as a dependency of itself", - env!("CARGO_CRATE_NAME"), - ); +/// Returns a [`struct@syn::Ident`] identifying the `name` dependency (or None). +/// +/// # Panics +/// +/// - Panics if `name` is used as a dependency of itself. +pub fn find_crate(name: &str) -> Option { + if let Ok(crate_) = proc_macro_crate::crate_name(name) { + match crate_ { + proc_macro_crate::FoundCrate::Itself => { + panic!( + "{} cannot be used as a dependency of itself", + env!("CARGO_CRATE_NAME"), + ); + } + proc_macro_crate::FoundCrate::Name(crate_) => Some(format_ident!("{crate_}")), } - proc_macro_crate::FoundCrate::Name(riot_rs_crate) => format_ident!("{riot_rs_crate}"), + } else { + None } } From 413c9713567a5cb8193f0d8550e0627f6e36b50e Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 24 May 2024 11:32:03 +0200 Subject: [PATCH 2/2] riot-rs-macros: make `thread` support both `riot-rs` and `riot-rs-deps` --- src/riot-rs-macros/src/thread.rs | 12 ++++++++++-- src/riot-rs-macros/src/utils.rs | 7 ++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/riot-rs-macros/src/thread.rs b/src/riot-rs-macros/src/thread.rs index 25ae9371a..cb95d0a4d 100644 --- a/src/riot-rs-macros/src/thread.rs +++ b/src/riot-rs-macros/src/thread.rs @@ -39,6 +39,8 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream { use quote::quote; + use crate::utils::find_crate; + let mut attrs = Attributes::default(); let thread_parser = syn::meta::parser(|meta| attrs.parse(&meta)); syn::parse_macro_input!(args with thread_parser); @@ -62,13 +64,19 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream { priority, } = Parameters::from(attrs); - let riot_rs_crate = utils::riot_rs_crate(); + let thread_crate = { + match (find_crate("riot-rs"), find_crate("riot-rs-threads")) { + (Some(riot_rs), _) => quote! { #riot_rs::thread }, + (None, Some(riot_rs_threads)) => quote! { #riot_rs_threads }, + _ => panic!(r#"neither "riot-rs" nor "riot-rs-threads" found in dependencies!"#), + } + }; let expanded = quote! { #no_mangle_attr #thread_function - #riot_rs_crate::thread::autostart_thread!(#fn_name, stacksize = #stack_size, priority = #priority); + #thread_crate::autostart_thread!(#fn_name, stacksize = #stack_size, priority = #priority); }; TokenStream::from(expanded) diff --git a/src/riot-rs-macros/src/utils.rs b/src/riot-rs-macros/src/utils.rs index a7400b699..ef361885a 100644 --- a/src/riot-rs-macros/src/utils.rs +++ b/src/riot-rs-macros/src/utils.rs @@ -14,7 +14,7 @@ pub fn riot_rs_crate() -> syn::Ident { .unwrap_or_else(|| panic!("{RIOT_RS_CRATE_NAME} should be present in `Cargo.toml`")) } -/// Returns a [`struct@syn::Ident`] identifying the `name` dependency (or None). +/// Returns a [`struct@syn::Ident`] identifying the `name` dependency (or `None`). /// /// # Panics /// @@ -23,10 +23,7 @@ pub fn find_crate(name: &str) -> Option { if let Ok(crate_) = proc_macro_crate::crate_name(name) { match crate_ { proc_macro_crate::FoundCrate::Itself => { - panic!( - "{} cannot be used as a dependency of itself", - env!("CARGO_CRATE_NAME"), - ); + panic!("{name} cannot be used as a dependency of itself"); } proc_macro_crate::FoundCrate::Name(crate_) => Some(format_ident!("{crate_}")), }