]> git.lizzy.rs Git - generate-random.git/commitdiff
Support HashMap
authorLizzy Fleckenstein <eliasfleckenstein@web.de>
Mon, 6 Feb 2023 15:52:09 +0000 (16:52 +0100)
committerLizzy Fleckenstein <eliasfleckenstein@web.de>
Mon, 6 Feb 2023 15:52:09 +0000 (16:52 +0100)
lib/src/lib.rs

index 3b555f295ba6b5a8c9cb5d7f0e69ae9e8193f76f..d0e96e30627138c48fa67e34f08201f1c3b65af9 100644 (file)
@@ -115,6 +115,19 @@ impl<T: GenerateRandom> GenerateRandom for Vec<T> {
     }
 }
 
+impl<K, V> GenerateRandom for std::collections::HashMap<K, V>
+where
+    K: GenerateRandom + std::cmp::Eq + std::hash::Hash,
+    V: GenerateRandom,
+{
+    fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
+        let len = rng.gen_range(0..8);
+        (0..len)
+            .map(|_| (K::generate_random(rng), V::generate_random(rng)))
+            .collect()
+    }
+}
+
 macro_rules! impl_generate_random_tuple {
        ( $t0:ident $( $t:ident )* ) => {
                impl< $t0, $( $t, )* > GenerateRandom for ( $t0, $( $t, )* )