From 9bcd950f253d8eaed9173bee77cb46caee171460 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Thu, 13 Jan 2022 01:40:47 -0500 Subject: [PATCH] Add support for arbitrary --- Cargo.toml | 1 + src/arbitrary.rs | 19 +++++++++++++++++++ src/lib.rs | 3 +++ 3 files changed, 23 insertions(+) create mode 100644 src/arbitrary.rs diff --git a/Cargo.toml b/Cargo.toml index 92021aa..b565ff7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/arbitrary.rs b/src/arbitrary.rs new file mode 100644 index 0000000..cbdfcb0 --- /dev/null +++ b/src/arbitrary.rs @@ -0,0 +1,19 @@ +use crate::{Array, SmallVec}; +use arbitrary::{Arbitrary, Unstructured}; + +impl<'a, A: Array> Arbitrary<'a> for SmallVec +where + ::Item: Arbitrary<'a>, +{ + fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { + u.arbitrary_iter()?.collect() + } + + fn arbitrary_take_rest(u: Unstructured<'a>) -> arbitrary::Result { + u.arbitrary_take_rest_iter()?.collect() + } + + fn size_hint(depth: usize) -> (usize, Option) { + arbitrary::size_hint::and(::size_hint(depth), (0, None)) + } +} diff --git a/src/lib.rs b/src/lib.rs index ba9cd78..1699a71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1651,6 +1651,9 @@ trait SpecFrom { #[cfg(feature = "specialization")] mod specialization; +#[cfg(feature = "arbitrary")] +mod arbitrary; + #[cfg(feature = "specialization")] impl<'a, A: Array> SpecFrom for SmallVec where