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