diff --git a/Cargo.toml b/Cargo.toml index adb78eb7..dc1cc744 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ thiserror = "1.0" rustc-hash = "1.1.0" serde = { version = "1.0", features = ["derive"], optional = true } log = "0.4.14" # for debug logs in tests +rand = "0.8.5" [dev-dependencies] proptest = "0.10.1" diff --git a/src/type_aliases.rs b/src/type_aliases.rs index 9515ee70..423071c9 100644 --- a/src/type_aliases.rs +++ b/src/type_aliases.rs @@ -36,14 +36,14 @@ impl Map { let keys: Vec<&K> = self.map.keys().collect(); MapIter { map: self, - order: keys.into_iter(), + order: shuffle(keys).into_iter(), } } pub fn keys(&self) -> impl Iterator { let keys: Vec<&K> = self.map.keys().collect(); MapKeys { - order: keys.into_iter(), + order: shuffle(keys).into_iter(), _v: std::marker::PhantomData::<&V>, } } @@ -142,7 +142,7 @@ impl IntoIterator for Map { let keys: Vec = self.map.keys().map(|k| k.clone()).collect(); MapIntoIter { map: self, - order: keys.into_iter(), + order: shuffle(keys).into_iter(), } } } @@ -205,7 +205,7 @@ impl Set { pub fn iter(&self) -> SetIter { let keys: Vec<&V> = self.set.iter().collect(); SetIter { - order: keys.into_iter(), + order: shuffle(keys).into_iter(), } } } @@ -251,7 +251,7 @@ impl IntoIterator for Set { fn into_iter(self) -> Self::IntoIter { let keys: Vec = self.set.into_iter().collect(); SetIntoIter { - order: keys.into_iter(), + order: shuffle(keys).into_iter(), } } } @@ -277,3 +277,9 @@ impl<'a, V> Iterator for SetIntoIter { self.order.next() } } + +fn shuffle(mut v: Vec) -> Vec { + use rand::seq::SliceRandom; + v.shuffle(&mut rand::thread_rng()); + v +}