-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[derive] Implement a IntoBytes-based Hash derive #2159
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## v0.8.x #2159 +/- ##
=======================================
Coverage 87.42% 87.42%
=======================================
Files 16 16
Lines 6115 6115
=======================================
Hits 5346 5346
Misses 769 769 ☔ View full report in Codecov by Sentry. |
Thank you for the contribution! I'm in awe you managed to pass our CI on your first commit! At a glance, this looks great; I'll give a more thorough review tomorrow. I think the right choice is to advocate for the usage pattern of The |
Unfortunately, this can lead to errors. This code, for example: use zerocopy::*;
#[derive(Hash)]
struct Foo; ...produces this error:
Unfortunately, we encourage users to glob-import from |
70dc0e3
to
1358fd7
Compare
That's unfortunate, I do like to preserve |
let type_ident = &ast.ident; | ||
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); | ||
let where_predicates = where_clause.map(|clause| &clause.predicates); | ||
Ok(quote! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last (I think) nit: Could you leave a comment why deferring to impl_block
isn't appropriate here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
The standard library's derive for `Hash` generates a recursive descent into the fields of the type it is applied to. This commit adds a `ByteHash` derive that generates an optimized, byte-oriented `Hash` implementation for types that implement `IntoBytes`. Instead of a recursive descent, the generated implementation makes a single call to `Hasher::write()` in both `Hash::hash()` and `Hash::hash_slice()`, feeding the hasher the bytes of the type or slice all at once. Resolves google#2075
1358fd7
to
05f4718
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me! Thank you for the contribution!
The standard library's derive for
Hash
generates a recursive descent into the fields of the type it is applied to. This commit adds an alternative derive that generates an optimized, byte-orientedHash
implementation for types that implementIntoBytes
. Instead of a recursive descent, the generated implementation makes a single call toHasher::write()
in bothHash::hash()
andHash::hash_slice()
, feeding the hasher the bytes of the type or slice all at once.Resolves #2075