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,42 @@ 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 ;
53+ #[ cfg( all( not( feature = "std" ) , feature = "alloc" ) ) ]
54+ use alloc:: borrow:: ToOwned ;
55+ #[ cfg( all( not( feature = "std" ) , feature = "alloc" ) ) ]
56+ use alloc:: boxed:: Box ;
57+ #[ cfg( feature = "alloc" ) ]
58+ use alloc:: collections:: { BTreeMap , BTreeSet , BinaryHeap , LinkedList , VecDeque } ;
59+ #[ cfg( feature = "alloc" ) ]
60+ use alloc:: ffi:: CString ;
61+ #[ cfg( feature = "alloc" ) ]
62+ use alloc:: rc:: Rc ;
63+ #[ cfg( all( not( feature = "std" ) , feature = "alloc" ) ) ]
64+ use alloc:: string:: String ;
65+ #[ cfg( feature = "alloc" ) ]
66+ use alloc:: sync:: Arc ;
67+ #[ cfg( all( not( feature = "std" ) , feature = "alloc" ) ) ]
68+ use alloc:: vec:: Vec ;
4669use core:: ops:: Bound ;
4770use 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 } ;
71+ #[ cfg( feature = "std" ) ]
72+ use std:: collections:: { HashMap , HashSet } ;
73+ #[ cfg( feature = "std" ) ]
74+ use std:: ffi:: OsString ;
75+ #[ cfg( feature = "std" ) ]
5176use std:: hash:: BuildHasher ;
77+ #[ cfg( feature = "std" ) ]
5278use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr , SocketAddrV4 , SocketAddrV6 } ;
79+ #[ cfg( feature = "std" ) ]
5380use std:: path:: PathBuf ;
54- use std:: rc :: Rc ;
55- use std:: sync:: { Arc , Mutex } ;
81+ # [ cfg ( feature = " std" ) ]
82+ use std:: sync:: Mutex ;
5683
5784/// Generate arbitrary structured values from raw, unstructured data.
5885///
@@ -81,11 +108,13 @@ use std::sync::{Arc, Mutex};
81108/// use arbitrary::Arbitrary;
82109/// use std::collections::HashSet;
83110///
111+ /// # #[cfg(feature = "std")]
84112/// #[derive(Arbitrary)]
85113/// pub struct AddressBook {
86114/// friends: HashSet<Friend>,
87115/// }
88116///
117+ /// # #[cfg(feature = "alloc")]
89118/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
90119/// pub enum Friend {
91120/// Buddy { name: String },
@@ -690,6 +719,7 @@ impl<'a> Arbitrary<'a> for &'a [u8] {
690719 }
691720}
692721
722+ #[ cfg( feature = "alloc" ) ]
693723impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Vec < A > {
694724 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
695725 u. arbitrary_iter ( ) ?. collect ( )
@@ -705,6 +735,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
705735 }
706736}
707737
738+ #[ cfg( feature = "alloc" ) ]
708739impl < ' a , K : Arbitrary < ' a > + Ord , V : Arbitrary < ' a > > Arbitrary < ' a > for BTreeMap < K , V > {
709740 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
710741 u. arbitrary_iter ( ) ?. collect ( )
@@ -720,6 +751,7 @@ impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K,
720751 }
721752}
722753
754+ #[ cfg( feature = "alloc" ) ]
723755impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BTreeSet < A > {
724756 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
725757 u. arbitrary_iter ( ) ?. collect ( )
@@ -754,6 +786,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> {
754786 }
755787}
756788
789+ #[ cfg( feature = "alloc" ) ]
757790impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BinaryHeap < A > {
758791 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
759792 u. arbitrary_iter ( ) ?. collect ( )
@@ -769,7 +802,8 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
769802 }
770803}
771804
772- impl < ' a , K : Arbitrary < ' a > + Eq + :: std:: hash:: Hash , V : Arbitrary < ' a > , S : BuildHasher + Default >
805+ #[ cfg( feature = "std" ) ]
806+ impl < ' a , K : Arbitrary < ' a > + Eq + core:: hash:: Hash , V : Arbitrary < ' a > , S : BuildHasher + Default >
773807 Arbitrary < ' a > for HashMap < K , V , S >
774808{
775809 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
@@ -786,7 +820,8 @@ impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>, S: BuildHa
786820 }
787821}
788822
789- impl < ' a , A : Arbitrary < ' a > + Eq + :: std:: hash:: Hash , S : BuildHasher + Default > Arbitrary < ' a >
823+ #[ cfg( feature = "std" ) ]
824+ impl < ' a , A : Arbitrary < ' a > + Eq + core:: hash:: Hash , S : BuildHasher + Default > Arbitrary < ' a >
790825 for HashSet < A , S >
791826{
792827 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
@@ -803,6 +838,7 @@ impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash, S: BuildHasher + Default> Ar
803838 }
804839}
805840
841+ #[ cfg( feature = "alloc" ) ]
806842impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for LinkedList < A > {
807843 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
808844 u. arbitrary_iter ( ) ?. collect ( )
@@ -818,6 +854,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
818854 }
819855}
820856
857+ #[ cfg( feature = "alloc" ) ]
821858impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for VecDeque < A > {
822859 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
823860 u. arbitrary_iter ( ) ?. collect ( )
@@ -833,6 +870,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
833870 }
834871}
835872
873+ #[ cfg( feature = "alloc" ) ]
836874impl < ' a , A > Arbitrary < ' a > for Cow < ' a , A >
837875where
838876 A : ToOwned + ?Sized ,
@@ -885,6 +923,7 @@ impl<'a> Arbitrary<'a> for &'a str {
885923 }
886924}
887925
926+ #[ cfg( feature = "alloc" ) ]
888927impl < ' a > Arbitrary < ' a > for String {
889928 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
890929 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -900,6 +939,7 @@ impl<'a> Arbitrary<'a> for String {
900939 }
901940}
902941
942+ #[ cfg( feature = "alloc" ) ]
903943impl < ' a > Arbitrary < ' a > for CString {
904944 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
905945 <Vec < u8 > as Arbitrary >:: arbitrary ( u) . map ( |mut x| {
@@ -915,6 +955,7 @@ impl<'a> Arbitrary<'a> for CString {
915955 }
916956}
917957
958+ #[ cfg( feature = "std" ) ]
918959impl < ' a > Arbitrary < ' a > for OsString {
919960 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
920961 <String as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -926,6 +967,7 @@ impl<'a> Arbitrary<'a> for OsString {
926967 }
927968}
928969
970+ #[ cfg( feature = "std" ) ]
929971impl < ' a > Arbitrary < ' a > for PathBuf {
930972 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
931973 <OsString as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -937,6 +979,7 @@ impl<'a> Arbitrary<'a> for PathBuf {
937979 }
938980}
939981
982+ #[ cfg( feature = "alloc" ) ]
940983impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < A > {
941984 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
942985 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -948,6 +991,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
948991 }
949992}
950993
994+ #[ cfg( feature = "alloc" ) ]
951995impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < [ A ] > {
952996 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
953997 u. arbitrary_iter ( ) ?. collect ( )
@@ -963,6 +1007,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
9631007 }
9641008}
9651009
1010+ #[ cfg( feature = "alloc" ) ]
9661011impl < ' a > Arbitrary < ' a > for Box < str > {
9671012 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
9681013 <String as Arbitrary >:: arbitrary ( u) . map ( |x| x. into_boxed_str ( ) )
@@ -987,6 +1032,7 @@ impl<'a> Arbitrary<'a> for Box<str> {
9871032// }
9881033// }
9891034
1035+ #[ cfg( feature = "alloc" ) ]
9901036impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < A > {
9911037 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
9921038 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -998,6 +1044,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
9981044 }
9991045}
10001046
1047+ #[ cfg( feature = "alloc" ) ]
10011048impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < [ A ] > {
10021049 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10031050 u. arbitrary_iter ( ) ?. collect ( )
@@ -1013,6 +1060,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> {
10131060 }
10141061}
10151062
1063+ #[ cfg( feature = "alloc" ) ]
10161064impl < ' a > Arbitrary < ' a > for Arc < str > {
10171065 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10181066 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1024,6 +1072,7 @@ impl<'a> Arbitrary<'a> for Arc<str> {
10241072 }
10251073}
10261074
1075+ #[ cfg( feature = "alloc" ) ]
10271076impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < A > {
10281077 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10291078 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1035,6 +1084,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
10351084 }
10361085}
10371086
1087+ #[ cfg( feature = "alloc" ) ]
10381088impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < [ A ] > {
10391089 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10401090 u. arbitrary_iter ( ) ?. collect ( )
@@ -1050,6 +1100,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> {
10501100 }
10511101}
10521102
1103+ #[ cfg( feature = "alloc" ) ]
10531104impl < ' a > Arbitrary < ' a > for Rc < str > {
10541105 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10551106 <& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1094,6 +1145,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
10941145 }
10951146}
10961147
1148+ #[ cfg( feature = "std" ) ]
10971149impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Mutex < A > {
10981150 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
10991151 Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1180,6 +1232,7 @@ implement_nonzero_int! { NonZeroU64, u64 }
11801232implement_nonzero_int ! { NonZeroU128 , u128 }
11811233implement_nonzero_int ! { NonZeroUsize , usize }
11821234
1235+ #[ cfg( feature = "std" ) ]
11831236impl < ' a > Arbitrary < ' a > for Ipv4Addr {
11841237 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
11851238 Ok ( Ipv4Addr :: from ( u32:: arbitrary ( u) ?) )
@@ -1191,6 +1244,7 @@ impl<'a> Arbitrary<'a> for Ipv4Addr {
11911244 }
11921245}
11931246
1247+ #[ cfg( feature = "std" ) ]
11941248impl < ' a > Arbitrary < ' a > for Ipv6Addr {
11951249 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
11961250 Ok ( Ipv6Addr :: from ( u128:: arbitrary ( u) ?) )
@@ -1202,6 +1256,7 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
12021256 }
12031257}
12041258
1259+ #[ cfg( feature = "std" ) ]
12051260impl < ' a > Arbitrary < ' a > for IpAddr {
12061261 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12071262 if u. arbitrary ( ) ? {
@@ -1219,6 +1274,7 @@ impl<'a> Arbitrary<'a> for IpAddr {
12191274 }
12201275}
12211276
1277+ #[ cfg( feature = "std" ) ]
12221278impl < ' a > Arbitrary < ' a > for SocketAddrV4 {
12231279 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12241280 Ok ( SocketAddrV4 :: new ( u. arbitrary ( ) ?, u. arbitrary ( ) ?) )
@@ -1230,6 +1286,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV4 {
12301286 }
12311287}
12321288
1289+ #[ cfg( feature = "std" ) ]
12331290impl < ' a > Arbitrary < ' a > for SocketAddrV6 {
12341291 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12351292 Ok ( SocketAddrV6 :: new (
@@ -1252,6 +1309,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV6 {
12521309 }
12531310}
12541311
1312+ #[ cfg( feature = "std" ) ]
12551313impl < ' a > Arbitrary < ' a > for SocketAddr {
12561314 fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
12571315 if u. arbitrary ( ) ? {
@@ -1275,6 +1333,7 @@ impl<'a> Arbitrary<'a> for SocketAddr {
12751333#[ cfg( test) ]
12761334mod test {
12771335 use super :: * ;
1336+ use std:: collections:: HashSet ;
12781337
12791338 /// Assert that the given expected values are all generated.
12801339 ///
@@ -1427,6 +1486,7 @@ mod test {
14271486 }
14281487
14291488 #[ test]
1489+ #[ cfg( feature = "alloc" ) ]
14301490 fn arbitrary_for_vec_u8 ( ) {
14311491 assert_generates :: < Vec < u8 > > ( [
14321492 vec ! [ ] ,
@@ -1448,6 +1508,7 @@ mod test {
14481508 }
14491509
14501510 #[ test]
1511+ #[ cfg( feature = "alloc" ) ]
14511512 fn arbitrary_for_vec_vec_u8 ( ) {
14521513 assert_generates :: < Vec < Vec < u8 > > > ( [
14531514 vec ! [ ] ,
@@ -1466,6 +1527,7 @@ mod test {
14661527 }
14671528
14681529 #[ test]
1530+ #[ cfg( feature = "alloc" ) ]
14691531 fn arbitrary_for_vec_vec_vec_u8 ( ) {
14701532 assert_generates :: < Vec < Vec < Vec < u8 > > > > ( [
14711533 vec ! [ ] ,
@@ -1490,11 +1552,13 @@ mod test {
14901552 }
14911553
14921554 #[ test]
1555+ #[ cfg( feature = "alloc" ) ]
14931556 fn arbitrary_for_string ( ) {
14941557 assert_generates :: < String > ( [ "" . into ( ) , "a" . into ( ) , "aa" . into ( ) , "aaa" . into ( ) ] ) ;
14951558 }
14961559
14971560 #[ test]
1561+ #[ cfg( feature = "alloc" ) ]
14981562 fn arbitrary_collection ( ) {
14991563 let x = [
15001564 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 +1594,7 @@ mod test {
15301594 }
15311595
15321596 #[ test]
1597+ #[ cfg( feature = "alloc" ) ]
15331598 fn arbitrary_take_rest ( ) {
15341599 // Basic examples
15351600 let x = [ 1 , 2 , 3 , 4 ] ;
@@ -1580,6 +1645,7 @@ mod test {
15801645 }
15811646
15821647 #[ test]
1648+ #[ cfg( feature = "alloc" ) ]
15831649 fn size_hint_for_tuples ( ) {
15841650 assert_eq ! (
15851651 ( 7 , Some ( 7 ) ) ,
0 commit comments