File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -449,6 +449,14 @@ impl Rng {
449
449
}
450
450
}
451
451
452
+ /// Return a byte array of the given size.
453
+ #[ inline]
454
+ pub fn bytes < const N : usize > ( & self ) -> [ u8 ; N ] {
455
+ let mut bytes = [ 0u8 ; N ] ;
456
+ self . fill ( & mut bytes) ;
457
+ bytes
458
+ }
459
+
452
460
rng_integer ! (
453
461
u8 ,
454
462
u8 ,
Original file line number Diff line number Diff line change @@ -81,6 +81,29 @@ fn u128() {
81
81
}
82
82
}
83
83
84
+ #[ test]
85
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
86
+ fn fill ( ) {
87
+ let r = fastrand:: Rng :: new ( ) ;
88
+ let mut a = [ 0u8 ; 64 ] ;
89
+ let mut b = [ 0u8 ; 64 ] ;
90
+
91
+ r. fill ( & mut a) ;
92
+ r. fill ( & mut b) ;
93
+
94
+ assert_ne ! ( a, b) ;
95
+ }
96
+
97
+ #[ test]
98
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
99
+ fn bytes ( ) {
100
+ let r = fastrand:: Rng :: new ( ) ;
101
+ let a: [ u8 ; 32 ] = r. bytes ( ) ;
102
+ let b: [ u8 ; 32 ] = r. bytes ( ) ;
103
+
104
+ assert_ne ! ( a, b) ;
105
+ }
106
+
84
107
#[ test]
85
108
#[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test) ]
86
109
fn rng ( ) {
You can’t perform that action at this time.
0 commit comments