Skip to content

Commit

Permalink
add support for iterators
Browse files Browse the repository at this point in the history
Adds the ability to iterate over nodes in the ring. This would help
users attempting to copy the internal ring for serialization purposes
and help with needs for replication where a user would want to access
a node that neighbors the node responsible for the key
  • Loading branch information
connormullett committed Jul 12, 2023
1 parent fd56df7 commit 26100a9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ impl<T: Hash, S: BuildHasher> HashRing<T, S> {
}
}

pub struct HashRingIterator<T> {
ring: std::vec::IntoIter<T>,
index: usize,
}

impl<T> Iterator for HashRingIterator<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
let out = self.ring.next();
self.index += 1;
out
}
}

impl<T: Clone> IntoIterator for HashRing<T> {
type Item = T;

type IntoIter = HashRingIterator<T>;

fn into_iter(self) -> Self::IntoIter {
HashRingIterator {
ring: self
.ring
.iter()
.map(|n| n.node.clone())
.collect::<Vec<T>>()
.into_iter(),
index: 0,
}
}
}

// An internal function for converting a reference to a hashable type into a `u64` which
// can be used as a key in the hash ring.
fn get_key<S, T>(hash_builder: &S, input: T) -> u64
Expand Down

0 comments on commit 26100a9

Please sign in to comment.