Skip to content

Commit 634131a

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
strong hash: Make it work with generics
Summary: ^ Reviewed By: IanChilds Differential Revision: D73628473 fbshipit-source-id: 1d94f3f002cb5f269561b18669b8d66664f25000
1 parent cfeb60c commit 634131a

File tree

2 files changed

+23
-2
lines changed
  • gazebo

2 files changed

+23
-2
lines changed

gazebo/strong_hash_derive/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ use syn::spanned::Spanned;
1717

1818
#[proc_macro_derive(StrongHash)]
1919
pub fn derive_hash(input: TokenStream) -> TokenStream {
20-
// TODO(scottcao): Make derive work with generics that implement StrongHash
21-
let input = parse_macro_input!(input as DeriveInput);
20+
let mut input = parse_macro_input!(input as DeriveInput);
2221
let name = input.ident;
22+
let ty_params = input
23+
.generics
24+
.type_params()
25+
.map(|p| p.ident.clone())
26+
.collect::<Vec<_>>();
27+
let where_clause = input.generics.make_where_clause();
28+
for ty_param in ty_params {
29+
where_clause
30+
.predicates
31+
.push(syn::parse_quote! { #ty_param: strong_hash::StrongHash });
32+
}
2333
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
2434
let output = match input.data {
2535
syn::Data::Struct(data) => {

gazebo/strong_hash_tests/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,14 @@ fn test_strong_hash_derive() {
9696
assert_ne!(bar_hash, foo_bar_bar_hash);
9797
assert_ne!(foo_bar_foo_hash, foo_bar_bar_hash);
9898
}
99+
100+
#[test]
101+
fn test_generics() {
102+
#[derive(StrongHash)]
103+
struct Foo<T>(T);
104+
105+
fn check_is_implemented<T: StrongHash>(_t: &T) {}
106+
107+
let foo = Foo(1u8);
108+
check_is_implemented(&foo);
109+
}

0 commit comments

Comments
 (0)