Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(foreign): move implementations to dedicated, structured directory #189

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/foreign/alloc/borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use {
crate::{size_hint, Arbitrary, Result, Unstructured},
std::borrow::{Cow, ToOwned},
};

impl<'a, A> Arbitrary<'a> for Cow<'a, A>
where
A: ToOwned + ?Sized,
<A as ToOwned>::Owned: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Cow::Owned)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
size_hint::recursion_guard(depth, |depth| {
<<A as ToOwned>::Owned as Arbitrary>::size_hint(depth)
})
}
}
47 changes: 47 additions & 0 deletions src/foreign/alloc/boxed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use {
crate::{size_hint, Arbitrary, Result, Unstructured},
std::boxed::Box,
};

impl<'a, A> Arbitrary<'a> for Box<A>
where
A: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint)
}
}

impl<'a, A> Arbitrary<'a> for Box<[A]>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}

impl<'a> Arbitrary<'a> for Box<str> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str())
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<String as Arbitrary>::size_hint(depth)
}
}
22 changes: 22 additions & 0 deletions src/foreign/alloc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::collections::binary_heap::BinaryHeap,
};

impl<'a, A> Arbitrary<'a> for BinaryHeap<A>
where
A: Arbitrary<'a> + Ord,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
23 changes: 23 additions & 0 deletions src/foreign/alloc/collections/btree_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::collections::btree_map::BTreeMap,
};

impl<'a, K, V> Arbitrary<'a> for BTreeMap<K, V>
where
K: Arbitrary<'a> + Ord,
V: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
22 changes: 22 additions & 0 deletions src/foreign/alloc/collections/btree_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::collections::btree_set::BTreeSet,
};

impl<'a, A> Arbitrary<'a> for BTreeSet<A>
where
A: Arbitrary<'a> + Ord,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
22 changes: 22 additions & 0 deletions src/foreign/alloc/collections/linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::collections::linked_list::LinkedList,
};

impl<'a, A> Arbitrary<'a> for LinkedList<A>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
5 changes: 5 additions & 0 deletions src/foreign/alloc/collections/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod binary_heap;
mod btree_map;
mod btree_set;
mod linked_list;
mod vec_deque;
22 changes: 22 additions & 0 deletions src/foreign/alloc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::collections::vec_deque::VecDeque,
};

impl<'a, A> Arbitrary<'a> for VecDeque<A>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
19 changes: 19 additions & 0 deletions src/foreign/alloc/ffi/c_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::ffi::CString,
};

impl<'a> Arbitrary<'a> for CString {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
x.retain(|&c| c != 0);
// SAFETY: all zero bytes have been removed
unsafe { Self::from_vec_unchecked(x) }
})
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<Vec<u8> as Arbitrary>::size_hint(depth)
}
}
1 change: 1 addition & 0 deletions src/foreign/alloc/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod c_str;
13 changes: 13 additions & 0 deletions src/foreign/alloc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Implementations of [`Arbitrary`] for [`alloc`] types,
//! excluding those in [`core`].
//!
//! [`Arbitrary`]: crate::Arbitrary

mod borrow;
mod boxed;
mod collections;
mod ffi;
mod rc;
mod string;
mod sync;
mod vec;
47 changes: 47 additions & 0 deletions src/foreign/alloc/rc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use {
crate::{size_hint, Arbitrary, Result, Unstructured},
std::rc::Rc,
};

impl<'a, A> Arbitrary<'a> for Rc<A>
where
A: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint)
}
}

impl<'a, A> Arbitrary<'a> for Rc<[A]>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}

impl<'a> Arbitrary<'a> for Rc<str> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<&str as Arbitrary>::arbitrary(u).map(Into::into)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<&str as Arbitrary>::size_hint(depth)
}
}
19 changes: 19 additions & 0 deletions src/foreign/alloc/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::string::String,
};

impl<'a> Arbitrary<'a> for String {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<&str as Arbitrary>::arbitrary(u).map(Into::into)
}

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
<&str as Arbitrary>::arbitrary_take_rest(u).map(Into::into)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<&str as Arbitrary>::size_hint(depth)
}
}
47 changes: 47 additions & 0 deletions src/foreign/alloc/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use {
crate::{size_hint, Arbitrary, Result, Unstructured},
std::sync::Arc,
};

impl<'a, A> Arbitrary<'a> for Arc<A>
where
A: Arbitrary<'a>,
{
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint)
}
}

impl<'a, A> Arbitrary<'a> for Arc<[A]>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}

impl<'a> Arbitrary<'a> for Arc<str> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<&str as Arbitrary>::arbitrary(u).map(Into::into)
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<&str as Arbitrary>::size_hint(depth)
}
}
22 changes: 22 additions & 0 deletions src/foreign/alloc/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::{Arbitrary, Result, Unstructured},
std::vec::Vec,
};

impl<'a, A> Arbitrary<'a> for Vec<A>
where
A: Arbitrary<'a>,
{
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()
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}
Loading
Loading