244
244
#![ cfg_attr( not( feature="std" ) , no_std) ]
245
245
#![ cfg_attr( all( feature="box" , not( feature="std" ) ) , feature( alloc) ) ]
246
246
#![ cfg_attr( all( feature="vec" , not( feature="std" ) ) , 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( all( feature="box" , not( feature="std" ) ) ) ] extern crate alloc;
253
254
#[ cfg( all( feature="vec" , not( feature="std" ) ) ) ] extern crate collections;
254
255
255
- #[ cfg( feature="std" ) ] use core:: cell:: RefCell ;
256
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ] use core:: cell:: RefCell ;
256
257
use core:: marker;
257
258
use core:: mem;
258
259
#[ cfg( feature="std" ) ] use std:: io;
259
- #[ cfg( feature="std" ) ] use std:: rc:: Rc ;
260
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ] use std:: rc:: Rc ;
260
261
use core:: num:: Wrapping as w;
261
262
#[ cfg( all( feature="box" , not( feature="std" ) ) ) ] use alloc:: boxed:: Box ;
262
263
#[ cfg( all( feature="vec" , not( feature="std" ) ) ) ] use collections:: vec:: Vec ;
263
264
264
- #[ cfg( feature="std" ) ] pub use os:: OsRng ;
265
+ #[ cfg( any ( feature="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( feature="std" ) ] pub mod os;
283
+ #[ cfg( any ( feature="std" , feature= "rdrand" ) ) ] pub mod os;
283
284
#[ cfg( any( feature="std" , feature="core_io" ) ) ] pub mod read;
284
285
285
286
#[ allow( bad_style) ]
@@ -822,7 +823,7 @@ impl StdRng {
822
823
///
823
824
/// Reading the randomness from the OS may fail, and any error is
824
825
/// propagated via the `io::Result` return value.
825
- #[ cfg( feature="std" ) ]
826
+ #[ cfg( any ( feature="std" , feature= "rdrand" ) ) ]
826
827
pub fn new ( ) -> io:: Result < StdRng > {
827
828
OsRng :: new ( ) . map ( |mut r| StdRng { rng : r. gen ( ) } )
828
829
}
@@ -861,7 +862,7 @@ impl<'a> SeedableRng<&'a [usize]> for StdRng {
861
862
///
862
863
/// This will read randomness from the operating system to seed the
863
864
/// generator.
864
- #[ cfg( feature="std" ) ]
865
+ #[ cfg( any ( feature="std" , feature= "rdrand" ) ) ]
865
866
pub fn weak_rng ( ) -> XorShiftRng {
866
867
match OsRng :: new ( ) {
867
868
Ok ( mut r) => r. gen ( ) ,
@@ -870,10 +871,10 @@ pub fn weak_rng() -> XorShiftRng {
870
871
}
871
872
872
873
/// Controls how the thread-local RNG is reseeded.
873
- #[ cfg( feature="std" ) ]
874
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
874
875
struct ThreadRngReseeder ;
875
876
876
- #[ cfg( feature="std" ) ]
877
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
877
878
impl reseeding:: Reseeder < StdRng > for ThreadRngReseeder {
878
879
fn reseed ( & mut self , rng : & mut StdRng ) {
879
880
* rng = match StdRng :: new ( ) {
@@ -882,14 +883,14 @@ impl reseeding::Reseeder<StdRng> for ThreadRngReseeder {
882
883
}
883
884
}
884
885
}
885
- #[ cfg( feature="std" ) ]
886
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
886
887
const THREAD_RNG_RESEED_THRESHOLD : u64 = 32_768 ;
887
- #[ cfg( feature="std" ) ]
888
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
888
889
type ThreadRngInner = reseeding:: ReseedingRng < StdRng , ThreadRngReseeder > ;
889
890
890
891
/// The thread-local RNG.
892
+ #[ cfg( all( feature="std" , not( feature="rdrand" ) ) ) ]
891
893
#[ derive( Clone ) ]
892
- #[ cfg( feature="std" ) ]
893
894
pub struct ThreadRng {
894
895
rng : Rc < RefCell < ThreadRngInner > > ,
895
896
}
@@ -905,7 +906,7 @@ pub struct ThreadRng {
905
906
/// if the operating system random number generator is rigged to give
906
907
/// the same sequence always. If absolute consistency is required,
907
908
/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
908
- #[ cfg( feature="std" ) ]
909
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
909
910
pub fn thread_rng ( ) -> ThreadRng {
910
911
// used to make space in TLS for a random number generator
911
912
thread_local ! ( static THREAD_RNG_KEY : Rc <RefCell <ThreadRngInner >> = {
@@ -922,7 +923,7 @@ pub fn thread_rng() -> ThreadRng {
922
923
ThreadRng { rng : THREAD_RNG_KEY . with ( |t| t. clone ( ) ) }
923
924
}
924
925
925
- #[ cfg( feature="std" ) ]
926
+ #[ cfg( all ( feature="std" , not ( feature= "rdrand" ) ) ) ]
926
927
impl Rng for ThreadRng {
927
928
fn next_u32 ( & mut self ) -> u32 {
928
929
self . rng . borrow_mut ( ) . next_u32 ( )
@@ -938,6 +939,14 @@ impl Rng for ThreadRng {
938
939
}
939
940
}
940
941
942
+ #[ cfg( feature="rdrand" ) ]
943
+ pub use os:: OsRng as ThreadRng ;
944
+
945
+ #[ cfg( feature="rdrand" ) ]
946
+ pub fn thread_rng ( ) -> ThreadRng {
947
+ OsRng :: new ( ) . unwrap ( )
948
+ }
949
+
941
950
/// Generates a random value using the thread-local random number generator.
942
951
///
943
952
/// `random()` can generate various types of random things, and so may require
@@ -980,7 +989,7 @@ impl Rng for ThreadRng {
980
989
/// *x = rng.gen();
981
990
/// }
982
991
/// ```
983
- #[ cfg( feature="std" ) ]
992
+ #[ cfg( any ( feature="std" , feature= "rdrand" ) ) ]
984
993
#[ inline]
985
994
pub fn random < T : Rand > ( ) -> T {
986
995
thread_rng ( ) . gen ( )
0 commit comments