243
243
244
244
#![ cfg_attr( feature="no_std" , no_std) ]
245
245
#![ cfg_attr( feature="no_std" , feature( alloc, collections) ) ]
246
+ #![ cfg_attr( feature="rdrand" , feature( asm) ) ]
246
247
247
248
#[ cfg( test) ] #[ macro_use] extern crate log;
248
249
251
252
#[ cfg( feature="no_std" ) ] extern crate alloc;
252
253
#[ cfg( feature="no_std" ) ] extern crate collections;
253
254
254
- #[ cfg( not( feature="no_std" ) ) ] use core:: cell:: RefCell ;
255
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ] use core:: cell:: RefCell ;
255
256
use core:: marker;
256
257
use core:: mem;
257
258
#[ cfg( not( feature="no_std" ) ) ] use std:: io;
258
- #[ cfg( not( feature="no_std" ) ) ] use std:: rc:: Rc ;
259
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ] use std:: rc:: Rc ;
259
260
use core:: num:: Wrapping as w;
260
261
#[ cfg( feature="no_std" ) ] use alloc:: boxed:: Box ;
261
262
#[ cfg( feature="no_std" ) ] use collections:: vec:: Vec ;
262
263
263
- #[ cfg( not( feature="no_std" ) ) ] pub use os:: OsRng ;
264
+ #[ cfg( any ( not( feature="no_std" ) , feature= "rdrand ") ) ] pub use os:: OsRng ;
264
265
265
266
pub use isaac:: { IsaacRng , Isaac64Rng } ;
266
267
pub use chacha:: ChaChaRng ;
@@ -278,7 +279,7 @@ pub mod isaac;
278
279
pub mod chacha;
279
280
pub mod reseeding;
280
281
mod rand_impls;
281
- #[ cfg( not( feature="no_std" ) ) ] pub mod os;
282
+ #[ cfg( any ( not( feature="no_std" ) , feature= "rdrand ") ) ] pub mod os;
282
283
pub mod read;
283
284
284
285
#[ allow( bad_style) ]
@@ -814,7 +815,7 @@ impl StdRng {
814
815
///
815
816
/// Reading the randomness from the OS may fail, and any error is
816
817
/// propagated via the `io::Result` return value.
817
- #[ cfg( not( feature="no_std" ) ) ]
818
+ #[ cfg( any ( not( feature="no_std" ) , feature= "rdrand ") ) ]
818
819
pub fn new ( ) -> io:: Result < StdRng > {
819
820
OsRng :: new ( ) . map ( |mut r| StdRng { rng : r. gen ( ) } )
820
821
}
@@ -853,7 +854,7 @@ impl<'a> SeedableRng<&'a [usize]> for StdRng {
853
854
///
854
855
/// This will read randomness from the operating system to seed the
855
856
/// generator.
856
- #[ cfg( not( feature="no_std" ) ) ]
857
+ #[ cfg( any ( not( feature="no_std" ) , feature= "rdrand ") ) ]
857
858
pub fn weak_rng ( ) -> XorShiftRng {
858
859
match OsRng :: new ( ) {
859
860
Ok ( mut r) => r. gen ( ) ,
@@ -862,10 +863,10 @@ pub fn weak_rng() -> XorShiftRng {
862
863
}
863
864
864
865
/// Controls how the thread-local RNG is reseeded.
865
- #[ cfg( not( feature="no_std" ) ) ]
866
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
866
867
struct ThreadRngReseeder ;
867
868
868
- #[ cfg( not( feature="no_std" ) ) ]
869
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
869
870
impl reseeding:: Reseeder < StdRng > for ThreadRngReseeder {
870
871
fn reseed ( & mut self , rng : & mut StdRng ) {
871
872
* rng = match StdRng :: new ( ) {
@@ -874,14 +875,14 @@ impl reseeding::Reseeder<StdRng> for ThreadRngReseeder {
874
875
}
875
876
}
876
877
}
877
- #[ cfg( not( feature="no_std" ) ) ]
878
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
878
879
const THREAD_RNG_RESEED_THRESHOLD : u64 = 32_768 ;
879
- #[ cfg( not( feature="no_std" ) ) ]
880
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
880
881
type ThreadRngInner = reseeding:: ReseedingRng < StdRng , ThreadRngReseeder > ;
881
882
882
883
/// The thread-local RNG.
884
+ #[ cfg( all( not( feature="no_std" ) , not( feature="rdrand" ) ) ) ]
883
885
#[ derive( Clone ) ]
884
- #[ cfg( not( feature="no_std" ) ) ]
885
886
pub struct ThreadRng {
886
887
rng : Rc < RefCell < ThreadRngInner > > ,
887
888
}
@@ -897,7 +898,7 @@ pub struct ThreadRng {
897
898
/// if the operating system random number generator is rigged to give
898
899
/// the same sequence always. If absolute consistency is required,
899
900
/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
900
- #[ cfg( not( feature="no_std" ) ) ]
901
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
901
902
pub fn thread_rng ( ) -> ThreadRng {
902
903
// used to make space in TLS for a random number generator
903
904
thread_local ! ( static THREAD_RNG_KEY : Rc <RefCell <ThreadRngInner >> = {
@@ -914,7 +915,7 @@ pub fn thread_rng() -> ThreadRng {
914
915
ThreadRng { rng : THREAD_RNG_KEY . with ( |t| t. clone ( ) ) }
915
916
}
916
917
917
- #[ cfg( not( feature="no_std" ) ) ]
918
+ #[ cfg( all ( not( feature="no_std" ) , not ( feature= "rdrand" ) ) ) ]
918
919
impl Rng for ThreadRng {
919
920
fn next_u32 ( & mut self ) -> u32 {
920
921
self . rng . borrow_mut ( ) . next_u32 ( )
@@ -930,6 +931,14 @@ impl Rng for ThreadRng {
930
931
}
931
932
}
932
933
934
+ #[ cfg( feature="rdrand" ) ]
935
+ pub use os:: OsRng as ThreadRng ;
936
+
937
+ #[ cfg( feature="rdrand" ) ]
938
+ pub fn thread_rng ( ) -> ThreadRng {
939
+ OsRng :: new ( ) . unwrap ( )
940
+ }
941
+
933
942
/// Generates a random value using the thread-local random number generator.
934
943
///
935
944
/// `random()` can generate various types of random things, and so may require
@@ -972,7 +981,7 @@ impl Rng for ThreadRng {
972
981
/// *x = rng.gen();
973
982
/// }
974
983
/// ```
975
- #[ cfg( not( feature="no_std" ) ) ]
984
+ #[ cfg( any ( not( feature="no_std" ) , feature= "rdrand ") ) ]
976
985
#[ inline]
977
986
pub fn random < T : Rand > ( ) -> T {
978
987
thread_rng ( ) . gen ( )
0 commit comments