Trait merkletree::hash::Hashable

source ·
pub trait Hashable<H: Hasher> {
    // Required method
    fn hash(&self, state: &mut H);

    // Provided method
    fn hash_slice(data: &[Self], state: &mut H)
       where Self: Sized { ... }
}
Expand description

A hashable type.

Types implementing Hashable are able to be [hash]ed with an instance of Hasher.

Implementing Hashable

You can derive Hashable with #[derive(Hashable)] if all fields implement Hashable. The resulting hash will be the combination of the values from calling [hash] on each field.

#[macro_use]
extern crate merkletree_derive;
extern crate merkletree;

use merkletree::hash::Hashable;

fn main() {
    #[derive(Hashable)]
    struct Foo {
        name: String,
        country: String,
    }
}

If you need more control over how a value is hashed, you can of course implement the Hashable trait yourself:

extern crate merkletree;

use merkletree::hash::Hashable;
use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;

fn main() {
   struct Person {
       id: u32,
       name: String,
       phone: u64,
   }

   impl<H: Hasher> Hashable<H> for Person {
       fn hash(&self, state: &mut H) {
           self.id.hash(state);
           self.name.hash(state);
           self.phone.hash(state);
       }
   }

   let foo = Person{
       id: 1,
       name: String::from("blah"),
       phone: 2,
   };

   let mut hr = DefaultHasher::new();
   foo.hash(&mut hr);
   assert_eq!(hr.finish(), 7101638158313343130)
}

Hashable and Eq

When implementing both Hashable and Eq, it is important that the following property holds:

k1 == k2 -> hash(k1) == hash(k2)

In other words, if two keys are equal, their hashes must also be equal.

Required Methods§

source

fn hash(&self, state: &mut H)

Feeds this value into the given Hasher.

Provided Methods§

source

fn hash_slice(data: &[Self], state: &mut H)where Self: Sized,

Feeds a slice of this type into the given Hasher.

Implementations on Foreign Types§

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E> Hashable<Z> for (A, B, C, D, E)where E: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for bool

source§

fn hash(&self, state: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G> Hashable<Z> for (A, B, C, D, E, F, G)where G: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for u16

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[u16], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for i16

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[i16], state: &mut H)

source§

impl<H: Hasher, T: ?Sized> Hashable<H> for *mut T

source§

fn hash(&self, state: &mut H)

source§

impl<H: Hasher> Hashable<H> for isize

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[isize], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for Vec<u8>

source§

fn hash(&self, state: &mut H)

source§

impl<H: Hasher> Hashable<H> for char

source§

fn hash(&self, state: &mut H)

source§

impl<H: Hasher> Hashable<H> for u128

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[u128], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for str

source§

fn hash(&self, state: &mut H)

source§

impl<Z: Hasher, A> Hashable<Z> for (A,)where A: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher, T: ?Sized> Hashable<H> for *const T

source§

fn hash(&self, state: &mut H)

source§

impl<'a, H: Hasher, T: ?Sized + Hashable<H>> Hashable<H> for &'a mut T

source§

fn hash(&self, state: &mut H)

source§

impl<H: Hasher> Hashable<H> for i64

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[i64], state: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G: Hashable<Z>, H: Hashable<Z>, I: Hashable<Z>, J: Hashable<Z>, K: Hashable<Z>, L> Hashable<Z> for (A, B, C, D, E, F, G, H, I, J, K, L)where L: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for usize

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[usize], state: &mut H)

source§

impl<H: Hasher, T: Hashable<H>> Hashable<H> for [T]

source§

fn hash(&self, state: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G: Hashable<Z>, H: Hashable<Z>, I: Hashable<Z>, J> Hashable<Z> for (A, B, C, D, E, F, G, H, I, J)where J: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C> Hashable<Z> for (A, B, C)where C: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for i128

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[i128], state: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G: Hashable<Z>, H> Hashable<Z> for (A, B, C, D, E, F, G, H)where H: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for ()

source§

fn hash(&self, _: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B> Hashable<Z> for (A, B)where B: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D> Hashable<Z> for (A, B, C, D)where D: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher> Hashable<H> for u32

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[u32], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for u8

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[u8], state: &mut H)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F> Hashable<Z> for (A, B, C, D, E, F)where F: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G: Hashable<Z>, H: Hashable<Z>, I> Hashable<Z> for (A, B, C, D, E, F, G, H, I)where I: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<Z: Hasher, A: Hashable<Z>, B: Hashable<Z>, C: Hashable<Z>, D: Hashable<Z>, E: Hashable<Z>, F: Hashable<Z>, G: Hashable<Z>, H: Hashable<Z>, I: Hashable<Z>, J: Hashable<Z>, K> Hashable<Z> for (A, B, C, D, E, F, G, H, I, J, K)where K: ?Sized + Hashable<Z>,

source§

fn hash(&self, state: &mut Z)

source§

impl<H: Hasher, const N: usize> Hashable<H> for [u8; N]

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[[u8; N]], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for i32

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[i32], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for i8

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[i8], state: &mut H)

source§

impl<H: Hasher> Hashable<H> for String

source§

fn hash(&self, state: &mut H)

source§

impl<H: Hasher> Hashable<H> for u64

source§

fn hash(&self, state: &mut H)

source§

fn hash_slice(data: &[u64], state: &mut H)

source§

impl<'a, H: Hasher, T: ?Sized + Hashable<H>> Hashable<H> for &'a T

source§

fn hash(&self, state: &mut H)

Implementors§