1- #![ allow( clippy:: unreadable_literal) ]
1+ #![ allow( clippy:: unreadable_literal, clippy :: upper_case_acronyms ) ]
22
33//! An MT19937 Mersenne Twister rng implementation, with the goal of being
44//! compatible with CPython's `_random` module.
@@ -70,12 +70,14 @@ pub struct MT19937 {
7070 mt : [ u32 ; N ] , /* the array for the state vector */
7171 mti : usize , /* mti==N+1 means mt[N] is not initialized */
7272}
73+ const MT19937_DEFAULT : MT19937 = MT19937 {
74+ mt : [ 0 ; N ] ,
75+ mti : N + 1 ,
76+ } ;
7377impl Default for MT19937 {
78+ #[ inline]
7479 fn default ( ) -> Self {
75- MT19937 {
76- mt : [ 0 ; N ] ,
77- mti : N + 1 ,
78- }
80+ MT19937_DEFAULT
7981 }
8082}
8183impl std:: fmt:: Debug for MT19937 {
@@ -85,6 +87,7 @@ impl std::fmt::Debug for MT19937 {
8587}
8688
8789impl MT19937 {
90+ #[ inline]
8891 pub fn new_with_slice_seed ( init_key : & [ u32 ] ) -> Self {
8992 let mut state = Self :: default ( ) ;
9093 state. seed_slice ( init_key) ;
@@ -231,15 +234,19 @@ pub fn gen_res53<R: rand_core::RngCore>(rng: &mut R) -> f64 {
231234}
232235
233236impl rand_core:: RngCore for MT19937 {
237+ #[ inline]
234238 fn next_u32 ( & mut self ) -> u32 {
235239 self . gen_u32 ( )
236240 }
241+ #[ inline]
237242 fn next_u64 ( & mut self ) -> u64 {
238243 rand_core:: impls:: next_u64_via_u32 ( self )
239244 }
245+ #[ inline]
240246 fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
241247 rand_core:: impls:: fill_bytes_via_next ( self , dest)
242248 }
249+ #[ inline]
243250 fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , rand_core:: Error > {
244251 self . fill_bytes ( dest) ;
245252 Ok ( ( ) )
@@ -251,20 +258,22 @@ impl rand_core::RngCore for MT19937 {
251258/// Very big seed, but this is the size that CPython uses as well
252259pub struct Seed ( pub [ u32 ; N ] ) ;
253260impl Default for Seed {
261+ #[ inline]
254262 fn default ( ) -> Self {
255263 Seed ( [ 0 ; N ] )
256264 }
257265}
258266impl AsMut < [ u8 ] > for Seed {
267+ #[ inline]
259268 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)
262270 unsafe { self . 0 . align_to_mut ( ) . 1 }
263271 }
264272}
265273impl rand_core:: SeedableRng for MT19937 {
266274 type Seed = Seed ;
275+ #[ inline]
267276 fn from_seed ( seed : Self :: Seed ) -> Self {
268- Self :: new_with_slice_seed ( & seed. 0 )
277+ Self :: new_with_slice_seed ( & seed. 0 [ .. ] )
269278 }
270279}
0 commit comments