Skip to content

Commit

Permalink
Allow cloning the hash ring to use iterators effectively
Browse files Browse the repository at this point in the history
  • Loading branch information
grcooper committed Oct 9, 2023
1 parent d4f749f commit 6ad02c7
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use std::fmt::Debug;
use std::hash::BuildHasher;
use std::hash::{Hash, Hasher};

#[derive(Clone)]
pub struct DefaultHashBuilder;

impl BuildHasher for DefaultHashBuilder {
Expand Down Expand Up @@ -133,6 +134,7 @@ impl<T> Ord for Node<T> {
}
}

#[derive(Clone)]
pub struct HashRing<T, S = DefaultHashBuilder> {
hash_builder: S,
ring: Vec<Node<T>>,
Expand Down Expand Up @@ -482,4 +484,31 @@ mod tests {
assert_eq!(Some(vnode2), iter.next());
assert_eq!(None, iter.next());
}

struct TestIterStruct {
ring: HashRing<String>,
}

impl TestIterStruct {
pub fn print_each(&self) -> Vec<String> {
let mut v = Vec::new();
for node in self.ring.clone().into_iter() {
v.push(node);
}
v
}
}

#[test]
fn into_iter_for_struct_field() {
let mut ring: HashRing<String> = HashRing::new();
ring.add("foo".to_string());
ring.add("bar".to_string());

let s = TestIterStruct { ring };

let v = s.print_each();

assert_eq!(v.len(), 2);
}
}

0 comments on commit 6ad02c7

Please sign in to comment.