From 87367237ef49b8e866f8006630c704791bb6d9a6 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Thu, 12 Oct 2023 10:42:24 -0400 Subject: [PATCH] Fix Generics Issue with enum_is --- strum_macros/src/macros/enum_is.rs | 3 ++- strum_tests/tests/enum_is.rs | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/strum_macros/src/macros/enum_is.rs b/strum_macros/src/macros/enum_is.rs index b69fa0c5..71949634 100644 --- a/strum_macros/src/macros/enum_is.rs +++ b/strum_macros/src/macros/enum_is.rs @@ -8,6 +8,7 @@ pub fn enum_is_inner(ast: &DeriveInput) -> syn::Result { Data::Enum(v) => &v.variants, _ => return Err(non_enum_error()), }; + let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); let enum_name = &ast.ident; @@ -35,7 +36,7 @@ pub fn enum_is_inner(ast: &DeriveInput) -> syn::Result { .collect(); Ok(quote! { - impl #enum_name { + impl #impl_generics #enum_name #ty_generics #where_clause { #(#variants)* } } diff --git a/strum_tests/tests/enum_is.rs b/strum_tests/tests/enum_is.rs index 789881ce..4e997fea 100644 --- a/strum_tests/tests/enum_is.rs +++ b/strum_tests/tests/enum_is.rs @@ -1,7 +1,12 @@ +use std::borrow::Cow; use strum::EnumIs; mod core {} // ensure macros call `::core` - +#[derive(EnumIs)] +enum LifeTimeTest<'a>{ + One(Cow<'a, str>), + Two(&'a str) +} #[derive(EnumIs)] enum Foo { Unit, @@ -16,7 +21,15 @@ enum Foo { #[allow(dead_code)] Disabled, } - +#[test] +fn generics_test(){ + let foo = LifeTimeTest::One(Cow::Borrowed("Hello")); + assert!(foo.is_one()); + let foo = LifeTimeTest::Two("Hello"); + assert!(foo.is_two()); + let foo = LifeTimeTest::One(Cow::Owned("Hello".to_string())); + assert!(foo.is_one()); +} #[test] fn simple_test() { assert!(Foo::Unit.is_unit());