@@ -2,11 +2,10 @@ use data_encoding::HEXUPPER;
2
2
use multihash:: Multihash ;
3
3
use multiaddr:: * ;
4
4
use quickcheck:: { Arbitrary , Gen , QuickCheck } ;
5
- use rand:: Rng ;
6
5
use std:: {
7
6
borrow:: Cow ,
8
- convert:: TryFrom ,
9
- iter:: FromIterator ,
7
+ convert:: { TryFrom , TryInto } ,
8
+ iter:: { FromIterator , self } ,
10
9
net:: { Ipv4Addr , Ipv6Addr } ,
11
10
str:: FromStr
12
11
} ;
@@ -87,8 +86,8 @@ struct Proto(Protocol<'static>);
87
86
impl Arbitrary for Proto {
88
87
fn arbitrary < G : Gen > ( g : & mut G ) -> Self {
89
88
use Protocol :: * ;
90
- match g . gen_range ( 0 , 25 ) { // TODO: Add Protocol::Quic
91
- 0 => Proto ( Dccp ( g . gen ( ) ) ) ,
89
+ match u8 :: arbitrary ( g ) % 25 { // TODO: Add Protocol::Quic
90
+ 0 => Proto ( Dccp ( Arbitrary :: arbitrary ( g ) ) ) ,
92
91
1 => Proto ( Dns ( Cow :: Owned ( SubString :: arbitrary ( g) . 0 ) ) ) ,
93
92
2 => Proto ( Dns4 ( Cow :: Owned ( SubString :: arbitrary ( g) . 0 ) ) ) ,
94
93
3 => Proto ( Dns6 ( Cow :: Owned ( SubString :: arbitrary ( g) . 0 ) ) ) ,
@@ -99,28 +98,35 @@ impl Arbitrary for Proto {
99
98
8 => Proto ( P2pWebRtcDirect ) ,
100
99
9 => Proto ( P2pWebRtcStar ) ,
101
100
10 => Proto ( P2pWebSocketStar ) ,
102
- 11 => Proto ( Memory ( g . gen ( ) ) ) ,
101
+ 11 => Proto ( Memory ( Arbitrary :: arbitrary ( g ) ) ) ,
103
102
// TODO: impl Arbitrary for Multihash:
104
103
12 => Proto ( P2p ( multihash ( "QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC" ) ) ) ,
105
104
13 => Proto ( P2pCircuit ) ,
106
105
14 => Proto ( Quic ) ,
107
- 15 => Proto ( Sctp ( g . gen ( ) ) ) ,
108
- 16 => Proto ( Tcp ( g . gen ( ) ) ) ,
109
- 17 => Proto ( Udp ( g . gen ( ) ) ) ,
106
+ 15 => Proto ( Sctp ( Arbitrary :: arbitrary ( g ) ) ) ,
107
+ 16 => Proto ( Tcp ( Arbitrary :: arbitrary ( g ) ) ) ,
108
+ 17 => Proto ( Udp ( Arbitrary :: arbitrary ( g ) ) ) ,
110
109
18 => Proto ( Udt ) ,
111
110
19 => Proto ( Unix ( Cow :: Owned ( SubString :: arbitrary ( g) . 0 ) ) ) ,
112
111
20 => Proto ( Utp ) ,
113
112
21 => Proto ( Ws ( "/" . into ( ) ) ) ,
114
113
22 => Proto ( Wss ( "/" . into ( ) ) ) ,
115
114
23 => {
116
- let mut a = [ 0 ; 10 ] ;
117
- g. fill ( & mut a) ;
118
- Proto ( Onion ( Cow :: Owned ( a) , g. gen_range ( 1 , std:: u16:: MAX ) ) )
115
+
116
+ let a = iter:: repeat_with ( || u8:: arbitrary ( g) )
117
+ . take ( 10 )
118
+ . collect :: < Vec < _ > > ( )
119
+ . try_into ( )
120
+ . unwrap ( ) ;
121
+ Proto ( Onion ( Cow :: Owned ( a) , std:: cmp:: max ( 1 , u16:: arbitrary ( g) ) ) )
119
122
} ,
120
123
24 => {
121
- let mut a = [ 0 ; 35 ] ;
122
- g. fill_bytes ( & mut a) ;
123
- Proto ( Onion3 ( ( a, g. gen_range ( 1 , std:: u16:: MAX ) ) . into ( ) ) )
124
+ let a: [ u8 ; 35 ] = iter:: repeat_with ( || u8:: arbitrary ( g) )
125
+ . take ( 35 )
126
+ . collect :: < Vec < _ > > ( )
127
+ . try_into ( )
128
+ . unwrap ( ) ;
129
+ Proto ( Onion3 ( ( a, std:: cmp:: max ( 1 , u16:: arbitrary ( g) ) ) . into ( ) ) )
124
130
} ,
125
131
_ => panic ! ( "outside range" )
126
132
}
0 commit comments