File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,19 @@ use syn::spanned::Spanned;
17
17
18
18
#[ proc_macro_derive( StrongHash ) ]
19
19
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 ) ;
22
21
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
+ }
23
33
let ( impl_generics, type_generics, where_clause) = input. generics . split_for_impl ( ) ;
24
34
let output = match input. data {
25
35
syn:: Data :: Struct ( data) => {
Original file line number Diff line number Diff line change @@ -96,3 +96,14 @@ fn test_strong_hash_derive() {
96
96
assert_ne ! ( bar_hash, foo_bar_bar_hash) ;
97
97
assert_ne ! ( foo_bar_foo_hash, foo_bar_bar_hash) ;
98
98
}
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
+ }
You can’t perform that action at this time.
0 commit comments