11
11
//! Thread-local random number generator
12
12
13
13
use std:: cell:: UnsafeCell ;
14
- use std:: rc:: Rc ;
15
14
16
15
use { RngCore , CryptoRng , SeedableRng , Error } ;
17
16
use rngs:: adapter:: ReseedingRng ;
@@ -74,18 +73,19 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 32*1024*1024; // 32 MiB
74
73
/// [HC-128]: ../prng/hc128/struct.Hc128Rng.html
75
74
#[ derive( Clone , Debug ) ]
76
75
pub struct ThreadRng {
77
- rng : Rc < UnsafeCell < ReseedingRng < Hc128Core , EntropyRng > > > ,
76
+ // use of raw pointer implies type is neither Send nor Sync
77
+ rng : * mut ReseedingRng < Hc128Core , EntropyRng > ,
78
78
}
79
79
80
80
thread_local ! (
81
- static THREAD_RNG_KEY : Rc < UnsafeCell <ReseedingRng <Hc128Core , EntropyRng > >> = {
81
+ static THREAD_RNG_KEY : UnsafeCell <ReseedingRng <Hc128Core , EntropyRng >> = {
82
82
let mut entropy_source = EntropyRng :: new( ) ;
83
83
let r = Hc128Core :: from_rng( & mut entropy_source) . unwrap_or_else( |err|
84
84
panic!( "could not initialize thread_rng: {}" , err) ) ;
85
85
let rng = ReseedingRng :: new( r,
86
86
THREAD_RNG_RESEED_THRESHOLD ,
87
87
entropy_source) ;
88
- Rc :: new ( UnsafeCell :: new( rng) )
88
+ UnsafeCell :: new( rng)
89
89
}
90
90
) ;
91
91
@@ -98,26 +98,26 @@ thread_local!(
98
98
///
99
99
/// [`ThreadRng`]: rngs/struct.ThreadRng.html
100
100
pub fn thread_rng ( ) -> ThreadRng {
101
- ThreadRng { rng : THREAD_RNG_KEY . with ( |t| t. clone ( ) ) }
101
+ ThreadRng { rng : THREAD_RNG_KEY . with ( |t| t. get ( ) ) }
102
102
}
103
103
104
104
impl RngCore for ThreadRng {
105
105
#[ inline( always) ]
106
106
fn next_u32 ( & mut self ) -> u32 {
107
- unsafe { ( * self . rng . get ( ) ) . next_u32 ( ) }
107
+ unsafe { ( * self . rng ) . next_u32 ( ) }
108
108
}
109
109
110
110
#[ inline( always) ]
111
111
fn next_u64 ( & mut self ) -> u64 {
112
- unsafe { ( * self . rng . get ( ) ) . next_u64 ( ) }
112
+ unsafe { ( * self . rng ) . next_u64 ( ) }
113
113
}
114
114
115
115
fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
116
- unsafe { ( * self . rng . get ( ) ) . fill_bytes ( dest) }
116
+ unsafe { ( * self . rng ) . fill_bytes ( dest) }
117
117
}
118
118
119
119
fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
120
- unsafe { ( * self . rng . get ( ) ) . try_fill_bytes ( dest) }
120
+ unsafe { ( * self . rng ) . try_fill_bytes ( dest) }
121
121
}
122
122
}
123
123
0 commit comments