14
14
//! [`Arbitrary`](./trait.Arbitrary.html) trait's documentation for details on
15
15
//! automatically deriving, implementing, and/or using the trait.
16
16
17
+ #![ cfg_attr( not( any( feature = "std" , test) ) , no_std) ]
17
18
#![ deny( bad_style) ]
18
19
#![ deny( missing_docs) ]
19
20
#![ deny( future_incompatible) ]
@@ -43,16 +44,42 @@ use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZ
43
44
use core:: ops:: { Range , RangeBounds , RangeFrom , RangeInclusive , RangeTo , RangeToInclusive } ;
44
45
use core:: str;
45
46
use 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 ;
46
69
use core:: ops:: Bound ;
47
70
use 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" ) ]
51
76
use std:: hash:: BuildHasher ;
77
+ #[ cfg( feature = "std" ) ]
52
78
use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr , SocketAddrV4 , SocketAddrV6 } ;
79
+ #[ cfg( feature = "std" ) ]
53
80
use std:: path:: PathBuf ;
54
- use std:: rc :: Rc ;
55
- use std:: sync:: { Arc , Mutex } ;
81
+ # [ cfg ( feature = " std" ) ]
82
+ use std:: sync:: Mutex ;
56
83
57
84
/// Generate arbitrary structured values from raw, unstructured data.
58
85
///
@@ -81,11 +108,13 @@ use std::sync::{Arc, Mutex};
81
108
/// use arbitrary::Arbitrary;
82
109
/// use std::collections::HashSet;
83
110
///
111
+ /// # #[cfg(feature = "std")]
84
112
/// #[derive(Arbitrary)]
85
113
/// pub struct AddressBook {
86
114
/// friends: HashSet<Friend>,
87
115
/// }
88
116
///
117
+ /// # #[cfg(feature = "alloc")]
89
118
/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
90
119
/// pub enum Friend {
91
120
/// Buddy { name: String },
@@ -690,6 +719,7 @@ impl<'a> Arbitrary<'a> for &'a [u8] {
690
719
}
691
720
}
692
721
722
+ #[ cfg( feature = "alloc" ) ]
693
723
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Vec < A > {
694
724
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
695
725
u. arbitrary_iter ( ) ?. collect ( )
@@ -705,6 +735,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
705
735
}
706
736
}
707
737
738
+ #[ cfg( feature = "alloc" ) ]
708
739
impl < ' a , K : Arbitrary < ' a > + Ord , V : Arbitrary < ' a > > Arbitrary < ' a > for BTreeMap < K , V > {
709
740
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
710
741
u. arbitrary_iter ( ) ?. collect ( )
@@ -720,6 +751,7 @@ impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K,
720
751
}
721
752
}
722
753
754
+ #[ cfg( feature = "alloc" ) ]
723
755
impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BTreeSet < A > {
724
756
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
725
757
u. arbitrary_iter ( ) ?. collect ( )
@@ -754,6 +786,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> {
754
786
}
755
787
}
756
788
789
+ #[ cfg( feature = "alloc" ) ]
757
790
impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BinaryHeap < A > {
758
791
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
759
792
u. arbitrary_iter ( ) ?. collect ( )
@@ -769,7 +802,8 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
769
802
}
770
803
}
771
804
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 >
773
807
Arbitrary < ' a > for HashMap < K , V , S >
774
808
{
775
809
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
786
820
}
787
821
}
788
822
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 >
790
825
for HashSet < A , S >
791
826
{
792
827
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
803
838
}
804
839
}
805
840
841
+ #[ cfg( feature = "alloc" ) ]
806
842
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for LinkedList < A > {
807
843
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
808
844
u. arbitrary_iter ( ) ?. collect ( )
@@ -818,6 +854,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
818
854
}
819
855
}
820
856
857
+ #[ cfg( feature = "alloc" ) ]
821
858
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for VecDeque < A > {
822
859
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
823
860
u. arbitrary_iter ( ) ?. collect ( )
@@ -833,6 +870,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
833
870
}
834
871
}
835
872
873
+ #[ cfg( feature = "alloc" ) ]
836
874
impl < ' a , A > Arbitrary < ' a > for Cow < ' a , A >
837
875
where
838
876
A : ToOwned + ?Sized ,
@@ -885,6 +923,7 @@ impl<'a> Arbitrary<'a> for &'a str {
885
923
}
886
924
}
887
925
926
+ #[ cfg( feature = "alloc" ) ]
888
927
impl < ' a > Arbitrary < ' a > for String {
889
928
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
890
929
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -900,6 +939,7 @@ impl<'a> Arbitrary<'a> for String {
900
939
}
901
940
}
902
941
942
+ #[ cfg( feature = "alloc" ) ]
903
943
impl < ' a > Arbitrary < ' a > for CString {
904
944
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
905
945
<Vec < u8 > as Arbitrary >:: arbitrary ( u) . map ( |mut x| {
@@ -915,6 +955,7 @@ impl<'a> Arbitrary<'a> for CString {
915
955
}
916
956
}
917
957
958
+ #[ cfg( feature = "std" ) ]
918
959
impl < ' a > Arbitrary < ' a > for OsString {
919
960
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
920
961
<String as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -926,6 +967,7 @@ impl<'a> Arbitrary<'a> for OsString {
926
967
}
927
968
}
928
969
970
+ #[ cfg( feature = "std" ) ]
929
971
impl < ' a > Arbitrary < ' a > for PathBuf {
930
972
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
931
973
<OsString as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -937,6 +979,7 @@ impl<'a> Arbitrary<'a> for PathBuf {
937
979
}
938
980
}
939
981
982
+ #[ cfg( feature = "alloc" ) ]
940
983
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < A > {
941
984
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
942
985
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -948,6 +991,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
948
991
}
949
992
}
950
993
994
+ #[ cfg( feature = "alloc" ) ]
951
995
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < [ A ] > {
952
996
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
953
997
u. arbitrary_iter ( ) ?. collect ( )
@@ -963,6 +1007,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
963
1007
}
964
1008
}
965
1009
1010
+ #[ cfg( feature = "alloc" ) ]
966
1011
impl < ' a > Arbitrary < ' a > for Box < str > {
967
1012
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
968
1013
<String as Arbitrary >:: arbitrary ( u) . map ( |x| x. into_boxed_str ( ) )
@@ -987,6 +1032,7 @@ impl<'a> Arbitrary<'a> for Box<str> {
987
1032
// }
988
1033
// }
989
1034
1035
+ #[ cfg( feature = "alloc" ) ]
990
1036
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < A > {
991
1037
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
992
1038
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -998,6 +1044,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
998
1044
}
999
1045
}
1000
1046
1047
+ #[ cfg( feature = "alloc" ) ]
1001
1048
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < [ A ] > {
1002
1049
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1003
1050
u. arbitrary_iter ( ) ?. collect ( )
@@ -1013,6 +1060,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> {
1013
1060
}
1014
1061
}
1015
1062
1063
+ #[ cfg( feature = "alloc" ) ]
1016
1064
impl < ' a > Arbitrary < ' a > for Arc < str > {
1017
1065
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1018
1066
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1024,6 +1072,7 @@ impl<'a> Arbitrary<'a> for Arc<str> {
1024
1072
}
1025
1073
}
1026
1074
1075
+ #[ cfg( feature = "alloc" ) ]
1027
1076
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < A > {
1028
1077
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1029
1078
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1035,6 +1084,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
1035
1084
}
1036
1085
}
1037
1086
1087
+ #[ cfg( feature = "alloc" ) ]
1038
1088
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < [ A ] > {
1039
1089
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1040
1090
u. arbitrary_iter ( ) ?. collect ( )
@@ -1050,6 +1100,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> {
1050
1100
}
1051
1101
}
1052
1102
1103
+ #[ cfg( feature = "alloc" ) ]
1053
1104
impl < ' a > Arbitrary < ' a > for Rc < str > {
1054
1105
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1055
1106
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1094,6 +1145,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
1094
1145
}
1095
1146
}
1096
1147
1148
+ #[ cfg( feature = "std" ) ]
1097
1149
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Mutex < A > {
1098
1150
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1099
1151
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1180,6 +1232,7 @@ implement_nonzero_int! { NonZeroU64, u64 }
1180
1232
implement_nonzero_int ! { NonZeroU128 , u128 }
1181
1233
implement_nonzero_int ! { NonZeroUsize , usize }
1182
1234
1235
+ #[ cfg( feature = "std" ) ]
1183
1236
impl < ' a > Arbitrary < ' a > for Ipv4Addr {
1184
1237
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1185
1238
Ok ( Ipv4Addr :: from ( u32:: arbitrary ( u) ?) )
@@ -1191,6 +1244,7 @@ impl<'a> Arbitrary<'a> for Ipv4Addr {
1191
1244
}
1192
1245
}
1193
1246
1247
+ #[ cfg( feature = "std" ) ]
1194
1248
impl < ' a > Arbitrary < ' a > for Ipv6Addr {
1195
1249
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1196
1250
Ok ( Ipv6Addr :: from ( u128:: arbitrary ( u) ?) )
@@ -1202,6 +1256,7 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
1202
1256
}
1203
1257
}
1204
1258
1259
+ #[ cfg( feature = "std" ) ]
1205
1260
impl < ' a > Arbitrary < ' a > for IpAddr {
1206
1261
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1207
1262
if u. arbitrary ( ) ? {
@@ -1219,6 +1274,7 @@ impl<'a> Arbitrary<'a> for IpAddr {
1219
1274
}
1220
1275
}
1221
1276
1277
+ #[ cfg( feature = "std" ) ]
1222
1278
impl < ' a > Arbitrary < ' a > for SocketAddrV4 {
1223
1279
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1224
1280
Ok ( SocketAddrV4 :: new ( u. arbitrary ( ) ?, u. arbitrary ( ) ?) )
@@ -1230,6 +1286,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV4 {
1230
1286
}
1231
1287
}
1232
1288
1289
+ #[ cfg( feature = "std" ) ]
1233
1290
impl < ' a > Arbitrary < ' a > for SocketAddrV6 {
1234
1291
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1235
1292
Ok ( SocketAddrV6 :: new (
@@ -1252,6 +1309,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV6 {
1252
1309
}
1253
1310
}
1254
1311
1312
+ #[ cfg( feature = "std" ) ]
1255
1313
impl < ' a > Arbitrary < ' a > for SocketAddr {
1256
1314
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1257
1315
if u. arbitrary ( ) ? {
@@ -1275,6 +1333,7 @@ impl<'a> Arbitrary<'a> for SocketAddr {
1275
1333
#[ cfg( test) ]
1276
1334
mod test {
1277
1335
use super :: * ;
1336
+ use std:: collections:: HashSet ;
1278
1337
1279
1338
/// Assert that the given expected values are all generated.
1280
1339
///
@@ -1427,6 +1486,7 @@ mod test {
1427
1486
}
1428
1487
1429
1488
#[ test]
1489
+ #[ cfg( feature = "alloc" ) ]
1430
1490
fn arbitrary_for_vec_u8 ( ) {
1431
1491
assert_generates :: < Vec < u8 > > ( [
1432
1492
vec ! [ ] ,
@@ -1448,6 +1508,7 @@ mod test {
1448
1508
}
1449
1509
1450
1510
#[ test]
1511
+ #[ cfg( feature = "alloc" ) ]
1451
1512
fn arbitrary_for_vec_vec_u8 ( ) {
1452
1513
assert_generates :: < Vec < Vec < u8 > > > ( [
1453
1514
vec ! [ ] ,
@@ -1466,6 +1527,7 @@ mod test {
1466
1527
}
1467
1528
1468
1529
#[ test]
1530
+ #[ cfg( feature = "alloc" ) ]
1469
1531
fn arbitrary_for_vec_vec_vec_u8 ( ) {
1470
1532
assert_generates :: < Vec < Vec < Vec < u8 > > > > ( [
1471
1533
vec ! [ ] ,
@@ -1490,11 +1552,13 @@ mod test {
1490
1552
}
1491
1553
1492
1554
#[ test]
1555
+ #[ cfg( feature = "alloc" ) ]
1493
1556
fn arbitrary_for_string ( ) {
1494
1557
assert_generates :: < String > ( [ "" . into ( ) , "a" . into ( ) , "aa" . into ( ) , "aaa" . into ( ) ] ) ;
1495
1558
}
1496
1559
1497
1560
#[ test]
1561
+ #[ cfg( feature = "alloc" ) ]
1498
1562
fn arbitrary_collection ( ) {
1499
1563
let x = [
1500
1564
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 {
1530
1594
}
1531
1595
1532
1596
#[ test]
1597
+ #[ cfg( feature = "alloc" ) ]
1533
1598
fn arbitrary_take_rest ( ) {
1534
1599
// Basic examples
1535
1600
let x = [ 1 , 2 , 3 , 4 ] ;
@@ -1580,6 +1645,7 @@ mod test {
1580
1645
}
1581
1646
1582
1647
#[ test]
1648
+ #[ cfg( feature = "alloc" ) ]
1583
1649
fn size_hint_for_tuples ( ) {
1584
1650
assert_eq ! (
1585
1651
( 7 , Some ( 7 ) ) ,
0 commit comments