RendezvousHasher

Struct RendezvousHasher 

Source
pub struct RendezvousHasher<H, B>
where H: Hasher, B: BuildHasher<Hasher = H>,
{ /* private fields */ }
Expand description

A hasher implementation for the Rendezvous (HRW - Highest Random Weight) hashing algorithm.

Rendezvous hashing provides a way to consistently distribute keys across a set of nodes, with minimal redistribution when nodes are added or removed. It’s particularly useful for distributed systems that need to balance load across multiple servers.

This implementation works with any hasher that implements std::hash::Hasher.

Implementations§

Source§

impl<H, B> RendezvousHasher<H, B>
where H: Hasher, B: BuildHasher<Hasher = H>,

Source

pub fn new(build_hasher: B) -> Self

Creates a new RendezvousHasher with the provided build hasher.

Source

pub fn select<'a, K, N>(&self, key: &K, nodes: &'a [N]) -> Option<&'a N>
where K: Hash, N: Hash,

Selects the preferred node for a given key from a list of nodes.

This method computes a hash score for each node combined with the key, and returns the node with the highest score.

§Parameters
  • key - The key to hash
  • nodes - A slice of nodes to choose from
§Returns

A reference to the selected node, or None if the nodes slice is empty

Source

pub fn select_index<K, N>(&self, key: &K, nodes: &[N]) -> Option<usize>
where K: Hash, N: Hash,

Selects the preferred node for a given key from a list of nodes, returning the index of the selected node.

§Parameters
  • key - The key to hash
  • nodes - A slice of nodes to choose from
§Returns

The index of the selected node, or None if the nodes slice is empty

Source

pub fn rank<'a, K, N>(&self, key: &K, nodes: &'a [N]) -> Vec<&'a N>
where K: Hash, N: Hash,

Ranks all nodes for a given key, returning them sorted by preference (highest score to lowest).

§Parameters
  • key - The key to hash
  • nodes - A slice of nodes to rank
§Returns

A vector of references to nodes, sorted by preference

Trait Implementations§

Source§

impl<H, B> Clone for RendezvousHasher<H, B>
where H: Hasher + Clone, B: BuildHasher<Hasher = H> + Clone,

Source§

fn clone(&self) -> RendezvousHasher<H, B>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H, B> Debug for RendezvousHasher<H, B>
where H: Hasher + Debug, B: BuildHasher<Hasher = H> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<H, B> Freeze for RendezvousHasher<H, B>
where B: Freeze,

§

impl<H, B> RefUnwindSafe for RendezvousHasher<H, B>

§

impl<H, B> Send for RendezvousHasher<H, B>
where B: Send, H: Send,

§

impl<H, B> Sync for RendezvousHasher<H, B>
where B: Sync, H: Sync,

§

impl<H, B> Unpin for RendezvousHasher<H, B>
where B: Unpin, H: Unpin,

§

impl<H, B> UnwindSafe for RendezvousHasher<H, B>
where B: UnwindSafe, H: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.