diff --git a/multi-eq-use-tests/tests/tests.rs b/multi-eq-use-tests/tests/tests.rs index 41c187c..45a8680 100644 --- a/multi-eq-use-tests/tests/tests.rs +++ b/multi-eq-use-tests/tests/tests.rs @@ -163,3 +163,23 @@ fn test_struct_attr_ignore() { println!("{}", TestStruct::new(0, false).b); } + +#[test] +fn test_struct_lifetime() { + #[derive(TestEq)] + struct TestStruct<'a> { + #[test_eq(cmp = "eq")] + s: &'a str, + } + + impl<'a> TestStruct<'a> { + fn new(s: &'a str) -> Self { + Self { s } + } + } + + assert!(TestStruct::new("foo").test_eq(&TestStruct::new("foo"))); + assert!(TestStruct::new("bar").test_eq(&TestStruct::new("bar"))); + assert!(!TestStruct::new("foo").test_eq(&TestStruct::new("bar"))); + assert!(!TestStruct::new("bar").test_eq(&TestStruct::new("foo"))); +} diff --git a/src/lib.rs b/src/lib.rs index 1b88094..a4252ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,10 +237,15 @@ macro_rules! multi_eq_make_derive { Some(name) => format_ident!("{}", name), None => format_ident!("{}", stringify!($method_name)), }; + let refr = if let syn::Type::Reference(_) = field.ty { + quote!() + } else { + quote!(&) + }; if field.attrs.iter().any(is_ignore) { acc } else { - quote!(#acc && self.#name.#method_name(&other.#name)) + quote!(#acc && self.#name.#method_name(#refr other.#name)) } }) }; @@ -339,8 +344,10 @@ macro_rules! multi_eq_make_derive { syn::Data::Union(_) => panic!("unions are not supported"), }; + let generics = input.generics; + let ret = quote! { - impl $trait_name for #input_ident { + impl #generics $trait_name for #input_ident #generics { fn $method_name(&self, other: &Self) -> bool { #expr }