Skip to content

Commit e54d1c7

Browse files
committed
refactor(foreign): move implementations to dedicated directory
At the moment, the trait-definition and all implementations for foreign types are located in `lib.rs` with 1732 LOC. IMHO this is difficult to maintain, especially when adding additional implementations. By moving all implementations into the private submodule foreign, sorting them by `core`, `alloc` and `std`, and submodules similar to those documented, I see i.a. the following advantages: 1. Easier to find for which types the trait is implemented, which implementations are missing, 2. If one wants to add a implementation, one can just place it in the module according to its type definition in `core`/`alloc`/`std` and does not have to guess, whether to place it at the beginning or end of `lib.rs` or somewhere random, 3. It will be easier to feature-gate some implementations, e.g. `std` for std-implementations, which should be a default-feature, which can be disabled for `no_std`-builds, 4. Implementations for additional (common) crates, e.g. `chrono` or `time`, can be placed in foreign behind feature-gates, 5. Changes to one file does not affect changes to other files, so merging multiple PRs might be less conflicting. Somewhat unrelated changes: Harmonising trait-bounds-placement.
1 parent 7bd1044 commit e54d1c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1592
-1339
lines changed

src/foreign/alloc/borrow.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use {
2+
crate::{size_hint, Arbitrary, Result, Unstructured},
3+
std::borrow::{Cow, ToOwned},
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for Cow<'a, A>
7+
where
8+
A: ToOwned + ?Sized,
9+
<A as ToOwned>::Owned: Arbitrary<'a>,
10+
{
11+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
12+
Arbitrary::arbitrary(u).map(Cow::Owned)
13+
}
14+
15+
#[inline]
16+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
17+
size_hint::recursion_guard(depth, |depth| {
18+
<<A as ToOwned>::Owned as Arbitrary>::size_hint(depth)
19+
})
20+
}
21+
}

src/foreign/alloc/boxed.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use {
2+
crate::{size_hint, Arbitrary, Result, Unstructured},
3+
std::boxed::Box,
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for Box<A>
7+
where
8+
A: Arbitrary<'a>,
9+
{
10+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11+
Arbitrary::arbitrary(u).map(Self::new)
12+
}
13+
14+
#[inline]
15+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
16+
size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint)
17+
}
18+
}
19+
20+
impl<'a, A> Arbitrary<'a> for Box<[A]>
21+
where
22+
A: Arbitrary<'a>,
23+
{
24+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
25+
u.arbitrary_iter()?.collect()
26+
}
27+
28+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
29+
u.arbitrary_take_rest_iter()?.collect()
30+
}
31+
32+
#[inline]
33+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
34+
(0, None)
35+
}
36+
}
37+
38+
impl<'a> Arbitrary<'a> for Box<str> {
39+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
40+
<String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str())
41+
}
42+
43+
#[inline]
44+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
45+
<String as Arbitrary>::size_hint(depth)
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::collections::binary_heap::BinaryHeap,
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for BinaryHeap<A>
7+
where
8+
A: Arbitrary<'a> + Ord,
9+
{
10+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11+
u.arbitrary_iter()?.collect()
12+
}
13+
14+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
15+
u.arbitrary_take_rest_iter()?.collect()
16+
}
17+
18+
#[inline]
19+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
20+
(0, None)
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::collections::btree_map::BTreeMap,
4+
};
5+
6+
impl<'a, K, V> Arbitrary<'a> for BTreeMap<K, V>
7+
where
8+
K: Arbitrary<'a> + Ord,
9+
V: Arbitrary<'a>,
10+
{
11+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
12+
u.arbitrary_iter()?.collect()
13+
}
14+
15+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
16+
u.arbitrary_take_rest_iter()?.collect()
17+
}
18+
19+
#[inline]
20+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
21+
(0, None)
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::collections::btree_set::BTreeSet,
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for BTreeSet<A>
7+
where
8+
A: Arbitrary<'a> + Ord,
9+
{
10+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11+
u.arbitrary_iter()?.collect()
12+
}
13+
14+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
15+
u.arbitrary_take_rest_iter()?.collect()
16+
}
17+
18+
#[inline]
19+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
20+
(0, None)
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::collections::linked_list::LinkedList,
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for LinkedList<A>
7+
where
8+
A: Arbitrary<'a>,
9+
{
10+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11+
u.arbitrary_iter()?.collect()
12+
}
13+
14+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
15+
u.arbitrary_take_rest_iter()?.collect()
16+
}
17+
18+
#[inline]
19+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
20+
(0, None)
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod binary_heap;
2+
mod btree_map;
3+
mod btree_set;
4+
mod linked_list;
5+
mod vec_deque;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::collections::vec_deque::VecDeque,
4+
};
5+
6+
impl<'a, A> Arbitrary<'a> for VecDeque<A>
7+
where
8+
A: Arbitrary<'a>,
9+
{
10+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11+
u.arbitrary_iter()?.collect()
12+
}
13+
14+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
15+
u.arbitrary_take_rest_iter()?.collect()
16+
}
17+
18+
#[inline]
19+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
20+
(0, None)
21+
}
22+
}

src/foreign/alloc/ffi/c_str.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use {
2+
crate::{Arbitrary, Result, Unstructured},
3+
std::ffi::CString,
4+
};
5+
6+
impl<'a> Arbitrary<'a> for CString {
7+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
8+
<Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
9+
x.retain(|&c| c != 0);
10+
// SAFETY: all zero bytes have been removed
11+
unsafe { Self::from_vec_unchecked(x) }
12+
})
13+
}
14+
15+
#[inline]
16+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
17+
<Vec<u8> as Arbitrary>::size_hint(depth)
18+
}
19+
}

src/foreign/alloc/ffi/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod c_str;

0 commit comments

Comments
 (0)