Skip to content

Commit

Permalink
Merge pull request #247 from cuviper/arbitrary-1.x
Browse files Browse the repository at this point in the history
Backport Arbitrary (#246) and release 1.9.2
  • Loading branch information
cuviper authored Nov 17, 2022
2 parents fe98ec2 + 4d41050 commit 4d52cf3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ jobs:
- rust: 1.56.0 # MSRV
features:
- rust: stable
features: serde
features: arbitrary
- rust: stable
features: quickcheck
- rust: stable
features: rayon
- rust: stable
features: rustc-rayon
- rust: stable
features: serde
- rust: stable
features: std
- rust: beta
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "indexmap"
edition = "2021"
version = "1.9.1"
version = "1.9.2"
documentation = "https://docs.rs/indexmap/"
repository = "https://github.com/bluss/indexmap"
license = "Apache-2.0 OR MIT"
Expand All @@ -17,6 +17,8 @@ bench = false
autocfg = "1"

[dependencies]
arbitrary = { version = "1.0", optional = true, default-features = false }
quickcheck = { version = "1.0", optional = true, default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
rayon = { version = "1.4.1", optional = true }

Expand Down Expand Up @@ -57,7 +59,7 @@ no-dev-version = true
tag-name = "{{version}}"

[package.metadata.docs.rs]
features = ["serde-1", "rayon"]
features = ["arbitrary", "quickcheck", "serde-1", "rayon"]

[workspace]
members = ["test-nostd", "test-serde"]
5 changes: 5 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
- 1.9.2

- `IndexMap` and `IndexSet` both implement `arbitrary::Arbitrary<'_>` and
`quickcheck::Arbitrary` if those optional dependency features are enabled.

- 1.9.1

- The MSRV now allows Rust 1.56.0 as well. However, currently `hashbrown`
Expand Down
75 changes: 75 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#[cfg(feature = "arbitrary")]
mod impl_arbitrary {
use crate::{IndexMap, IndexSet};
use arbitrary::{Arbitrary, Result, Unstructured};
use core::hash::{BuildHasher, Hash};

impl<'a, K, V, S> Arbitrary<'a> for IndexMap<K, V, S>
where
K: Arbitrary<'a> + Hash + Eq,
V: Arbitrary<'a>,
S: BuildHasher + Default,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
}

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
u.arbitrary_take_rest_iter()?.collect()
}
}

impl<'a, T, S> Arbitrary<'a> for IndexSet<T, S>
where
T: Arbitrary<'a> + Hash + Eq,
S: BuildHasher + Default,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
}

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
u.arbitrary_take_rest_iter()?.collect()
}
}
}

#[cfg(feature = "quickcheck")]
mod impl_quickcheck {
use crate::{IndexMap, IndexSet};
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::hash::{BuildHasher, Hash};
use quickcheck::{Arbitrary, Gen};

impl<K, V, S> Arbitrary for IndexMap<K, V, S>
where
K: Arbitrary + Hash + Eq,
V: Arbitrary,
S: BuildHasher + Default + Clone + 'static,
{
fn arbitrary(g: &mut Gen) -> Self {
Self::from_iter(Vec::arbitrary(g))
}

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let vec = Vec::from_iter(self.clone());
Box::new(vec.shrink().map(Self::from_iter))
}
}

impl<T, S> Arbitrary for IndexSet<T, S>
where
T: Arbitrary + Hash + Eq,
S: BuildHasher + Default + Clone + 'static,
{
fn arbitrary(g: &mut Gen) -> Self {
Self::from_iter(Vec::arbitrary(g))
}

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let vec = Vec::from_iter(self.clone());
Box::new(vec.shrink().map(Self::from_iter))
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ extern crate std;

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

mod arbitrary;
#[macro_use]
mod macros;
mod equivalent;
Expand Down

0 comments on commit 4d52cf3

Please sign in to comment.