Skip to content

Commit ef5dff6

Browse files
committed
Implement Arbitrary for Arc<[A]> and Rc<[A]>.
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.
1 parent b3e8342 commit ef5dff6

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

src/lib.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,12 +934,16 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
934934

935935
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
936936
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
937-
<Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice())
937+
u.arbitrary_iter()?.collect()
938+
}
939+
940+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
941+
u.arbitrary_take_rest_iter()?.collect()
938942
}
939943

940944
#[inline]
941-
fn size_hint(depth: usize) -> (usize, Option<usize>) {
942-
<Vec<A> as Arbitrary>::size_hint(depth)
945+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
946+
(0, None)
943947
}
944948
}
945949

@@ -978,6 +982,21 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
978982
}
979983
}
980984

985+
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> {
986+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
987+
u.arbitrary_iter()?.collect()
988+
}
989+
990+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
991+
u.arbitrary_take_rest_iter()?.collect()
992+
}
993+
994+
#[inline]
995+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
996+
(0, None)
997+
}
998+
}
999+
9811000
impl<'a> Arbitrary<'a> for Arc<str> {
9821001
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
9831002
<&str as Arbitrary>::arbitrary(u).map(Into::into)
@@ -1000,6 +1019,21 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
10001019
}
10011020
}
10021021

1022+
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> {
1023+
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
1024+
u.arbitrary_iter()?.collect()
1025+
}
1026+
1027+
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
1028+
u.arbitrary_take_rest_iter()?.collect()
1029+
}
1030+
1031+
#[inline]
1032+
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
1033+
(0, None)
1034+
}
1035+
}
1036+
10031037
impl<'a> Arbitrary<'a> for Rc<str> {
10041038
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
10051039
<&str as Arbitrary>::arbitrary(u).map(Into::into)
@@ -1393,6 +1427,18 @@ mod test {
13931427
checked_arbitrary::<Vec<u8>>(&mut Unstructured::new(&x)).unwrap(),
13941428
&[2, 4, 6, 8, 1]
13951429
);
1430+
assert_eq!(
1431+
&*checked_arbitrary::<Box<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
1432+
&[2, 4, 6, 8, 1]
1433+
);
1434+
assert_eq!(
1435+
&*checked_arbitrary::<Arc<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
1436+
&[2, 4, 6, 8, 1]
1437+
);
1438+
assert_eq!(
1439+
&*checked_arbitrary::<Rc<[u8]>>(&mut Unstructured::new(&x)).unwrap(),
1440+
&[2, 4, 6, 8, 1]
1441+
);
13961442
assert_eq!(
13971443
checked_arbitrary::<Vec<u32>>(&mut Unstructured::new(&x)).unwrap(),
13981444
&[84148994]
@@ -1415,6 +1461,18 @@ mod test {
14151461
checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&x)).unwrap(),
14161462
&[2, 4]
14171463
);
1464+
assert_eq!(
1465+
&*checked_arbitrary_take_rest::<Box<[u8]>>(Unstructured::new(&x)).unwrap(),
1466+
&[2, 4]
1467+
);
1468+
assert_eq!(
1469+
&*checked_arbitrary_take_rest::<Arc<[u8]>>(Unstructured::new(&x)).unwrap(),
1470+
&[2, 4]
1471+
);
1472+
assert_eq!(
1473+
&*checked_arbitrary_take_rest::<Rc<[u8]>>(Unstructured::new(&x)).unwrap(),
1474+
&[2, 4]
1475+
);
14181476
assert_eq!(
14191477
checked_arbitrary_take_rest::<Vec<u32>>(Unstructured::new(&x)).unwrap(),
14201478
&[0x040302]

0 commit comments

Comments
 (0)