Skip to content

Commit 8e4cc9a

Browse files
authored
Merge pull request #246 from cuviper/arbitrary
impl Arbitrary for IndexMap and IndexSet
2 parents d355f11 + cc4c47d commit 8e4cc9a

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ jobs:
1919
- rust: 1.61.0 # MSRV
2020
features:
2121
- rust: stable
22-
features: serde
22+
features: arbitrary
23+
- rust: stable
24+
features: quickcheck
2325
- rust: stable
2426
features: rayon
2527
- rust: stable
2628
features: rustc-rayon
29+
- rust: stable
30+
features: serde
2731
- rust: stable
2832
features: std
2933
- rust: beta

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ rust-version = "1.61"
1515
bench = false
1616

1717
[dependencies]
18+
arbitrary = { version = "1.0", optional = true, default-features = false }
19+
quickcheck = { version = "1.0", optional = true, default-features = false }
1820
serde = { version = "1.0", optional = true, default-features = false }
1921
rayon = { version = "1.4.1", optional = true }
2022

@@ -51,7 +53,7 @@ no-dev-version = true
5153
tag-name = "{{version}}"
5254

5355
[package.metadata.docs.rs]
54-
features = ["serde", "rayon"]
56+
features = ["arbitrary", "quickcheck", "serde", "rayon"]
5557

5658
[workspace]
5759
members = ["test-nostd", "test-serde"]

RELEASES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
comparison traits like `Eq` only consider items in order, rather than hash
1818
lookups, and slices even implement `Hash`.
1919

20+
- `IndexMap` and `IndexSet` both implement `arbitrary::Arbitrary<'_>` and
21+
`quickcheck::Arbitrary` if those optional dependency features are enabled.
22+
2023
- The `hashbrown` dependency has been updated to version 0.13.
2124

2225
- 1.9.1

src/arbitrary.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#[cfg(feature = "arbitrary")]
2+
mod impl_arbitrary {
3+
use crate::{IndexMap, IndexSet};
4+
use arbitrary::{Arbitrary, Result, Unstructured};
5+
use core::hash::{BuildHasher, Hash};
6+
7+
impl<'a, K, V, S> Arbitrary<'a> for IndexMap<K, V, S>
8+
where
9+
K: Arbitrary<'a> + Hash + Eq,
10+
V: Arbitrary<'a>,
11+
S: BuildHasher + Default,
12+
{
13+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
14+
u.arbitrary_iter()?.collect()
15+
}
16+
17+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
18+
u.arbitrary_take_rest_iter()?.collect()
19+
}
20+
}
21+
22+
impl<'a, T, S> Arbitrary<'a> for IndexSet<T, S>
23+
where
24+
T: Arbitrary<'a> + Hash + Eq,
25+
S: BuildHasher + Default,
26+
{
27+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
28+
u.arbitrary_iter()?.collect()
29+
}
30+
31+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
32+
u.arbitrary_take_rest_iter()?.collect()
33+
}
34+
}
35+
}
36+
37+
#[cfg(feature = "quickcheck")]
38+
mod impl_quickcheck {
39+
use crate::{IndexMap, IndexSet};
40+
use alloc::boxed::Box;
41+
use alloc::vec::Vec;
42+
use core::hash::{BuildHasher, Hash};
43+
use quickcheck::{Arbitrary, Gen};
44+
45+
impl<K, V, S> Arbitrary for IndexMap<K, V, S>
46+
where
47+
K: Arbitrary + Hash + Eq,
48+
V: Arbitrary,
49+
S: BuildHasher + Default + Clone + 'static,
50+
{
51+
fn arbitrary(g: &mut Gen) -> Self {
52+
Self::from_iter(Vec::arbitrary(g))
53+
}
54+
55+
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
56+
let vec = Vec::from_iter(self.clone());
57+
Box::new(vec.shrink().map(Self::from_iter))
58+
}
59+
}
60+
61+
impl<T, S> Arbitrary for IndexSet<T, S>
62+
where
63+
T: Arbitrary + Hash + Eq,
64+
S: BuildHasher + Default + Clone + 'static,
65+
{
66+
fn arbitrary(g: &mut Gen) -> Self {
67+
Self::from_iter(Vec::arbitrary(g))
68+
}
69+
70+
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
71+
let vec = Vec::from_iter(self.clone());
72+
Box::new(vec.shrink().map(Self::from_iter))
73+
}
74+
}
75+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extern crate std;
8484

8585
use alloc::vec::{self, Vec};
8686

87+
mod arbitrary;
8788
#[macro_use]
8889
mod macros;
8990
mod equivalent;

0 commit comments

Comments
 (0)