-
Notifications
You must be signed in to change notification settings - Fork 48
Conditional proc-macros #192
Copy link
Copy link
Open
Description
The library I'm working on, has the requirement of being able to be used by rust and non-rust users.
I didn't like the idea of creating a separate crate in the project just as a wrapper for my main library, so I created a proc-macro crate that makes the safe-ffi macros conditional based on a feature flag.
It seems to work well because the file size of the outputs decreases drastically. The ".a" file goes from 26,3 MiB to 25,7 MiB and the ".rlib" file from 641,7 KiB to 51,8 KiB.
This seems like a improvement to me, but it could also be that this would not make any difference to the library users. The Rust compiler could just remove this unneeded code, when it's not used.
use proc_macro::TokenStream;
use syn::parse_macro_input;
#[proc_macro_attribute]
pub fn ffi_export (
attrs: TokenStream,
input: TokenStream,
) -> TokenStream
{
parse_macro_input!(attrs as syn::parse::Nothing);
if cfg!(feature = "ffi_compile") {
format!("#[::safer_ffi::ffi_export]\n{}", input).parse().unwrap()
} else {
input
}
}
#[proc_macro_attribute]
pub fn derive_ReprC (
attrs: TokenStream,
input: TokenStream,
) -> TokenStream
{
//eprintln!("attrs: \"{}\"", attrs);
if cfg!(feature = "ffi_compile") {
format!("#[::safer_ffi::derive_ReprC]\n#[repr({})]\n{}", attrs, input).parse().unwrap()
} else {
input
}
}[features]
default = ["headers"]
headers = ["safer-ffi/headers", "ffi_compile"]
ffi_compile = ["safer-ffi-deactivate/ffi_compile"]Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels