-
Notifications
You must be signed in to change notification settings - Fork 145
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
Add Hash derive #532
Changes from 8 commits
98ce6f2
cd7f01a
4be4742
bedc307
0bc53ba
71be6cf
ef8aad5
aecafc3
832dfba
e850d90
3fd8115
a3b0311
a7df0d6
64c7d39
dc31c68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/ | ||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fredszaq I think, deriving 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm... okay, it seems that Good, so we don't need to bother about that. However, some tests for such cases still would be good to see.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The #[derive(Hash)]
struct Test {
a: u32,
b: u32,
}becomes #![feature(prelude_import)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2024::*;
struct Test {
a: u32,
b: u32,
}
#[automatically_derived]
impl ::core::hash::Hash for Test {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.a, state);
::core::hash::Hash::hash(&self.b, 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. | ||
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 this better be configured globally on your side.