1
- #![ allow( clippy:: unreadable_literal) ]
1
+ #![ allow( clippy:: unreadable_literal, clippy :: upper_case_acronyms ) ]
2
2
3
3
//! An MT19937 Mersenne Twister rng implementation, with the goal of being
4
4
//! compatible with CPython's `_random` module.
@@ -70,12 +70,14 @@ pub struct MT19937 {
70
70
mt : [ u32 ; N ] , /* the array for the state vector */
71
71
mti : usize , /* mti==N+1 means mt[N] is not initialized */
72
72
}
73
+ const MT19937_DEFAULT : MT19937 = MT19937 {
74
+ mt : [ 0 ; N ] ,
75
+ mti : N + 1 ,
76
+ } ;
73
77
impl Default for MT19937 {
78
+ #[ inline]
74
79
fn default ( ) -> Self {
75
- MT19937 {
76
- mt : [ 0 ; N ] ,
77
- mti : N + 1 ,
78
- }
80
+ MT19937_DEFAULT
79
81
}
80
82
}
81
83
impl std:: fmt:: Debug for MT19937 {
@@ -85,6 +87,7 @@ impl std::fmt::Debug for MT19937 {
85
87
}
86
88
87
89
impl MT19937 {
90
+ #[ inline]
88
91
pub fn new_with_slice_seed ( init_key : & [ u32 ] ) -> Self {
89
92
let mut state = Self :: default ( ) ;
90
93
state. seed_slice ( init_key) ;
@@ -231,15 +234,19 @@ pub fn gen_res53<R: rand_core::RngCore>(rng: &mut R) -> f64 {
231
234
}
232
235
233
236
impl rand_core:: RngCore for MT19937 {
237
+ #[ inline]
234
238
fn next_u32 ( & mut self ) -> u32 {
235
239
self . gen_u32 ( )
236
240
}
241
+ #[ inline]
237
242
fn next_u64 ( & mut self ) -> u64 {
238
243
rand_core:: impls:: next_u64_via_u32 ( self )
239
244
}
245
+ #[ inline]
240
246
fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
241
247
rand_core:: impls:: fill_bytes_via_next ( self , dest)
242
248
}
249
+ #[ inline]
243
250
fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , rand_core:: Error > {
244
251
self . fill_bytes ( dest) ;
245
252
Ok ( ( ) )
@@ -251,20 +258,22 @@ impl rand_core::RngCore for MT19937 {
251
258
//github.com/ Very big seed, but this is the size that CPython uses as well
252
259
pub struct Seed ( pub [ u32 ; N ] ) ;
253
260
impl Default for Seed {
261
+ #[ inline]
254
262
fn default ( ) -> Self {
255
263
Seed ( [ 0 ; N ] )
256
264
}
257
265
}
258
266
impl AsMut < [ u8 ] > for Seed {
267
+ #[ inline]
259
268
fn as_mut ( & mut self ) -> & mut [ u8 ] {
260
- // in the rare chance that we aren't aligned well, just ignore it; the
261
- // max entropy we could lose is 48 bits
269
+ // this will always get the full bytes, since align_of(u32) > align_of(u8)
262
270
unsafe { self . 0 . align_to_mut ( ) . 1 }
263
271
}
264
272
}
265
273
impl rand_core:: SeedableRng for MT19937 {
266
274
type Seed = Seed ;
275
+ #[ inline]
267
276
fn from_seed ( seed : Self :: Seed ) -> Self {
268
- Self :: new_with_slice_seed ( & seed. 0 )
277
+ Self :: new_with_slice_seed ( & seed. 0 [ .. ] )
269
278
}
270
279
}
0 commit comments