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,40 @@ 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 , 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 ;
46
67
use core:: ops:: Bound ;
47
68
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 } ;
69
+ #[ cfg( feature = "std" ) ]
70
+ use std:: collections:: { HashMap , HashSet } ;
71
+ #[ cfg( feature = "std" ) ]
72
+ use std:: ffi:: OsString ;
73
+ #[ cfg( feature = "std" ) ]
51
74
use std:: hash:: BuildHasher ;
75
+ #[ cfg( feature = "std" ) ]
52
76
use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr , SocketAddrV4 , SocketAddrV6 } ;
77
+ #[ cfg( feature = "std" ) ]
53
78
use std:: path:: PathBuf ;
54
- use std:: rc :: Rc ;
55
- use std:: sync:: { Arc , Mutex } ;
79
+ # [ cfg ( feature = " std" ) ]
80
+ use std:: sync:: Mutex ;
56
81
57
82
/// Generate arbitrary structured values from raw, unstructured data.
58
83
///
@@ -81,11 +106,13 @@ use std::sync::{Arc, Mutex};
81
106
/// use arbitrary::Arbitrary;
82
107
/// use std::collections::HashSet;
83
108
///
109
+ /// # #[cfg(feature = "std")]
84
110
/// #[derive(Arbitrary)]
85
111
/// pub struct AddressBook {
86
112
/// friends: HashSet<Friend>,
87
113
/// }
88
114
///
115
+ /// # #[cfg(feature = "alloc")]
89
116
/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
90
117
/// pub enum Friend {
91
118
/// Buddy { name: String },
@@ -690,6 +717,7 @@ impl<'a> Arbitrary<'a> for &'a [u8] {
690
717
}
691
718
}
692
719
720
+ #[ cfg( feature = "alloc" ) ]
693
721
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Vec < A > {
694
722
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
695
723
u. arbitrary_iter ( ) ?. collect ( )
@@ -705,6 +733,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
705
733
}
706
734
}
707
735
736
+ #[ cfg( feature = "alloc" ) ]
708
737
impl < ' a , K : Arbitrary < ' a > + Ord , V : Arbitrary < ' a > > Arbitrary < ' a > for BTreeMap < K , V > {
709
738
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
710
739
u. arbitrary_iter ( ) ?. collect ( )
@@ -720,6 +749,7 @@ impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K,
720
749
}
721
750
}
722
751
752
+ #[ cfg( feature = "alloc" ) ]
723
753
impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BTreeSet < A > {
724
754
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
725
755
u. arbitrary_iter ( ) ?. collect ( )
@@ -754,6 +784,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> {
754
784
}
755
785
}
756
786
787
+ #[ cfg( feature = "alloc" ) ]
757
788
impl < ' a , A : Arbitrary < ' a > + Ord > Arbitrary < ' a > for BinaryHeap < A > {
758
789
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
759
790
u. arbitrary_iter ( ) ?. collect ( )
@@ -769,7 +800,8 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
769
800
}
770
801
}
771
802
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 >
773
805
Arbitrary < ' a > for HashMap < K , V , S >
774
806
{
775
807
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
786
818
}
787
819
}
788
820
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 >
790
823
for HashSet < A , S >
791
824
{
792
825
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
803
836
}
804
837
}
805
838
839
+ #[ cfg( feature = "alloc" ) ]
806
840
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for LinkedList < A > {
807
841
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
808
842
u. arbitrary_iter ( ) ?. collect ( )
@@ -818,6 +852,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
818
852
}
819
853
}
820
854
855
+ #[ cfg( feature = "alloc" ) ]
821
856
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for VecDeque < A > {
822
857
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
823
858
u. arbitrary_iter ( ) ?. collect ( )
@@ -833,6 +868,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
833
868
}
834
869
}
835
870
871
+ #[ cfg( feature = "alloc" ) ]
836
872
impl < ' a , A > Arbitrary < ' a > for Cow < ' a , A >
837
873
where
838
874
A : ToOwned + ?Sized ,
@@ -885,6 +921,7 @@ impl<'a> Arbitrary<'a> for &'a str {
885
921
}
886
922
}
887
923
924
+ #[ cfg( feature = "alloc" ) ]
888
925
impl < ' a > Arbitrary < ' a > for String {
889
926
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
890
927
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -900,6 +937,7 @@ impl<'a> Arbitrary<'a> for String {
900
937
}
901
938
}
902
939
940
+ #[ cfg( feature = "alloc" ) ]
903
941
impl < ' a > Arbitrary < ' a > for CString {
904
942
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
905
943
<Vec < u8 > as Arbitrary >:: arbitrary ( u) . map ( |mut x| {
@@ -915,6 +953,7 @@ impl<'a> Arbitrary<'a> for CString {
915
953
}
916
954
}
917
955
956
+ #[ cfg( feature = "std" ) ]
918
957
impl < ' a > Arbitrary < ' a > for OsString {
919
958
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
920
959
<String as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -926,6 +965,7 @@ impl<'a> Arbitrary<'a> for OsString {
926
965
}
927
966
}
928
967
968
+ #[ cfg( feature = "std" ) ]
929
969
impl < ' a > Arbitrary < ' a > for PathBuf {
930
970
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
931
971
<OsString as Arbitrary >:: arbitrary ( u) . map ( From :: from)
@@ -937,6 +977,7 @@ impl<'a> Arbitrary<'a> for PathBuf {
937
977
}
938
978
}
939
979
980
+ #[ cfg( feature = "alloc" ) ]
940
981
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < A > {
941
982
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
942
983
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -948,6 +989,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
948
989
}
949
990
}
950
991
992
+ #[ cfg( feature = "alloc" ) ]
951
993
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Box < [ A ] > {
952
994
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
953
995
u. arbitrary_iter ( ) ?. collect ( )
@@ -963,6 +1005,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
963
1005
}
964
1006
}
965
1007
1008
+ #[ cfg( feature = "alloc" ) ]
966
1009
impl < ' a > Arbitrary < ' a > for Box < str > {
967
1010
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
968
1011
<String as Arbitrary >:: arbitrary ( u) . map ( |x| x. into_boxed_str ( ) )
@@ -987,6 +1030,7 @@ impl<'a> Arbitrary<'a> for Box<str> {
987
1030
// }
988
1031
// }
989
1032
1033
+ #[ cfg( feature = "alloc" ) ]
990
1034
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < A > {
991
1035
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
992
1036
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -998,6 +1042,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
998
1042
}
999
1043
}
1000
1044
1045
+ #[ cfg( feature = "alloc" ) ]
1001
1046
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Arc < [ A ] > {
1002
1047
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1003
1048
u. arbitrary_iter ( ) ?. collect ( )
@@ -1013,6 +1058,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> {
1013
1058
}
1014
1059
}
1015
1060
1061
+ #[ cfg( feature = "alloc" ) ]
1016
1062
impl < ' a > Arbitrary < ' a > for Arc < str > {
1017
1063
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1018
1064
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1024,6 +1070,7 @@ impl<'a> Arbitrary<'a> for Arc<str> {
1024
1070
}
1025
1071
}
1026
1072
1073
+ #[ cfg( feature = "alloc" ) ]
1027
1074
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < A > {
1028
1075
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1029
1076
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1035,6 +1082,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
1035
1082
}
1036
1083
}
1037
1084
1085
+ #[ cfg( feature = "alloc" ) ]
1038
1086
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Rc < [ A ] > {
1039
1087
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1040
1088
u. arbitrary_iter ( ) ?. collect ( )
@@ -1050,6 +1098,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> {
1050
1098
}
1051
1099
}
1052
1100
1101
+ #[ cfg( feature = "alloc" ) ]
1053
1102
impl < ' a > Arbitrary < ' a > for Rc < str > {
1054
1103
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1055
1104
<& str as Arbitrary >:: arbitrary ( u) . map ( Into :: into)
@@ -1094,6 +1143,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
1094
1143
}
1095
1144
}
1096
1145
1146
+ #[ cfg( feature = "std" ) ]
1097
1147
impl < ' a , A : Arbitrary < ' a > > Arbitrary < ' a > for Mutex < A > {
1098
1148
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1099
1149
Arbitrary :: arbitrary ( u) . map ( Self :: new)
@@ -1180,6 +1230,7 @@ implement_nonzero_int! { NonZeroU64, u64 }
1180
1230
implement_nonzero_int ! { NonZeroU128 , u128 }
1181
1231
implement_nonzero_int ! { NonZeroUsize , usize }
1182
1232
1233
+ #[ cfg( feature = "std" ) ]
1183
1234
impl < ' a > Arbitrary < ' a > for Ipv4Addr {
1184
1235
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1185
1236
Ok ( Ipv4Addr :: from ( u32:: arbitrary ( u) ?) )
@@ -1191,6 +1242,7 @@ impl<'a> Arbitrary<'a> for Ipv4Addr {
1191
1242
}
1192
1243
}
1193
1244
1245
+ #[ cfg( feature = "std" ) ]
1194
1246
impl < ' a > Arbitrary < ' a > for Ipv6Addr {
1195
1247
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1196
1248
Ok ( Ipv6Addr :: from ( u128:: arbitrary ( u) ?) )
@@ -1202,6 +1254,7 @@ impl<'a> Arbitrary<'a> for Ipv6Addr {
1202
1254
}
1203
1255
}
1204
1256
1257
+ #[ cfg( feature = "std" ) ]
1205
1258
impl < ' a > Arbitrary < ' a > for IpAddr {
1206
1259
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1207
1260
if u. arbitrary ( ) ? {
@@ -1219,6 +1272,7 @@ impl<'a> Arbitrary<'a> for IpAddr {
1219
1272
}
1220
1273
}
1221
1274
1275
+ #[ cfg( feature = "std" ) ]
1222
1276
impl < ' a > Arbitrary < ' a > for SocketAddrV4 {
1223
1277
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1224
1278
Ok ( SocketAddrV4 :: new ( u. arbitrary ( ) ?, u. arbitrary ( ) ?) )
@@ -1230,6 +1284,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV4 {
1230
1284
}
1231
1285
}
1232
1286
1287
+ #[ cfg( feature = "std" ) ]
1233
1288
impl < ' a > Arbitrary < ' a > for SocketAddrV6 {
1234
1289
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1235
1290
Ok ( SocketAddrV6 :: new (
@@ -1252,6 +1307,7 @@ impl<'a> Arbitrary<'a> for SocketAddrV6 {
1252
1307
}
1253
1308
}
1254
1309
1310
+ #[ cfg( feature = "std" ) ]
1255
1311
impl < ' a > Arbitrary < ' a > for SocketAddr {
1256
1312
fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
1257
1313
if u. arbitrary ( ) ? {
@@ -1275,6 +1331,7 @@ impl<'a> Arbitrary<'a> for SocketAddr {
1275
1331
#[ cfg( test) ]
1276
1332
mod test {
1277
1333
use super :: * ;
1334
+ use std:: collections:: HashSet ;
1278
1335
1279
1336
/// Assert that the given expected values are all generated.
1280
1337
///
@@ -1427,6 +1484,7 @@ mod test {
1427
1484
}
1428
1485
1429
1486
#[ test]
1487
+ #[ cfg( feature = "alloc" ) ]
1430
1488
fn arbitrary_for_vec_u8 ( ) {
1431
1489
assert_generates :: < Vec < u8 > > ( [
1432
1490
vec ! [ ] ,
@@ -1448,6 +1506,7 @@ mod test {
1448
1506
}
1449
1507
1450
1508
#[ test]
1509
+ #[ cfg( feature = "alloc" ) ]
1451
1510
fn arbitrary_for_vec_vec_u8 ( ) {
1452
1511
assert_generates :: < Vec < Vec < u8 > > > ( [
1453
1512
vec ! [ ] ,
@@ -1466,6 +1525,7 @@ mod test {
1466
1525
}
1467
1526
1468
1527
#[ test]
1528
+ #[ cfg( feature = "alloc" ) ]
1469
1529
fn arbitrary_for_vec_vec_vec_u8 ( ) {
1470
1530
assert_generates :: < Vec < Vec < Vec < u8 > > > > ( [
1471
1531
vec ! [ ] ,
@@ -1490,11 +1550,13 @@ mod test {
1490
1550
}
1491
1551
1492
1552
#[ test]
1553
+ #[ cfg( feature = "alloc" ) ]
1493
1554
fn arbitrary_for_string ( ) {
1494
1555
assert_generates :: < String > ( [ "" . into ( ) , "a" . into ( ) , "aa" . into ( ) , "aaa" . into ( ) ] ) ;
1495
1556
}
1496
1557
1497
1558
#[ test]
1559
+ #[ cfg( feature = "alloc" ) ]
1498
1560
fn arbitrary_collection ( ) {
1499
1561
let x = [
1500
1562
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 {
1530
1592
}
1531
1593
1532
1594
#[ test]
1595
+ #[ cfg( feature = "alloc" ) ]
1533
1596
fn arbitrary_take_rest ( ) {
1534
1597
// Basic examples
1535
1598
let x = [ 1 , 2 , 3 , 4 ] ;
@@ -1580,6 +1643,7 @@ mod test {
1580
1643
}
1581
1644
1582
1645
#[ test]
1646
+ #[ cfg( feature = "alloc" ) ]
1583
1647
fn size_hint_for_tuples ( ) {
1584
1648
assert_eq ! (
1585
1649
( 7 , Some ( 7 ) ) ,
0 commit comments