Skip to content

Commit

Permalink
Implement Arbitrary for Arc<[A]> and Rc<[A]>.
Browse files Browse the repository at this point in the history
Also adjust the `Box<[A]>` implementation to be simpler and regular,
not using a conversion from `Vec`, and provide an implementation of
`arbitrary_take_rest()` rather than using the default.
  • Loading branch information
kpreid committed Oct 30, 2023
1 parent b3e8342 commit ef5dff6
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,12 +934,16 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice())
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>) {
<Vec<A> as Arbitrary>::size_hint(depth)
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}

Expand Down Expand Up @@ -978,6 +982,21 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
}
}

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[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)
Expand All @@ -1000,6 +1019,21 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
}
}

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[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)
Expand Down Expand Up @@ -1393,6 +1427,18 @@ mod test {
checked_arbitrary::<Vec<u8>>(&mut Unstructured::new(&x)).unwrap(),
&[2, 4, 6, 8, 1]
);
assert_eq!(
&*checked_arbitrary::<Box<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
&[2, 4, 6, 8, 1]
);
assert_eq!(
&*checked_arbitrary::<Arc<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
&[2, 4, 6, 8, 1]
);
assert_eq!(
&*checked_arbitrary::<Rc<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
&[2, 4, 6, 8, 1]
);
assert_eq!(
checked_arbitrary::<Vec<u32>>(&mut Unstructured::new(&x)).unwrap(),
&[84148994]
Expand All @@ -1415,6 +1461,18 @@ mod test {
checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&x)).unwrap(),
&[2, 4]
);
assert_eq!(
&*checked_arbitrary_take_rest::<Box<[u8]>>(Unstructured::new(&x)).unwrap(),
&[2, 4]
);
assert_eq!(
&*checked_arbitrary_take_rest::<Arc<[u8]>>(Unstructured::new(&x)).unwrap(),
&[2, 4]
);
assert_eq!(
&*checked_arbitrary_take_rest::<Rc<[u8]>>(Unstructured::new(&x)).unwrap(),
&[2, 4]
);
assert_eq!(
checked_arbitrary_take_rest::<Vec<u32>>(Unstructured::new(&x)).unwrap(),
&[0x040302]
Expand Down

0 comments on commit ef5dff6

Please sign in to comment.