33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- use rand:: distributions :: { Distribution , Uniform } ;
7- use rand:: { thread_rng , Rng } ;
6+ use rand:: distr :: { Distribution , Uniform } ;
7+ use rand:: { rng , Rng } ;
88
99/// Samples alphanumeric characters `[A-Za-z0-9]` including newline `\n`
1010///
@@ -38,7 +38,7 @@ impl AlphanumericNewline {
3838 where
3939 R : Rng + ?Sized ,
4040 {
41- let idx = rng. gen_range ( 0 ..Self :: CHARSET . len ( ) ) ;
41+ let idx = rng. random_range ( 0 ..Self :: CHARSET . len ( ) ) ;
4242 Self :: CHARSET [ idx]
4343 }
4444}
@@ -80,7 +80,7 @@ impl RandomString {
8080 where
8181 D : Distribution < u8 > ,
8282 {
83- thread_rng ( )
83+ rng ( )
8484 . sample_iter ( dist)
8585 . take ( length)
8686 . map ( |b| b as char )
@@ -132,15 +132,15 @@ impl RandomString {
132132 return if num_delimiter > 0 {
133133 String :: from ( delimiter as char )
134134 } else {
135- String :: from ( thread_rng ( ) . sample ( & dist) as char )
135+ String :: from ( rng ( ) . sample ( & dist) as char )
136136 } ;
137137 }
138138
139139 let samples = length - 1 ;
140- let mut result: Vec < u8 > = thread_rng ( ) . sample_iter ( & dist) . take ( samples) . collect ( ) ;
140+ let mut result: Vec < u8 > = rng ( ) . sample_iter ( & dist) . take ( samples) . collect ( ) ;
141141
142142 if num_delimiter == 0 {
143- result. push ( thread_rng ( ) . sample ( & dist) ) ;
143+ result. push ( rng ( ) . sample ( & dist) ) ;
144144 return String :: from_utf8 ( result) . unwrap ( ) ;
145145 }
146146
@@ -150,9 +150,10 @@ impl RandomString {
150150 num_delimiter
151151 } ;
152152
153- let between = Uniform :: new ( 0 , samples) ;
153+ // safe to unwrap because samples is at least 1, thus high > low
154+ let between = Uniform :: new ( 0 , samples) . unwrap ( ) ;
154155 for _ in 0 ..num_delimiter {
155- let mut pos = between. sample ( & mut thread_rng ( ) ) ;
156+ let mut pos = between. sample ( & mut rng ( ) ) ;
156157 let turn = pos;
157158 while result[ pos] == delimiter {
158159 pos += 1 ;
@@ -169,7 +170,7 @@ impl RandomString {
169170 if end_with_delimiter {
170171 result. push ( delimiter) ;
171172 } else {
172- result. push ( thread_rng ( ) . sample ( & dist) ) ;
173+ result. push ( rng ( ) . sample ( & dist) ) ;
173174 }
174175
175176 String :: from_utf8 ( result) . unwrap ( )
@@ -179,7 +180,7 @@ impl RandomString {
179180#[ cfg( test) ]
180181mod tests {
181182 use super :: * ;
182- use rand:: distributions :: Alphanumeric ;
183+ use rand:: distr :: Alphanumeric ;
183184
184185 #[ test]
185186 fn test_random_string_generate ( ) {
0 commit comments