discriminant_hash_derive

Crates.iodiscriminant_hash_derive
lib.rsdiscriminant_hash_derive
version0.1.0
sourcesrc
created_at2021-06-11 11:29:52.494204
updated_at2021-06-11 11:29:52.494204
descriptionDerive macro to implement Hash trait based on enum's discriminants only and ignore variant's fields
homepage
repositoryhttps://github.com/MihirLuthra/discriminant_hash_derive
max_upload_size
id408960
size7,160
Mihir Luthra (MihirLuthra)

documentation

README

discriminant_hash_derive

Deriving DiscriminantHash implements Hash trait for the underlying enum. Here hash is only dependent on discriminant and isn’t effected by variant’s fields.

Example

use discriminant_hash_derive::DiscriminantHash;
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

#[derive(DiscriminantHash)]
enum Abc<T> {
    Simple,
    HashNotImplemented(Xyz),
    Generic(T),
}

#[allow(unused)]
#[derive(Hash)]
enum Pqr<'a> {
    Simple,
    Lifetime(&'a str),
}

// Xyz doesn't impl Hash
struct Xyz;

fn main() {
    assert_eq!(my_hash(Abc::Simple::<i32>), my_hash(Abc::Simple::<Xyz>));
    assert_eq!(
        my_hash(Abc::HashNotImplemented::<i32>(Xyz)),
        my_hash(Abc::HashNotImplemented::<String>(Xyz))
    );
    assert_eq!(
        my_hash(Abc::Generic::<i32>(4)),
        my_hash(Abc::Generic::<Xyz>(Xyz))
    );

    assert_ne!(
        my_hash(Abc::Simple::<i32>),
        my_hash(Abc::Generic::<Xyz>(Xyz))
    );

     
    // This may be same depending on how Pqr is defined
    // assert_eq!(
    //     my_hash(Abc::Simple::<i32>),
    //     my_hash(Pqr::Simple)
    // );
     
}

fn my_hash<T>(obj: T) -> u64
where
    T: Hash,
{
    let mut hasher = DefaultHasher::new();
    obj.hash(&mut hasher);
    hasher.finish()
}
Commit count: 3

cargo fmt