Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Generics with Enum Is #305

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion strum_macros/src/macros/enum_is.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn enum_is_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
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;

Expand Down Expand Up @@ -35,7 +36,7 @@ pub fn enum_is_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
.collect();

Ok(quote! {
impl #enum_name {
impl #impl_generics #enum_name #ty_generics #where_clause {
#(#variants)*
}
}
Expand Down
17 changes: 15 additions & 2 deletions strum_tests/tests/enum_is.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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());
Expand Down