mirror of
https://github.com/bytedream/serde-inline-default.git
synced 2025-12-16 16:50:44 +01:00
Copy conditional compilation flags onto created constructors (#13)
(Closes #12)
This commit is contained in:
@@ -14,6 +14,10 @@ pub(crate) fn expand_struct(mut item: ItemStruct) -> proc_macro::TokenStream {
|
||||
|
||||
let default: TokenStream = attr.parse_args().unwrap();
|
||||
|
||||
// copy all the same #[cfg] conditional compilations flags for the field onto our built "constructor"
|
||||
// otherwise, it's possible to create a constructor for a type that may be filtered by the same #[cfg]'s, breaking compilation
|
||||
let cfg_attrs = field.attrs.iter().filter(|a| a.path().is_ident("cfg"));
|
||||
|
||||
let fn_name_lit = format!("__serde_inline_default_{}_{}", item.ident, i);
|
||||
let fn_name_ident = Ident::new(&fn_name_lit, Span::call_site());
|
||||
let mut return_type = field.ty.clone();
|
||||
@@ -24,6 +28,7 @@ pub(crate) fn expand_struct(mut item: ItemStruct) -> proc_macro::TokenStream {
|
||||
inline_fns.push(quote! {
|
||||
#[doc(hidden)]
|
||||
#[allow(non_snake_case)]
|
||||
#( #cfg_attrs )*
|
||||
fn #fn_name_ident () -> #return_type {
|
||||
#default
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user