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