Skip to content

Commit

Permalink
Add support for lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
io12 committed Mar 10, 2020
1 parent 87f23db commit 527b729
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
20 changes: 20 additions & 0 deletions multi-eq-use-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
};
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 527b729

Please sign in to comment.