11
11
//! A wrapper around another RNG that reseeds it after it
12
12
//! generates a certain number of random bytes.
13
13
14
- use { Rng , Error } ;
15
- #[ cfg( feature="std" ) ]
16
- use NewSeeded ;
14
+ use { Rng , SeedableRng , Error } ;
17
15
18
16
/// A wrapper around any RNG which reseeds the underlying RNG after it
19
17
/// has generated a certain number of random bytes.
@@ -23,10 +21,10 @@ pub struct ReseedingRng<R, Rsdr> {
23
21
generation_threshold : u64 ,
24
22
bytes_generated : u64 ,
25
23
/// Controls the behaviour when reseeding the RNG.
26
- pub reseeder : Rsdr ,
24
+ reseeder : Rsdr ,
27
25
}
28
26
29
- impl < R : Rng , Rsdr : Reseeder < R > > ReseedingRng < R , Rsdr > {
27
+ impl < R : Rng + SeedableRng , Rsdr : Rng > ReseedingRng < R , Rsdr > {
30
28
/// Create a new `ReseedingRng` with the given parameters.
31
29
///
32
30
/// # Arguments
@@ -47,14 +45,14 @@ impl<R: Rng, Rsdr: Reseeder<R>> ReseedingRng<R, Rsdr> {
47
45
/// generated exceed the threshold.
48
46
pub fn reseed_if_necessary ( & mut self ) {
49
47
if self . bytes_generated >= self . generation_threshold {
50
- self . reseeder . reseed ( & mut self . rng ) . unwrap ( ) ;
48
+ R :: from_rng ( & mut self . reseeder ) . map ( |result| self . rng = result ) . unwrap ( ) ;
51
49
self . bytes_generated = 0 ;
52
50
}
53
51
}
54
52
}
55
53
56
54
57
- impl < R : Rng , Rsdr : Reseeder < R > > Rng for ReseedingRng < R , Rsdr > {
55
+ impl < R : Rng + SeedableRng , Rsdr : Rng > Rng for ReseedingRng < R , Rsdr > {
58
56
fn next_u32 ( & mut self ) -> u32 {
59
57
self . reseed_if_necessary ( ) ;
60
58
self . bytes_generated += 4 ;
@@ -80,35 +78,11 @@ impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> {
80
78
}
81
79
}
82
80
83
- /// Something that can be used to reseed an RNG via `ReseedingRng`.
84
- ///
85
- /// Note that implementations should support `Clone` only if reseeding is
86
- /// deterministic (no external entropy source). This is so that a `ReseedingRng`
87
- /// only supports `Clone` if fully deterministic.
88
- pub trait Reseeder < R : ?Sized > {
89
- /// Reseed the given RNG.
90
- ///
91
- /// On error, this should just forward the source error; errors are handled
92
- /// by the caller.
93
- fn reseed ( & mut self , rng : & mut R ) -> Result < ( ) , Error > ;
94
- }
95
-
96
- /// Reseed an RNG using `NewSeeded` to replace the current instance.
97
- #[ cfg( feature="std" ) ]
98
- #[ derive( Debug ) ]
99
- pub struct ReseedWithNew ;
100
-
101
- #[ cfg( feature="std" ) ]
102
- impl < R : Rng + NewSeeded > Reseeder < R > for ReseedWithNew {
103
- fn reseed ( & mut self , rng : & mut R ) -> Result < ( ) , Error > {
104
- R :: new ( ) . map ( |result| * rng = result)
105
- }
106
- }
107
81
108
82
#[ cfg( test) ]
109
83
mod test {
110
84
use { impls, le} ;
111
- use super :: { ReseedingRng , Reseeder } ;
85
+ use super :: { ReseedingRng } ;
112
86
use { SeedableRng , Rng , Error } ;
113
87
114
88
struct Counter {
@@ -139,17 +113,20 @@ mod test {
139
113
}
140
114
141
115
#[ derive( Debug , Clone ) ]
142
- struct ReseedCounter ;
143
- impl Reseeder < Counter > for ReseedCounter {
144
- fn reseed ( & mut self , rng : & mut Counter ) -> Result < ( ) , Error > {
145
- * rng = Counter { i : 0 } ;
116
+ struct ResetCounter ;
117
+ impl Rng for ResetCounter {
118
+ fn next_u32 ( & mut self ) -> u32 { unimplemented ! ( ) }
119
+ fn next_u64 ( & mut self ) -> u64 { unimplemented ! ( ) }
120
+ fn fill_bytes ( & mut self , _dest : & mut [ u8 ] ) { unimplemented ! ( ) }
121
+ fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
122
+ for i in dest. iter_mut ( ) { * i = 0 ; }
146
123
Ok ( ( ) )
147
124
}
148
125
}
149
126
150
127
#[ test]
151
128
fn test_reseeding ( ) {
152
- let mut rs = ReseedingRng :: new ( Counter { i : 0 } , 400 , ReseedCounter ) ;
129
+ let mut rs = ReseedingRng :: new ( Counter { i : 0 } , 400 , ResetCounter ) ;
153
130
154
131
let mut i = 0 ;
155
132
for _ in 0 ..1000 {
0 commit comments