From 10fe481d632c983af2e6c1aa1b66122272c2f3d3 Mon Sep 17 00:00:00 2001 From: Graham Cooper Date: Mon, 9 Oct 2023 13:18:42 -0400 Subject: [PATCH 1/2] Allow cloning the hash ring to use iterators effectively --- src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 23581f5..2469d3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,6 +88,7 @@ use std::fmt::Debug; use std::hash::BuildHasher; use std::hash::Hash; +#[derive(Clone)] pub struct DefaultHashBuilder; impl BuildHasher for DefaultHashBuilder { @@ -133,6 +134,7 @@ impl Ord for Node { } } +#[derive(Clone)] pub struct HashRing { hash_builder: S, ring: Vec>, @@ -494,4 +496,31 @@ mod tests { assert_eq!(Some(vnode2), iter.next()); assert_eq!(None, iter.next()); } + + struct TestIterStruct { + ring: HashRing, + } + + impl TestIterStruct { + pub fn print_each(&self) -> Vec { + 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 = 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); + } } From 41146f4bc135e01a27e2c4435213c2906c5149df Mon Sep 17 00:00:00 2001 From: timvisee Date: Fri, 17 May 2024 12:59:22 +0200 Subject: [PATCH 2/2] Remove iter struct test, it does not seem to add value --- src/lib.rs | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2469d3e..dd6ca7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -496,31 +496,4 @@ mod tests { assert_eq!(Some(vnode2), iter.next()); assert_eq!(None, iter.next()); } - - struct TestIterStruct { - ring: HashRing, - } - - impl TestIterStruct { - pub fn print_each(&self) -> Vec { - 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 = 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); - } }