keyby

Crates.iokeyby
lib.rskeyby
version0.2.0
sourcesrc
created_at2019-06-28 19:01:06.681856
updated_at2019-07-21 14:51:26.55946
descriptionA library for hashing on specific Struct fields
homepagehttps://github.com/Max-Meldrum/keyby
repositoryhttps://github.com/Max-Meldrum/keyby
max_upload_size
id144370
size7,001
Max Meldrum (Max-Meldrum)

documentation

README

keyby

An attribute macro for hashing on a specific field of a struct.

Example

#[macro_use]
extern crate keyby;

use std::collections::hash_map::DefaultHasher;

#[key_by(id)]
pub struct Item {
    id: u64,
    price: u32,
}

fn main() {
    let i1 = Item { id: 1, price: 20 };
    let i2 = Item { id: 2, price: 150 };
    let i3 = Item { id: 1, price: 50 };

    assert_eq!(calc_hash(&i1), calc_hash(&i3));
    assert!(calc_hash(&i1) != calc_hash(&i2));
}

fn calc_hash<T: std::hash::Hash>(t: &T) -> u64 {
    use std::hash::Hasher;
    let mut s = DefaultHasher::new();
    t.hash(&mut s);
    s.finish()
}
Commit count: 3

cargo fmt