Skip to content

Commit d97bd18

Browse files
committed
feat: introduce ArbitraryInRange
1 parent 1cc0e46 commit d97bd18

File tree

26 files changed

+1053
-768
lines changed

26 files changed

+1053
-768
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub enum Error {
1111
NotEnoughData,
1212
/// The input bytes were not of the right format
1313
IncorrectFormat,
14+
/// Cannot generate value in given range
15+
InvalidRange,
1416
}
1517

1618
impl fmt::Display for Error {
@@ -28,6 +30,7 @@ impl fmt::Display for Error {
2830
f,
2931
"The raw data is not of the correct format to construct this type"
3032
),
33+
Error::InvalidRange => write!(f, "Cannot generate a value in given range"),
3134
}
3235
}
3336
}

src/foreign/alloc/boxed.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
use {
2-
crate::{size_hint, Arbitrary, Result, Unstructured},
2+
crate::{Arbitrary, Result, Unstructured},
33
std::boxed::Box,
44
};
55

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-
Self::try_size_hint(depth).unwrap_or_default()
17-
}
18-
19-
#[inline]
20-
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
21-
size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
22-
}
23-
}
24-
25-
impl<'a, A> Arbitrary<'a> for Box<[A]>
26-
where
27-
A: Arbitrary<'a>,
28-
{
29-
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
30-
u.arbitrary_iter()?.collect()
31-
}
32-
33-
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
34-
u.arbitrary_take_rest_iter()?.collect()
35-
}
36-
37-
#[inline]
38-
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
39-
(0, None)
40-
}
41-
}
6+
implement_wrapped_new! { Box! }
7+
implement_from_iter! { Box<[A]> }
428

439
impl<'a> Arbitrary<'a> for Box<str> {
4410
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::collections::binary_heap::BinaryHeap,
4-
};
1+
use std::collections::binary_heap::BinaryHeap;
52

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-
}
3+
implement_from_iter! { BinaryHeap<A>: Ord }
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::collections::btree_set::BTreeSet,
4-
};
1+
use std::collections::btree_set::BTreeSet;
52

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-
}
3+
implement_from_iter! { BTreeSet<A>: Ord }
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::collections::linked_list::LinkedList,
4-
};
1+
use std::collections::linked_list::LinkedList;
52

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-
}
3+
implement_from_iter! { LinkedList<A> }
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::collections::vec_deque::VecDeque,
4-
};
1+
use std::collections::vec_deque::VecDeque;
52

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-
}
3+
implement_from_iter! { VecDeque<A> }

src/foreign/alloc/rc.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
use {
2-
crate::{size_hint, Arbitrary, Result, Unstructured},
2+
crate::{Arbitrary, Result, Unstructured},
33
std::rc::Rc,
44
};
55

6-
impl<'a, A> Arbitrary<'a> for Rc<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-
Self::try_size_hint(depth).unwrap_or_default()
17-
}
18-
19-
#[inline]
20-
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
21-
size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
22-
}
23-
}
24-
25-
impl<'a, A> Arbitrary<'a> for Rc<[A]>
26-
where
27-
A: Arbitrary<'a>,
28-
{
29-
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
30-
u.arbitrary_iter()?.collect()
31-
}
32-
33-
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
34-
u.arbitrary_take_rest_iter()?.collect()
35-
}
36-
37-
#[inline]
38-
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
39-
(0, None)
40-
}
41-
}
6+
implement_wrapped_new! { Rc! }
7+
implement_from_iter! { Rc<[A]> }
428

439
impl<'a> Arbitrary<'a> for Rc<str> {
4410
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {

src/foreign/alloc/string.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::string::String,
2+
crate::{Arbitrary, ArbitraryInRange, Result, Unstructured},
3+
std::{ops::RangeBounds, string::String},
44
};
55

6+
impl<'a> ArbitraryInRange<'a> for String {
7+
type Bound = char;
8+
9+
fn arbitrary_in_range<R>(u: &mut Unstructured<'a>, range: &R) -> Result<Self>
10+
where
11+
R: RangeBounds<Self::Bound>,
12+
{
13+
u.arbitrary_in_range_iter::<Self::Bound, _>(range)?
14+
.collect()
15+
}
16+
}
17+
618
impl<'a> Arbitrary<'a> for String {
719
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
820
<&str as Arbitrary>::arbitrary(u).map(Into::into)

src/foreign/alloc/sync.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
use {
2-
crate::{size_hint, Arbitrary, Result, Unstructured},
2+
crate::{Arbitrary, Result, Unstructured},
33
std::sync::Arc,
44
};
55

6-
impl<'a, A> Arbitrary<'a> for Arc<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-
Self::try_size_hint(depth).unwrap_or_default()
17-
}
18-
19-
#[inline]
20-
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
21-
size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
22-
}
23-
}
24-
25-
impl<'a, A> Arbitrary<'a> for Arc<[A]>
26-
where
27-
A: Arbitrary<'a>,
28-
{
29-
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
30-
u.arbitrary_iter()?.collect()
31-
}
32-
33-
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
34-
u.arbitrary_take_rest_iter()?.collect()
35-
}
36-
37-
#[inline]
38-
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
39-
(0, None)
40-
}
41-
}
6+
implement_wrapped_new! { Arc! }
7+
implement_from_iter! { Arc<[A]> }
428

439
impl<'a> Arbitrary<'a> for Arc<str> {
4410
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {

src/foreign/alloc/vec.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
use {
2-
crate::{Arbitrary, Result, Unstructured},
3-
std::vec::Vec,
4-
};
1+
use std::vec::Vec;
52

6-
impl<'a, A> Arbitrary<'a> for Vec<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-
}
3+
implement_from_iter! { Vec<A> }

0 commit comments

Comments
 (0)