-
Notifications
You must be signed in to change notification settings - Fork 144
Add Hash derive #532
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
Open
fredszaq
wants to merge
12
commits into
JelteF:master
Choose a base branch
from
fredszaq:hash-derive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Hash derive #532
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
98ce6f2
add .idea to gitignore (rustrover and other jetbrains ide files)
fredszaq cd7f01a
first version of Hash derive, supporting skipping fields
fredszaq 4be4742
better tests and doc for Hash
fredszaq bedc307
add #[hash(with(func)] attribute to Hash
fredszaq 0bc53ba
add hash to changelog and readme
fredszaq 71be6cf
fix ci
fredszaq ef8aad5
typo
fredszaq aecafc3
add missing section in changelog
fredszaq 832dfba
move all hash tests to single file
fredszaq e850d90
Merge remote-tracking branch 'JelteF/master' into hash-derive
fredszaq 3fd8115
add more tests around possible hash collisions
fredszaq a3b0311
Merge remote-tracking branch 'origin/master' into hash-derive
fredszaq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,6 @@ gh-pages | |
| # git files | ||
| .swp | ||
| /tags | ||
|
|
||
| # RustRover/Other jetbrains IDE files | ||
| .idea/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| # Using `#[derive(Hash)]` | ||
|
|
||
| Deriving `Hash` works by hashing values according to their type structure. | ||
|
|
||
| ## Structural hashing | ||
|
|
||
| Deriving `Hash` for enums/structs works in a similar way to the one in `std`, | ||
| by hashing all the available fields, but, in contrast: | ||
| 1. Does not overconstrain generic parameters. | ||
| 2. Allows to ignore fields, whole structs or enum variants via `#[hash(skip)]` attribute. | ||
| 3. Allows to use a custom hash function for a field via `#[hash(with(function))]` attribute. | ||
|
|
||
| ### Structs | ||
|
|
||
| For structs all the available fields are hashed. | ||
|
|
||
| ```rust | ||
| # use std::marker::PhantomData; | ||
| # use derive_more::Hash; | ||
| # | ||
| trait Trait { | ||
| type Assoc; | ||
| } | ||
| impl<T: ?Sized> Trait for T { | ||
| type Assoc = u8; | ||
| } | ||
|
|
||
| #[derive(Debug, Hash)] | ||
| struct Foo<A, B, C: Trait + ?Sized> { | ||
| a: A, | ||
| b: PhantomData<B>, | ||
| c: C::Assoc, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct NoHash; | ||
| ``` | ||
|
|
||
| This generates code equivalent to: | ||
|
|
||
| ```rust | ||
| # use std::marker::PhantomData; | ||
| # use derive_more::core::hash::{Hash, Hasher}; | ||
| # | ||
| # trait Trait { | ||
| # type Assoc; | ||
| # } | ||
| # impl<T: ?Sized> Trait for T { | ||
| # type Assoc = u8; | ||
| # } | ||
| # | ||
| # struct Foo<A, B, C: Trait + ?Sized> { | ||
| # a: A, | ||
| # b: PhantomData<B>, | ||
| # c: C::Assoc, | ||
| # } | ||
| # | ||
| impl<A, B, C: Trait + ?Sized> Hash for Foo<A, B, C> | ||
| where | ||
| A: Hash, | ||
| PhantomData<B>: Hash, // `B: Hash` is generated by `std` instead | ||
| C::Assoc: Hash, // `C: Hash` is generated by `std` instead | ||
| { | ||
| fn hash<H: Hasher>(&self, state: &mut H) { | ||
| match self { | ||
| Self { a: self_0, b: self_1, c: self_2 } => { | ||
| self_0.hash(state); | ||
| self_1.hash(state); | ||
| self_2.hash(state); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Enums | ||
|
|
||
| For enums the discriminant is hashed first, followed by the fields. | ||
|
|
||
| ```rust | ||
| # use std::marker::PhantomData; | ||
| # use derive_more::Hash; | ||
| # | ||
| # trait Trait { | ||
| # type Assoc; | ||
| # } | ||
| # impl<T: ?Sized> Trait for T { | ||
| # type Assoc = u8; | ||
| # } | ||
| # | ||
| #[derive(Debug, Hash)] | ||
| enum Foo<A, B, C: Trait + ?Sized> { | ||
| A(A), | ||
| B { b: PhantomData<B> }, | ||
| C(C::Assoc), | ||
| } | ||
| # | ||
| # #[derive(Debug)] | ||
| # struct NoHash; | ||
| ``` | ||
|
|
||
| This generates code equivalent to: | ||
|
|
||
| ```rust | ||
| # use std::marker::PhantomData; | ||
| # use derive_more::core::hash::{Hash, Hasher}; | ||
| # | ||
| # trait Trait { | ||
| # type Assoc; | ||
| # } | ||
| # impl<T: ?Sized> Trait for T { | ||
| # type Assoc = u8; | ||
| # } | ||
| # | ||
| # enum Foo<A, B, C: Trait + ?Sized> { | ||
| # A(A), | ||
| # B { b: PhantomData<B> }, | ||
| # C(C::Assoc), | ||
| # } | ||
| # | ||
| impl<A, B, C: Trait + ?Sized> Hash for Foo<A, B, C> | ||
| where | ||
| A: Hash, | ||
| PhantomData<B>: Hash, // `B: Hash` is generated by `std` instead | ||
| C::Assoc: Hash, // `C: Hash` is generated by `std` instead | ||
| { | ||
| fn hash<H: Hasher>(&self, state: &mut H) { | ||
| std::mem::discriminant(self).hash(state); | ||
| match self { | ||
| Self::A(self_0) => { self_0.hash(state); } | ||
| Self::B { b: self_0 } => { self_0.hash(state); } | ||
| Self::C(self_0) => { self_0.hash(state); } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Ignoring | ||
|
|
||
| The `#[hash(skip)]` attribute can be used to ignore fields, a whole struct or enum variants in the expansion. | ||
|
|
||
| Note that if you also implement the `Eq` or `PartialEq` traits, fields marked with `#[eq(skip)]` or `#[partial_eq(skip)]` | ||
| will be ignored during hashing. This is done so that this property holds: | ||
|
|
||
| ```txt | ||
| k1 == k2 -> hash(k1) == hash(k2) | ||
| ``` | ||
| That is [expected](https://doc.rust-lang.org/std/hash/trait.Hash.html#hash-and-eq) from `Hash` implementations. | ||
|
|
||
| ```rust | ||
| # use derive_more::Hash; | ||
| # | ||
| #[derive(Debug)] | ||
| struct NoHash; // doesn't implement `Hash` | ||
|
|
||
| #[derive(Debug, Hash)] | ||
| struct Foo { | ||
| num: i32, | ||
| #[hash(skip)] // or #[hash(ignore)] | ||
| ignored: f32, | ||
| } | ||
|
|
||
| #[derive(Debug, Hash)] | ||
| // Makes all fields of this struct being ignored. | ||
| #[hash(skip)] // or #[hash(ignore)] | ||
| struct Bar(f32, NoHash); | ||
|
|
||
| #[derive(Debug, Hash)] | ||
| enum Enum { | ||
| Foo(i32, #[hash(skip)] NoHash), | ||
| #[hash(skip)] | ||
| Bar(NoHash), | ||
| Baz, | ||
| } | ||
| ``` | ||
|
|
||
| This generates code equivalent to: | ||
|
|
||
| ```rust | ||
| # use derive_more::core::hash::{Hash, Hasher}; | ||
| # struct NoHash; | ||
| # | ||
| # struct Foo { num: i32, ignored: f32 } | ||
| # | ||
| impl Hash for Foo { | ||
| fn hash<H: Hasher>(&self, state: &mut H) { | ||
| match self { | ||
| Self { num: self_0, .. } => { self_0.hash(state); } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # struct Bar(i32, NoHash); | ||
| # | ||
| impl Hash for Bar { | ||
| fn hash<H: Hasher>(&self, _state: &mut H) {} | ||
| } | ||
|
|
||
| # enum Enum { | ||
| # Foo(i32, NoHash), | ||
| # Bar(NoHash), | ||
| # Baz, | ||
| # } | ||
| # | ||
| impl Hash for Enum { | ||
| fn hash<H: Hasher>(&self, state: &mut H) { | ||
| std::mem::discriminant(self).hash(state); | ||
| match self { | ||
| Self::Foo(self_0, _) => { self_0.hash(state); } | ||
| Self::Bar(_) => {} | ||
| Self::Baz => {} | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Custom hash function | ||
|
|
||
| The `#[hash(with(function))]` attribute can be used to specify a custom hash function for a field. | ||
| The function must have the signature `fn<H: Hasher>(value: &FieldType, state: &mut H)`. | ||
|
|
||
| ```rust | ||
| # use derive_more::Hash; | ||
| mod my_hash { | ||
| use core::hash::Hasher; | ||
|
|
||
| pub fn hash_abs<H: Hasher>(value: &i32, state: &mut H) { | ||
| state.write_i32(value.abs()); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Hash)] | ||
| struct Foo { | ||
| #[hash(with(my_hash::hash_abs))] | ||
| value: i32, | ||
| } | ||
| ``` | ||
|
|
||
| This generates code equivalent to: | ||
|
|
||
| ```rust | ||
| # use derive_more::core::hash::{Hash, Hasher}; | ||
| # mod my_hash { | ||
| # use derive_more::core::hash::Hasher; | ||
| # | ||
| # pub fn hash_abs<H: Hasher>(value: &i32, state: &mut H) { | ||
| # state.write_i32(value.abs()); | ||
| # } | ||
| # } | ||
| # | ||
| # struct Foo { | ||
| # value: i32, | ||
| # } | ||
| # | ||
| impl Hash for Foo { | ||
| fn hash<H: Hasher>(&self, state: &mut H) { | ||
| match self { | ||
| Self { value: self_0 } => { | ||
| my_hash::hash_abs(self_0, state); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This is useful for types that don't implement `Hash` but can be hashed in a custom way, or when you need different hashing behavior than the default. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@fredszaq I think, deriving
Hashis a little bit trickier than it seems to be.For the correctness, we should definitely consider and work correctly with the prefix collisions. They also should have good coverage in tests, since seem to be quite error-prone to edge cases.
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.
Hmmm... okay, it seems that
stdimpls for tuples doesn't bother with that. It's all up to "leaf" types to bother with such things.Good, so we don't need to bother about that. However, some tests for such cases still would be good to see.
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.
@tyranron I added a few more tests around possible collisions, see 3fd8115 did you have other use cases in mind ?
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.
The
rustcalso doesn't bother for its derived impls (pass-Zunpretty=expandedtorustcor see playground):becomes