Skip to main content

Ring

Struct Ring 

Source
pub struct Ring<'a, T> { /* private fields */ }
Expand description

A hashing ring implemented using maglev hashing.

Maglev hashing produces a lookup table that allows finding a node in constant time by generating random permutations.

§Examples

use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);

assert_eq!(ring.get_node(&"point-1"), &"node-3");
assert_eq!(ring.nodes(), 3);
assert_eq!(ring.capacity(), 307);

Implementations§

Source§

impl<'a, T> Ring<'a, T>

Source

pub fn new(nodes: Vec<&'a T>) -> Self
where T: Hash,

Constructs a new Ring<T> with a specified list of nodes.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
Source

pub fn with_capacity_hint(nodes: Vec<&'a T>, capacity_hint: usize) -> Self
where T: Hash,

Constructs a new Ring<T> with a specified list of nodes and a capacity hint. The actual capacity of the ring will always be the next prime greater than or equal to capacity_hint. If nodes are removed and the ring is regenerated, the ring should be rebuilt with the same capacity.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::with_capacity_hint(vec![&"node-1", &"node-2", &"node-3"], 100);
assert_eq!(ring.capacity(), 101);
Source

pub fn nodes(&self) -> usize

Returns the number of nodes in the ring.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.nodes(), 3);
Source

pub fn capacity(&self) -> usize

Returns the capacity of the ring. If nodes are removed and the ring is regenerated, the ring should be rebuilt with the same capacity.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.capacity(), 307);
Source

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

Returns the node associated with a key.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
assert_eq!(ring.get_node(&"point-1"), &"node-3");
Source

pub fn iter(&'a self) -> impl Iterator<Item = &'a T>

Returns an iterator over the ring. The iterator will yield the nodes in the ring.

§Examples
use hash_rings::maglev::Ring;

let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);

let mut iterator = ring.iter();
assert_eq!(iterator.next(), Some(&"node-1"));
assert_eq!(iterator.next(), Some(&"node-2"));
assert_eq!(iterator.next(), Some(&"node-3"));
assert_eq!(iterator.next(), None);

Trait Implementations§

Source§

impl<'a, T> IntoIterator for &'a Ring<'a, T>
where T: Hash + Eq,

Source§

type IntoIter = Box<dyn Iterator<Item = &'a T> + 'a>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Ring<'a, T>

§

impl<'a, T> RefUnwindSafe for Ring<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Ring<'a, T>
where T: Sync,

§

impl<'a, T> Sync for Ring<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Ring<'a, T>

§

impl<'a, T> UnsafeUnpin for Ring<'a, T>

§

impl<'a, T> UnwindSafe for Ring<'a, T>
where T: RefUnwindSafe,

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.