1919 */
2020
2121use cfg_if:: cfg_if;
22- use rand:: { Rng , SeedableRng , distr:: Alphanumeric , rngs:: SmallRng } ;
23- use std:: iter;
24-
25- #[ cfg( test) ]
26- const TEST_RANDOM_SEED : [ u8 ; 32 ] = [
27- 0x53 , 0x43 , 0x50 , 0x2d , 0x31 , 0x37 , 0x33 , 0x3a , 0x20 , 0x4d , 0x6f , 0x76 , 0x65 , 0x64 ,
28- 0x20 , 0x74 , 0x6f , 0x20 , 0x53 , 0x69 , 0x74 , 0x65 , 0x2d , 0x31 , 0x39 , 0x20 , 0x31 , 0x39 ,
29- 0x39 , 0x33 , 0x2e , 0x0a ,
30- ] ;
22+ use rand:: distr:: { Alphanumeric , SampleString } ;
23+ use rand:: rngs:: SmallRng ;
3124
3225#[ derive( Debug ) ]
3326pub struct Random {
@@ -39,9 +32,10 @@ impl Default for Random {
3932 fn default ( ) -> Self {
4033 cfg_if ! {
4134 if #[ cfg( test) ] {
42- let rng = SmallRng :: from_seed( TEST_RANDOM_SEED ) ;
35+ use rand:: SeedableRng ;
36+ let rng = SmallRng :: seed_from_u64( 1 ) ;
4337 } else {
44- let rng = SmallRng :: from_rng ( & mut rand:: rng ( ) ) ;
38+ let rng = rand:: make_rng ( ) ;
4539 }
4640 }
4741
@@ -52,13 +46,7 @@ impl Default for Random {
5246impl Random {
5347 pub fn generate_html_id_into ( & mut self , buffer : & mut String ) {
5448 buffer. push_str ( "wj-id-" ) ;
55-
56- let char_stream = iter:: repeat ( ( ) )
57- . map ( |_| self . rng . sample ( Alphanumeric ) )
58- . map ( char:: from)
59- . take ( 16 ) ;
60-
61- buffer. extend ( char_stream) ;
49+ Alphanumeric . append_string ( & mut self . rng , buffer, 16 ) ;
6250 }
6351
6452 pub fn generate_html_id ( & mut self ) -> String {
@@ -80,13 +68,13 @@ fn html_id() {
8068
8169 rand. generate_html_id_into ( & mut buffer) ;
8270 assert_eq ! (
83- buffer, "wj-id-bW5Ql2DLZtnd9s18 " ,
71+ buffer, "wj-id-zvGvLlhGI6VEZFKj " ,
8472 "Generated HTML ID doesn't match expected" ,
8573 ) ;
8674
8775 let html_id = rand. generate_html_id ( ) ;
8876 assert_eq ! (
89- html_id, "wj-id-ePZbhugrfP89c4Fk " ,
77+ html_id, "wj-id-e9pQyKaPmLnulpgn " ,
9078 "Generated HTML ID doesn't match expected" ,
9179 ) ;
9280}
0 commit comments