Skip to content
Closed
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
7 changes: 5 additions & 2 deletions enum-display-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn parse_case_name(case_name: &str) -> Case {
pub fn derive(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let DeriveInput {
ident, data, attrs, ..
ident, data, attrs, generics, ..
} = parse_macro_input!(input);

// Should we transform the case of the enum variants?
Expand Down Expand Up @@ -82,13 +82,16 @@ pub fn derive(input: TokenStream) -> TokenStream {
}
});

// Copy generics and bounds
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

// #[allow(unused_qualifications)] is needed
// due to https://github.com/SeedyROM/enum-display/issues/1
// Possibly related to https://github.com/rust-lang/rust/issues/96698
let output = quote! {
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::fmt::Display for #ident {
impl #impl_generics ::core::fmt::Display for #ident #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(
f,
Expand Down
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ mod tests {
DateOfBirth(u32, u32, u32),
}

#[allow(dead_code)]
#[derive(EnumDisplay)]
enum TestEnumWithGenerics<'a, T: Clone> where T: std::fmt::Display {
Name,
Address {
street: &'a T,
city: &'a T,
state: &'a T,
zip: &'a T,
},
DateOfBirth(u32, u32, u32),
}

#[test]
fn test_unit_field_variant() {
assert_eq!(TestEnum::Name.to_string(), "Name");
Expand Down Expand Up @@ -117,4 +130,28 @@ mod tests {
"date-of-birth"
);
}

#[test]
fn test_unit_field_variant_with_generics() {
assert_eq!(TestEnumWithGenerics::<'_, String>::Name.to_string(), "Name");
}

#[test]
fn test_named_fields_variant_with_generics() {
assert_eq!(
TestEnumWithGenerics::Address {
street: &"123 Main St".to_string(),
city: &"Any Town".to_string(),
state: &"CA".to_string(),
zip: &"12345".to_string()
}
.to_string(),
"Address"
);
}

#[test]
fn test_unnamed_fields_variant_with_generics() {
assert_eq!(TestEnumWithGenerics::<'_, String>::DateOfBirth(1, 1, 2000).to_string(), "DateOfBirth");
}
}
Loading