pub struct HashContext<T> {
pub hash: fn(&T, &mut dyn Hasher),
}Expand description
A context providing a custom hash function.
When used with WithContext, implements Hash by dispatching
through the stored hash function. The function receives a &mut dyn Hasher
so it can work with any hasher type.
§Examples
use context_trait::{WithContext, HashContext};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let ctx = HashContext { hash: |v: &(i32, i32), h: &mut dyn Hasher| {
h.write_i32(v.0);
}};
let a = WithContext { inner: (1, 100), ctx };
let mut hasher = DefaultHasher::new();
a.hash(&mut hasher);
let hash_a = hasher.finish();
let b = WithContext { inner: (1, 200), ctx };
let mut hasher = DefaultHasher::new();
b.hash(&mut hasher);
let hash_b = hasher.finish();
// Same hash because only first element is hashed
assert_eq!(hash_a, hash_b);Fields§
§hash: fn(&T, &mut dyn Hasher)The hash function.
Trait Implementations§
Source§impl<T> Clone for HashContext<T>
impl<T> Clone for HashContext<T>
Source§fn clone(&self) -> HashContext<T>
fn clone(&self) -> HashContext<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> Debug for HashContext<T>
impl<T> Debug for HashContext<T>
impl<T> Copy for HashContext<T>
Auto Trait Implementations§
impl<T> Freeze for HashContext<T>
impl<T> RefUnwindSafe for HashContext<T>
impl<T> Send for HashContext<T>
impl<T> Sync for HashContext<T>
impl<T> Unpin for HashContext<T>
impl<T> UnsafeUnpin for HashContext<T>
impl<T> UnwindSafe for HashContext<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more