Skip to content

Commit

Permalink
Add support for arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
as-com committed Jan 13, 2022
1 parent 7cbb3b1 commit 9bcd950
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 9bcd950

Please sign in to comment.