1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Sean Lawlor
//
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree.

//! Factory's defaulting routing hash mechanism. Hashes a [super::JobKey] to a finite
//! space

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

/// Hash a key into a finite space
pub fn hash_with_max<TKey>(key: &TKey, excluded_max: usize) -> usize
where
    TKey: Hash,
{
    let mut dh = DefaultHasher::new();
    key.hash(&mut dh);
    let bounded_hash = dh.finish() % (excluded_max as u64);
    bounded_hash as usize
}

#[cfg(test)]
mod tests {

    use super::*;

    #[derive(Hash)]
    struct TestHashable(u128);

    #[test]
    fn test_bounded_hashing() {
        let test_values = [
            TestHashable(0),
            TestHashable(10),
            TestHashable(23),
            TestHashable(128),
            TestHashable(u64::MAX as u128),
        ];

        let test_results = [0usize, 2, 1, 0, 0];

        for (test_value, test_result) in test_values.iter().zip(test_results) {
            let hashed = hash_with_max(test_value, 3);
            assert_eq!(test_result, hashed);
        }
    }
}