From f35593127d30cbc779150281aef917fa00a2a31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCn=20=C3=96zerk?= Date: Thu, 28 Nov 2024 09:22:09 +0300 Subject: [PATCH] preserve other macros (#39) --- src/type_state.rs | 8 ++++++++ tests/simple_example.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/type_state.rs b/src/type_state.rs index e53d073..9f40452 100644 --- a/src/type_state.rs +++ b/src/type_state.rs @@ -124,6 +124,13 @@ pub fn type_state_inner(args: TokenStream, input: TokenStream) -> TokenStream { .map(|ident| quote!(::core::marker::PhantomData #ident>)) .collect::>(); + // Get the struct's attributes (other macros) excluding the #[type_state] macro + let attrs: Vec<_> = input_struct + .attrs + .iter() + .filter(|attr| !attr.path().is_ident("type_state")) + .collect(); + // Generate the final output let output = quote! { mod #sealed_mod_name { @@ -138,6 +145,7 @@ pub fn type_state_inner(args: TokenStream, input: TokenStream) -> TokenStream { #(#trait_impls)* + #(#attrs)* #[allow(clippy::type_complexity)] #visibility struct #struct_name<#combined_generics> #merged_where_clause diff --git a/tests/simple_example.rs b/tests/simple_example.rs index 985c243..c18652d 100644 --- a/tests/simple_example.rs +++ b/tests/simple_example.rs @@ -14,6 +14,7 @@ enum Race { Human, } +#[derive(Debug)] #[type_state(states = (Initial, RaceSet, LevelSet, SkillSlotsSet), slots = (Initial))] struct PlayerBuilder { race: Option, @@ -133,4 +134,11 @@ mod tests { assert_eq!(player.level, another_player.level); assert_eq!(player.skill_slots, another_player.skill_slots); } + + #[test] + fn other_macros_are_preserved() { + let player = PlayerBuilder::new(); + println!("{:?}", player); // ensures `#[derive(Debug)]` is preserved + assert_eq!(player.level, None); + } }