diff --git a/README.md b/README.md index 5b21e6b..4794456 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Available features: - `zerocopy` - `glam` - `url` + - `indexmap` - `always-true-rng`: expose AlwaysTrueRng - `maybe-non-empty-collections`: allow to use AlwaysTrueRng to generate non-empty collections diff --git a/fake/Cargo.toml b/fake/Cargo.toml index 3236cbb..9e529af 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -39,6 +39,7 @@ glam = { version = "0.29.0", optional = true } url-escape = { version = "0.1", optional = true } bson = { version = "2.10", optional = true } url = { version = "2.5.2", optional = true } +indexmap = { version = "2", optional = true} [dev-dependencies] chrono = { version = "0.4", features = ["clock"], default-features = false } diff --git a/fake/src/impls/indexmap/mod.rs b/fake/src/impls/indexmap/mod.rs new file mode 100644 index 0000000..849ef9c --- /dev/null +++ b/fake/src/impls/indexmap/mod.rs @@ -0,0 +1,22 @@ +use crate::{Dummy, Fake, Faker}; +use indexmap::IndexMap; +use rand::Rng; +use std::hash::{BuildHasher, Hash}; + +use super::std::collections::get_len; + +impl Dummy for IndexMap +where + K: Dummy + Hash + Eq, + V: Dummy, + S: BuildHasher + Default, +{ + fn dummy_with_rng(config: &Faker, rng: &mut R) -> Self { + let len = get_len(config, rng); + let mut m = IndexMap::with_capacity_and_hasher(len, S::default()); + for _ in 0..len { + m.insert(config.fake_with_rng(rng), config.fake_with_rng(rng)); + } + m + } +} diff --git a/fake/src/impls/mod.rs b/fake/src/impls/mod.rs index c1041d0..64fd258 100644 --- a/fake/src/impls/mod.rs +++ b/fake/src/impls/mod.rs @@ -18,6 +18,8 @@ pub mod geo; pub mod glam; #[cfg(feature = "http")] pub mod http; +#[cfg(feature = "indexmap")] +pub mod indexmap; #[cfg(feature = "semver")] pub mod semver; #[cfg(feature = "serde_json")]