@@ -29,7 +29,6 @@ use core::num::NonZeroU32;
29
29
use core:: ops:: Shl ;
30
30
use embedded_hal_02:: blocking:: rng;
31
31
use fugit:: RateExtU32 ;
32
- use rand_core:: RngCore ;
33
32
34
33
/// Random number generator specific errors
35
34
#[ derive( Debug , Eq , PartialEq , Copy , Clone ) ]
@@ -43,10 +42,10 @@ pub enum ErrorKind {
43
42
SeedError = 4 ,
44
43
}
45
44
46
- impl From < ErrorKind > for rand_core :: Error {
47
- fn from ( err : ErrorKind ) -> rand_core :: Error {
48
- let err_code = NonZeroU32 :: new ( rand_core :: Error :: CUSTOM_START + err as u32 ) . unwrap ( ) ;
49
- rand_core :: Error :: from ( err_code)
45
+ impl From < ErrorKind > for rand_core_06 :: Error {
46
+ fn from ( err : ErrorKind ) -> rand_core_06 :: Error {
47
+ let err_code = NonZeroU32 :: new ( rand_core_06 :: Error :: CUSTOM_START + err as u32 ) . unwrap ( ) ;
48
+ rand_core_06 :: Error :: from ( err_code)
50
49
}
51
50
}
52
51
@@ -135,6 +134,19 @@ impl Rng {
135
134
}
136
135
}
137
136
137
+ fn try_fill_bytes ( & mut self , buffer : & mut [ u8 ] ) -> Result < ( ) , ErrorKind > {
138
+ const BATCH_SIZE : usize = 4 / mem:: size_of :: < u8 > ( ) ;
139
+ let mut i = 0_usize ;
140
+ while i < buffer. len ( ) {
141
+ let random_word = self . next_random_word ( ) ?;
142
+ let bytes = random_word. to_ne_bytes ( ) ;
143
+ let n = cmp:: min ( BATCH_SIZE , buffer. len ( ) - i) ;
144
+ buffer[ i..i + n] . copy_from_slice ( & bytes[ ..n] ) ;
145
+ i += n;
146
+ }
147
+ Ok ( ( ) )
148
+ }
149
+
138
150
/// Releases ownership of the [`RNG`] peripheral object
139
151
/// (after which `self` can't be used anymore).
140
152
pub fn release ( self ) -> RNG {
@@ -143,14 +155,31 @@ impl Rng {
143
155
}
144
156
145
157
impl rng:: Read for Rng {
146
- type Error = rand_core :: Error ;
158
+ type Error = rand_core_06 :: Error ;
147
159
148
160
fn read ( & mut self , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
149
- self . try_fill_bytes ( buffer)
161
+ self . try_fill_bytes ( buffer) ?;
162
+ Ok ( ( ) )
163
+ }
164
+ }
165
+
166
+ impl rand_core:: RngCore for Rng {
167
+ fn next_u32 ( & mut self ) -> u32 {
168
+ self . next_random_word ( ) . unwrap ( )
169
+ }
170
+
171
+ fn next_u64 ( & mut self ) -> u64 {
172
+ let w1 = self . next_u32 ( ) ;
173
+ let w2 = self . next_u32 ( ) ;
174
+ ( w1 as u64 ) . shl ( 32 ) | ( w2 as u64 )
175
+ }
176
+
177
+ fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
178
+ self . try_fill_bytes ( dest) . unwrap ( )
150
179
}
151
180
}
152
181
153
- impl RngCore for Rng {
182
+ impl rand_core_06 :: RngCore for Rng {
154
183
fn next_u32 ( & mut self ) -> u32 {
155
184
self . next_random_word ( ) . unwrap ( )
156
185
}
@@ -166,16 +195,8 @@ impl RngCore for Rng {
166
195
}
167
196
168
197
/// Fills buffer with random values, or returns an error
169
- fn try_fill_bytes ( & mut self , buffer : & mut [ u8 ] ) -> Result < ( ) , rand_core:: Error > {
170
- const BATCH_SIZE : usize = 4 / mem:: size_of :: < u8 > ( ) ;
171
- let mut i = 0_usize ;
172
- while i < buffer. len ( ) {
173
- let random_word = self . next_random_word ( ) ?;
174
- let bytes = random_word. to_ne_bytes ( ) ;
175
- let n = cmp:: min ( BATCH_SIZE , buffer. len ( ) - i) ;
176
- buffer[ i..i + n] . copy_from_slice ( & bytes[ ..n] ) ;
177
- i += n;
178
- }
198
+ fn try_fill_bytes ( & mut self , buffer : & mut [ u8 ] ) -> Result < ( ) , rand_core_06:: Error > {
199
+ self . try_fill_bytes ( buffer) ?;
179
200
Ok ( ( ) )
180
201
}
181
- }
202
+ }
0 commit comments