Skip to main content

HashFunction

Trait HashFunction 

Source
pub trait HashFunction {
    // Required method
    fn hash(&self, key: &str) -> i64;
}
Expand description

Trait for hashing string keys to long integer values

Implementations of this trait provide different hash algorithms that can be used in consistent hashing rings to distribute keys across nodes.

§Example

use rocketmq_common::common::consistenthash::HashFunction;

struct SimpleHashFunction;

impl HashFunction for SimpleHashFunction {
    fn hash(&self, key: &str) -> i64 {
        // Simple example: sum of character codes
        key.chars().map(|c| c as i64).sum()
    }
}

let hasher = SimpleHashFunction;
let hash_value = hasher.hash("test-key");
assert!(hash_value > 0);

Required Methods§

Source

fn hash(&self, key: &str) -> i64

Hash a string key to a 64-bit signed integer value

§Arguments
  • key - The string key to hash
§Returns

A 64-bit signed integer hash value

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§