Skip to main content

Ring

Struct Ring 

Source
pub struct Ring<H = RandomState> { /* private fields */ }
Expand description

A hashing ring implemented using jump hashing.

Jump hashing is based on using a hash of the key as the seed for a random number generator and using it to jump forward in a list of nodes until it falls off the end. The last node it lands on is the result.

Jump hashing is very fast and executes in O(ln n) time. It also has no memory overhead and has virtually perfect key distribution. However, the main limitation of jump hashing is that it returns an integer in the range [0, nodes) and it does not support arbitrary node names.

§Examples

use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;

let ring = Ring::with_hasher(DefaultBuildHasher::default(), 100);

assert_eq!(ring.get_node(&"foo"), 8);
assert_eq!(ring.nodes(), 100);

Implementations§

Source§

impl Ring<RandomState>

Source

pub fn new(nodes: u32) -> Self

Constructs a new Ring with a specified number of nodes.

§Panics

Panics if the number of nodes is zero.

§Examples
use hash_rings::jump::Ring;

let ring: Ring = Ring::new(100);
Source§

impl<H> Ring<H>

Source

pub fn with_hasher(hash_builder: H, nodes: u32) -> Self

Constructs a new Ring with a specified number of nodes and hash builder.

§Panics

Panics if the number of nodes is zero.

§Examples
use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;

let ring: Ring<_> = Ring::with_hasher(DefaultBuildHasher::default(), 100);
Source

pub fn get_node<T>(&self, key: &T) -> u32
where T: Hash, H: BuildHasher,

Returns the node associated with a key.

§Examples
use hash_rings::jump::Ring;
use std::collections::hash_map::DefaultHasher;
use std::hash::BuildHasherDefault;

type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;

let ring = Ring::with_hasher(DefaultBuildHasher::default(), 100);
assert_eq!(ring.get_node(&"foo"), 8);
Source

pub fn nodes(&self) -> u32

Returns the number of nodes in the ring.

§Examples
use hash_rings::jump::Ring;

let ring = Ring::new(100);
assert_eq!(ring.nodes(), 100);

Auto Trait Implementations§

§

impl<H> Freeze for Ring<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for Ring<H>
where H: RefUnwindSafe,

§

impl<H> Send for Ring<H>
where H: Send,

§

impl<H> Sync for Ring<H>
where H: Sync,

§

impl<H> Unpin for Ring<H>
where H: Unpin,

§

impl<H> UnsafeUnpin for Ring<H>
where H: UnsafeUnpin,

§

impl<H> UnwindSafe for Ring<H>
where 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> 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, 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.