Skip to content

Commit

Permalink
Auto merge of #275 - as-com:arbitrary-support, r=mbrubeck
Browse files Browse the repository at this point in the history
Add support for arbitrary

This PR adds optional support for [Arbitrary](https://github.com/rust-fuzz/arbitrary/), which is helpful in fuzz testing. The implementation is nearly identical to Arbitrary's existing Vec implementation.
  • Loading branch information
bors-servo committed Jan 14, 2022
2 parents 7cbb3b1 + 9bcd950 commit 6d0dea5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ may_dangle = []

[dependencies]
serde = { version = "1", optional = true, default-features = false }
arbitrary = { version = "1", optional = true }

[dev_dependencies]
bincode = "1.0.1"
Expand Down
19 changes: 19 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{Array, SmallVec};
use arbitrary::{Arbitrary, Unstructured};

impl<'a, A: Array> Arbitrary<'a> for SmallVec<A>
where
<A as Array>::Item: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
u.arbitrary_iter()?.collect()
}

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

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,9 @@ trait SpecFrom<A: Array, S> {
#[cfg(feature = "specialization")]
mod specialization;

#[cfg(feature = "arbitrary")]
mod arbitrary;

#[cfg(feature = "specialization")]
impl<'a, A: Array> SpecFrom<A, &'a [A::Item]> for SmallVec<A>
where
Expand Down

0 comments on commit 6d0dea5

Please sign in to comment.