1414//! [`Arbitrary`](./trait.Arbitrary.html) trait's documentation for details on
1515//! automatically deriving, implementing, and/or using the trait.
1616
17+ #![ cfg_attr( not( any( feature = "std" , test) ) , no_std) ]
1718#![ deny( bad_style) ]
1819#![ deny( missing_docs) ]
1920#![ deny( future_incompatible) ]
@@ -43,16 +44,40 @@ use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZ
4344use core:: ops:: { Range , RangeBounds , RangeFrom , RangeInclusive , RangeTo , RangeToInclusive } ;
4445use core:: str;
4546use core:: time:: Duration ;
47+
48+ #[ cfg( feature = "alloc" ) ]
49+ extern crate alloc;
50+
51+ #[ cfg( feature = "alloc" ) ]
52+ use alloc:: borrow:: { Cow , ToOwned } ;
53+ #[ cfg( feature = "alloc" ) ]
54+ use alloc:: boxed:: Box ;
55+ #[ cfg( feature = "alloc" ) ]
56+ use alloc:: collections:: { BTreeMap , BTreeSet , BinaryHeap , LinkedList , VecDeque } ;
57+ #[ cfg( feature = "alloc" ) ]
58+ use alloc:: ffi:: CString ;
59+ #[ cfg( feature = "alloc" ) ]
60+ use alloc:: rc:: Rc ;
61+ #[ cfg( feature = "alloc" ) ]
62+ use alloc:: string:: String ;
63+ #[ cfg( feature = "alloc" ) ]
64+ use alloc:: sync:: Arc ;
65+ #[ cfg( feature = "alloc" ) ]
66+ use alloc:: vec:: Vec ;
4667use core:: ops:: Bound ;
4768use core:: sync:: atomic:: { AtomicBool , AtomicIsize , AtomicUsize } ;
48- use std:: borrow:: { Cow , ToOwned } ;
49- use std:: collections:: { BTreeMap , BTreeSet , BinaryHeap , HashMap , HashSet , LinkedList , VecDeque } ;
50- use std:: ffi:: { CString , OsString } ;
69+ #[ cfg( feature = "std" ) ]
70+ use std:: collections:: { HashMap , HashSet } ;
71+ #[ cfg( feature = "std" ) ]
72+ use std:: ffi:: OsString ;
73+ #[ cfg( feature = "std" ) ]
5174use std:: hash:: BuildHasher ;
75+ #[ cfg( feature = "std" ) ]
5276use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr , SocketAddrV4 , SocketAddrV6 } ;
77+ #[ cfg( feature = "std" ) ]
5378use std:: path:: PathBuf ;
54- use std:: rc :: Rc ;
55- use std:: sync:: { Arc , Mutex } ;
79+ # [ cfg ( feature = " std" ) ]
80+ use std:: sync:: Mutex ;
5681
5782/// Generate arbitrary structured values from raw, unstructured data.
5883///
@@ -81,11 +106,13 @@ use std::sync::{Arc, Mutex};
81106/// use arbitrary::Arbitrary;
82107/// use std::collections::HashSet;
83108///
109+ /// # #[cfg(feature = "std")]
84110/// #[derive(Arbitrary)]
85111/// pub struct AddressBook {
86112/// friends: HashSet<Friend>,
87113/// }
88114///
115+ /// # #[cfg(feature = "alloc")]
89116/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
90117/// pub enum Friend {
91118/// Buddy { name: String },
@@ -690,6 +717,7 @@ impl<'a> Arbitrary<'a> for &'a [u8] {
690717 }
691718}
692719
720+ #[ cfg( feature = "alloc" ) ]
693721impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Vec < A > {
694722 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
695723 u. arbitrary_iter ( ) ?. collect ( )
@@ -705,6 +733,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
705733 }
706734}
707735
736+ #[ cfg( feature = "alloc" ) ]
708737impl < ' a , K : Arbitrary < ' a > + Ord , V : Arbitrary < ' a > > Arbitrary < ' a > for BTreeMap < K , V > {
709738 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
710739 u. arbitrary_iter ( ) ?. collect ( )
@@ -720,6 +749,7 @@ impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K,
720749 }
721750}
722751
752+ #[ cfg( feature = "alloc" ) ]
723753impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BTreeSet < A > {
724754 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
725755 u. arbitrary_iter ( ) ?. collect ( )
@@ -754,6 +784,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> {
754784 }
755785}
756786
787+ #[ cfg( feature = "alloc" ) ]
757788impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BinaryHeap < A > {
758789 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
759790 u. arbitrary_iter ( ) ?. collect ( )
@@ -769,7 +800,8 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
769800 }
770801}
771802
772- impl < ' a , K : Arbitrary < ' a > + Eq + :: std:: hash:: Hash , V : Arbitrary < ' a > , S : BuildHasher + Default >
803+ #[ cfg( feature = "std" ) ]
804+ impl < ' a , K : Arbitrary < ' a > + Eq + core:: hash:: Hash , V : Arbitrary < ' a > , S : BuildHasher + Default >
773805 Arbitrary < ' a > for HashMap < K , V , S >
774806{
775807 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
@@ -786,7 +818,8 @@ impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>, S: BuildHa
786818 }
787819}
788820
789- impl < ' a , A : Arbitrary < ' a > + Eq + :: std:: hash:: Hash , S : BuildHasher + Default > Arbitrary < ' a >
821+ #[ cfg( feature = "std" ) ]
822+ impl < ' a , A : Arbitrary < ' a > + Eq + core:: hash:: Hash , S : BuildHasher + Default > Arbitrary < ' a >
790823 for HashSet < A , S >
791824{
792825 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
@@ -803,6 +836,7 @@ impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash, S: BuildHasher + Default> Ar
803836 }
804837}
805838
839+ #[ cfg( feature = "alloc" ) ]
806840impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for LinkedList < A > {
807841 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
808842 u. arbitrary_iter ( ) ?. collect ( )
@@ -818,6 +852,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
818852 }
819853}
820854
855+ #[ cfg( feature = "alloc" ) ]
821856impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for VecDeque < A > {
822857 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
823858 u. arbitrary_iter ( ) ?. collect ( )
@@ -833,6 +868,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
833868 }
834869}
835870
871+ #[ cfg( feature = "alloc" ) ]
836872impl < ' a , A > Arbitrary < ' a > for Cow < ' a , A >
837873where
838874 A : ToOwned + ?Sized ,
@@ -885,6 +921,7 @@ impl<'a> Arbitrary<'a> for &'a str {
885921 }
886922}
887923
924+ #[ cfg( feature = "alloc" ) ]
888925impl < ' a > Arbitrary < ' a > for String {
889926 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
890927 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -900,6 +937,7 @@ impl<'a> Arbitrary<'a> for String {
900937 }
901938}
902939
940+ #[ cfg( feature = "alloc" ) ]
903941impl < ' a > Arbitrary < ' a > for CString {
904942 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
905943 <Vec < u8 > as Arbitrary >:: arbitrary ( u) . map ( |mut x| {
@@ -915,6 +953,7 @@ impl<'a> Arbitrary<'a> for CString {
915953 }
916954}
917955
956+ #[ cfg( feature = "std" ) ]
918957impl < ' a > Arbitrary < ' a > for OsString {
919958 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
920959 <String as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -926,6 +965,7 @@ impl<'a> Arbitrary<'a> for OsString {
926965 }
927966}
928967
968+ #[ cfg( feature = "std" ) ]
929969impl < ' a > Arbitrary < ' a > for PathBuf {
930970 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
931971 <OsString as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -937,6 +977,7 @@ impl<'a> Arbitrary<'a> for PathBuf {
937977 }
938978}
939979
980+ #[ cfg( feature = "alloc" ) ]
940981impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < A > {
941982 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
942983 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -948,6 +989,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
948989 }
949990}
950991
992+ #[ cfg( feature = "alloc" ) ]
951993impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < [ A ] > {
952994 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
953995 u. arbitrary_iter ( ) ?. collect ( )
@@ -963,6 +1005,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
9631005 }
9641006}
9651007
1008+ #[ cfg( feature = "alloc" ) ]
9661009impl < ' a > Arbitrary < ' a > for Box < str > {
9671010 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
9681011 <String as Arbitrary >:: arbitrary ( u) . map ( |x| x. into_boxed_str ( ) )
@@ -987,6 +1030,7 @@ impl<'a> Arbitrary<'a> for Box<str> {
9871030// }
9881031// }
9891032
1033+ #[ cfg( feature = "alloc" ) ]
9901034impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < A > {
9911035 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
9921036 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -998,6 +1042,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
9981042 }
9991043}
10001044
1045+ #[ cfg( feature = "alloc" ) ]
10011046impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < [ A ] > {
10021047 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10031048 u. arbitrary_iter ( ) ?. collect ( )
@@ -1013,6 +1058,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> {
10131058 }
10141059}
10151060
1061+ #[ cfg( feature = "alloc" ) ]
10161062impl < ' a > Arbitrary < ' a > for Arc < str > {
10171063 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10181064 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1024,6 +1070,7 @@ impl<'a> Arbitrary<'a> for Arc<str> {
10241070 }
10251071}
10261072
1073+ #[ cfg( feature = "alloc" ) ]
10271074impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < A > {
10281075 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10291076 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1035,6 +1082,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
10351082 }
10361083}
10371084
1085+ #[ cfg( feature = "alloc" ) ]
10381086impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < [ A ] > {
10391087 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10401088 u. arbitrary_iter ( ) ?. collect ( )
@@ -1050,6 +1098,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> {
10501098 }
10511099}
10521100
1101+ #[ cfg( feature = "alloc" ) ]
10531102impl < ' a > Arbitrary < ' a > for Rc < str > {
10541103 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10551104 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1094,6 +1143,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
10941143 }
10951144}
10961145
1146+ #[ cfg( feature = "std" ) ]
10971147impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Mutex < A > {
10981148 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10991149 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1180,6 +1230,7 @@ implement_nonzero_int! { NonZeroU64, u64 }
11801230implement_nonzero_int ! { NonZeroU128 , u128 }
11811231implement_nonzero_int ! { NonZeroUsize , usize }
11821232
1233+ #[ cfg( feature = "std" ) ]
11831234impl < ' a > Arbitrary < ' a > for Ipv4Addr {
11841235 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
11851236 Ok ( Ipv4Addr :: from ( u32:: arbitrary ( u) ?) )
@@ -1191,6 +1242,7 @@ impl<'a> Arbitrary<'a> for Ipv4Addr {
11911242 }
11921243}
11931244
1245+ #[ cfg( feature = "std" ) ]
11941246impl < ' a > Arbitrary < ' a > for Ipv6Addr {
11951247 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
11961248 Ok ( Ipv6Addr :: from ( u128:: arbitrary ( u) ?) )
@@ -1202,6 +1254,7 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
12021254 }
12031255}
12041256
1257+ #[ cfg( feature = "std" ) ]
12051258impl < ' a > Arbitrary < ' a > for IpAddr {
12061259 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12071260 if u. arbitrary ( ) ? {
@@ -1219,6 +1272,7 @@ impl<'a> Arbitrary<'a> for IpAddr {
12191272 }
12201273}
12211274
1275+ #[ cfg( feature = "std" ) ]
12221276impl < ' a > Arbitrary < ' a > for SocketAddrV4 {
12231277 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12241278 Ok ( SocketAddrV4 :: new ( u. arbitrary ( ) ?, u. arbitrary ( ) ?) )
@@ -1230,6 +1284,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV4 {
12301284 }
12311285}
12321286
1287+ #[ cfg( feature = "std" ) ]
12331288impl < ' a > Arbitrary < ' a > for SocketAddrV6 {
12341289 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12351290 Ok ( SocketAddrV6 :: new (
@@ -1252,6 +1307,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV6 {
12521307 }
12531308}
12541309
1310+ #[ cfg( feature = "std" ) ]
12551311impl < ' a > Arbitrary < ' a > for SocketAddr {
12561312 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12571313 if u. arbitrary ( ) ? {
@@ -1275,6 +1331,7 @@ impl<'a> Arbitrary<'a> for SocketAddr {
12751331#[ cfg( test) ]
12761332mod test {
12771333 use super :: * ;
1334+ use std:: collections:: HashSet ;
12781335
12791336 /// Assert that the given expected values are all generated.
12801337 ///
@@ -1427,6 +1484,7 @@ mod test {
14271484 }
14281485
14291486 #[ test]
1487+ #[ cfg( feature = "alloc" ) ]
14301488 fn arbitrary_for_vec_u8 ( ) {
14311489 assert_generates :: < Vec < u8 > > ( [
14321490 vec ! [ ] ,
@@ -1448,6 +1506,7 @@ mod test {
14481506 }
14491507
14501508 #[ test]
1509+ #[ cfg( feature = "alloc" ) ]
14511510 fn arbitrary_for_vec_vec_u8 ( ) {
14521511 assert_generates :: < Vec < Vec < u8 > > > ( [
14531512 vec ! [ ] ,
@@ -1466,6 +1525,7 @@ mod test {
14661525 }
14671526
14681527 #[ test]
1528+ #[ cfg( feature = "alloc" ) ]
14691529 fn arbitrary_for_vec_vec_vec_u8 ( ) {
14701530 assert_generates :: < Vec < Vec < Vec < u8 > > > > ( [
14711531 vec ! [ ] ,
@@ -1490,11 +1550,13 @@ mod test {
14901550 }
14911551
14921552 #[ test]
1553+ #[ cfg( feature = "alloc" ) ]
14931554 fn arbitrary_for_string ( ) {
14941555 assert_generates :: < String > ( [ "" . into ( ) , "a" . into ( ) , "aa" . into ( ) , "aaa" . into ( ) ] ) ;
14951556 }
14961557
14971558 #[ test]
1559+ #[ cfg( feature = "alloc" ) ]
14981560 fn arbitrary_collection ( ) {
14991561 let x = [
15001562 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 12 ,
@@ -1530,6 +1592,7 @@ mod test {
15301592 }
15311593
15321594 #[ test]
1595+ #[ cfg( feature = "alloc" ) ]
15331596 fn arbitrary_take_rest ( ) {
15341597 // Basic examples
15351598 let x = [ 1 , 2 , 3 , 4 ] ;
@@ -1580,6 +1643,7 @@ mod test {
15801643 }
15811644
15821645 #[ test]
1646+ #[ cfg( feature = "alloc" ) ]
15831647 fn size_hint_for_tuples ( ) {
15841648 assert_eq ! (
15851649 ( 7 , Some ( 7 ) ) ,
0 commit comments