mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-21 01:35:22 -04:00
Add arbitrary implementations for TinyVec and ArrayVec (#146)
This commit is contained in:
@@ -13,6 +13,8 @@ repository = "https://github.com/Lokathor/tinyvec"
|
||||
tinyvec_macros = { version = "0.1", optional = true }
|
||||
# Provides `Serialize` and `Deserialize` implementations
|
||||
serde = { version = "1.0", optional = true, default-features = false }
|
||||
# Provides derived `Arbitrary` implementations
|
||||
arbitrary = { version = "1", optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -180,6 +180,22 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "arbitrary", feature = "nightly_const_generics"))]
|
||||
#[cfg_attr(
|
||||
docs_rs,
|
||||
doc(cfg(all(feature = "arbitrary", feature = "nightly_const_generics")))
|
||||
)]
|
||||
impl<'a, T, const N: usize> arbitrary::Arbitrary<'a> for ArrayVec<[T; N]>
|
||||
where
|
||||
T: arbitrary::Arbitrary<'a> + Default,
|
||||
{
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
|
||||
let v = <[T; N]>::arbitrary(u)?;
|
||||
let av = ArrayVec::from(v);
|
||||
Ok(av)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> ArrayVec<A> {
|
||||
/// Move all values from `other` into this vec.
|
||||
///
|
||||
|
||||
@@ -178,6 +178,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
#[cfg_attr(docs_rs, doc(cfg(feature = "arbitrary")))]
|
||||
impl<'a, A> arbitrary::Arbitrary<'a> for TinyVec<A>
|
||||
where
|
||||
A: Array,
|
||||
A::Item: arbitrary::Arbitrary<'a>,
|
||||
{
|
||||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
|
||||
let v = Vec::arbitrary(u)?;
|
||||
let mut tv = TinyVec::Heap(v);
|
||||
tv.shrink_to_fit();
|
||||
Ok(tv)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> TinyVec<A> {
|
||||
/// Returns whether elements are on heap
|
||||
#[inline(always)]
|
||||
|
||||
Reference in New Issue
Block a user