84
84
//!
85
85
//! The [`distributions` module] provides implementations
86
86
//! of some other distributions, including Normal, Log-Normal and Exponential.
87
- //!
87
+ //!
88
88
//! It is worth noting that the functionality already mentioned is implemented
89
89
//! with distributions: [`gen`] samples values using the [`Standard`]
90
90
//! distribution, while [`gen_range`] uses [`Uniform`].
109
109
//!
110
110
//! // thread_rng is often the most convenient source of randomness:
111
111
//! let mut rng = thread_rng();
112
- //!
112
+ //!
113
113
//! if rng.gen() { // random bool
114
114
//! let x: f64 = rng.gen(); // random number in range [0, 1)
115
115
//! println!("x is: {}", x);
230
230
#![ cfg_attr( all( feature="i128_support" , feature="nightly" ) , feature( i128_type, i128 ) ) ]
231
231
#![ cfg_attr( all( feature="simd_support" , feature="nightly" ) , feature( stdsimd) ) ]
232
232
#![ cfg_attr( feature = "stdweb" , recursion_limit="128" ) ]
233
+ #![ cfg_attr( feature = "wasm-bindgen" , feature( proc_macro) ) ]
234
+ #![ cfg_attr( feature = "wasm-bindgen" , feature( wasm_import_module) ) ]
235
+ #![ cfg_attr( feature = "wasm-bindgen" , feature( wasm_custom_section) ) ]
233
236
234
237
#[ cfg( feature="std" ) ] extern crate std as core;
235
238
#[ cfg( all( feature = "alloc" , not( feature="std" ) ) ) ] extern crate alloc;
242
245
#[ macro_use]
243
246
extern crate stdweb;
244
247
248
+ #[ cfg( all( target_arch = "wasm32" , feature = "wasm-bindgen" ) ) ]
249
+ extern crate wasm_bindgen;
250
+
245
251
extern crate rand_core;
246
252
247
253
#[ cfg( feature = "log" ) ] #[ macro_use] extern crate log;
@@ -297,7 +303,8 @@ pub mod seq;
297
303
target_os = "redox" ,
298
304
target_os = "fuchsia" ,
299
305
windows,
300
- all( target_arch = "wasm32" , feature = "stdweb" )
306
+ all( target_arch = "wasm32" , feature = "stdweb" ) ,
307
+ all( target_arch = "wasm32" , feature = "wasm-bindgen" ) ,
301
308
) ) ) ]
302
309
#[ doc( hidden) ]
303
310
pub use deprecated:: OsRng ;
@@ -329,7 +336,8 @@ pub mod jitter {
329
336
target_os = "redox" ,
330
337
target_os = "fuchsia" ,
331
338
windows,
332
- all( target_arch = "wasm32" , feature = "stdweb" )
339
+ all( target_arch = "wasm32" , feature = "stdweb" ) ,
340
+ all( target_arch = "wasm32" , feature = "wasm-bindgen" ) ,
333
341
) ) ) ]
334
342
#[ doc( hidden) ]
335
343
pub mod os {
@@ -364,20 +372,20 @@ use distributions::uniform::{SampleUniform, UniformSampler, SampleBorrow};
364
372
365
373
/// An automatically-implemented extension trait on [`RngCore`] providing high-level
366
374
/// generic methods for sampling values and other convenience methods.
367
- ///
375
+ ///
368
376
/// This is the primary trait to use when generating random values.
369
- ///
377
+ ///
370
378
/// # Generic usage
371
- ///
379
+ ///
372
380
/// The basic pattern is `fn foo<R: Rng + ?Sized>(rng: &mut R)`. Some
373
381
/// things are worth noting here:
374
- ///
382
+ ///
375
383
/// - Since `Rng: RngCore` and every `RngCore` implements `Rng`, it makes no
376
384
/// difference whether we use `R: Rng` or `R: RngCore`.
377
385
/// - The `+ ?Sized` un-bounding allows functions to be called directly on
378
386
/// type-erased references; i.e. `foo(r)` where `r: &mut RngCore`. Without
379
387
/// this it would be necessary to write `foo(&mut r)`.
380
- ///
388
+ ///
381
389
/// An alternative pattern is possible: `fn foo<R: Rng>(rng: R)`. This has some
382
390
/// trade-offs. It allows the argument to be consumed directly without a `&mut`
383
391
/// (which is how `from_rng(thread_rng())` works); also it still works directly
@@ -386,20 +394,20 @@ use distributions::uniform::{SampleUniform, UniformSampler, SampleBorrow};
386
394
/// hence many uses of `rng` require an extra reference, either explicitly
387
395
/// (`distr.sample(&mut rng)`) or implicitly (`rng.gen()`); one may hope the
388
396
/// optimiser can remove redundant references later.
389
- ///
397
+ ///
390
398
/// Example:
391
- ///
399
+ ///
392
400
/// ```
393
401
/// # use rand::thread_rng;
394
402
/// use rand::Rng;
395
- ///
403
+ ///
396
404
/// fn foo<R: Rng + ?Sized>(rng: &mut R) -> f32 {
397
405
/// rng.gen()
398
406
/// }
399
407
///
400
408
/// # let v = foo(&mut thread_rng());
401
409
/// ```
402
- ///
410
+ ///
403
411
/// [`RngCore`]: trait.RngCore.html
404
412
pub trait Rng : RngCore {
405
413
/// Return a random value supporting the [`Standard`] distribution.
@@ -624,7 +632,7 @@ pub trait Rng: RngCore {
624
632
/// Return a random element from `values`.
625
633
///
626
634
/// Deprecated: use [`SliceRandom::choose`] instead.
627
- ///
635
+ ///
628
636
/// [`SliceRandom::choose`]: seq/trait.SliceRandom.html#method.choose
629
637
#[ deprecated( since="0.6.0" , note="use SliceRandom::choose instead" ) ]
630
638
fn choose < ' a , T > ( & mut self , values : & ' a [ T ] ) -> Option < & ' a T > {
@@ -635,7 +643,7 @@ pub trait Rng: RngCore {
635
643
/// Return a mutable pointer to a random element from `values`.
636
644
///
637
645
/// Deprecated: use [`SliceRandom::choose_mut`] instead.
638
- ///
646
+ ///
639
647
/// [`SliceRandom::choose_mut`]: seq/trait.SliceRandom.html#method.choose_mut
640
648
#[ deprecated( since="0.6.0" , note="use SliceRandom::choose_mut instead" ) ]
641
649
fn choose_mut < ' a , T > ( & mut self , values : & ' a mut [ T ] ) -> Option < & ' a mut T > {
@@ -646,7 +654,7 @@ pub trait Rng: RngCore {
646
654
/// Shuffle a mutable slice in place.
647
655
///
648
656
/// Deprecated: use [`SliceRandom::shuffle`] instead.
649
- ///
657
+ ///
650
658
/// [`SliceRandom::shuffle`]: seq/trait.SliceRandom.html#method.shuffle
651
659
#[ deprecated( since="0.6.0" , note="use SliceRandom::shuffle instead" ) ]
652
660
fn shuffle < T > ( & mut self , values : & mut [ T ] ) {
@@ -658,15 +666,15 @@ pub trait Rng: RngCore {
658
666
impl < R : RngCore + ?Sized > Rng for R { }
659
667
660
668
/// Trait for casting types to byte slices
661
- ///
669
+ ///
662
670
/// This is used by the [`fill`] and [`try_fill`] methods.
663
- ///
671
+ ///
664
672
/// [`fill`]: trait.Rng.html#method.fill
665
673
/// [`try_fill`]: trait.Rng.html#method.try_fill
666
674
pub trait AsByteSliceMut {
667
675
/// Return a mutable reference to self as a byte slice
668
676
fn as_byte_slice_mut ( & mut self ) -> & mut [ u8 ] ;
669
-
677
+
670
678
/// Call `to_le` on each element (i.e. byte-swap on Big Endian platforms).
671
679
fn to_le ( & mut self ) ;
672
680
}
@@ -675,7 +683,7 @@ impl AsByteSliceMut for [u8] {
675
683
fn as_byte_slice_mut ( & mut self ) -> & mut [ u8 ] {
676
684
self
677
685
}
678
-
686
+
679
687
fn to_le ( & mut self ) { }
680
688
}
681
689
@@ -698,7 +706,7 @@ macro_rules! impl_as_byte_slice {
698
706
}
699
707
}
700
708
}
701
-
709
+
702
710
fn to_le( & mut self ) {
703
711
for x in self {
704
712
* x = x. to_le( ) ;
@@ -724,7 +732,7 @@ macro_rules! impl_as_byte_slice_arrays {
724
732
( $n: expr, ) => { } ;
725
733
( $n: expr, $N: ident, $( $NN: ident, ) * ) => {
726
734
impl_as_byte_slice_arrays!( $n - 1 , $( $NN, ) * ) ;
727
-
735
+
728
736
impl <T > AsByteSliceMut for [ T ; $n] where [ T ] : AsByteSliceMut {
729
737
fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
730
738
self [ ..] . as_byte_slice_mut( )
@@ -743,7 +751,7 @@ macro_rules! impl_as_byte_slice_arrays {
743
751
fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
744
752
self [ ..] . as_byte_slice_mut( )
745
753
}
746
-
754
+
747
755
fn to_le( & mut self ) {
748
756
self [ ..] . to_le( )
749
757
}
@@ -874,6 +882,59 @@ pub fn random<T>() -> T where Standard: Distribution<T> {
874
882
thread_rng ( ) . gen ( )
875
883
}
876
884
885
+ // Due to rustwasm/wasm-bindgen#201 this can't be defined in the inner os
886
+ // modules, so hack around it for now and place it at the root.
887
+ #[ cfg( all( feature = "wasm-bindgen" , target_arch = "wasm32" ) ) ]
888
+ #[ doc( hidden) ]
889
+ #[ allow( missing_debug_implementations) ]
890
+ pub mod __wbg_shims {
891
+ extern crate wasm_bindgen;
892
+
893
+ pub use wasm_bindgen:: prelude:: * ;
894
+
895
+ #[ wasm_bindgen]
896
+ extern {
897
+ pub type This ;
898
+ pub static this: This ;
899
+
900
+ #[ wasm_bindgen( method, getter, structural) ]
901
+ pub fn window ( me : & This ) -> JsValue ;
902
+ #[ wasm_bindgen( method, getter, structural) ]
903
+ pub fn crypto ( me : & This ) -> JsValue ;
904
+
905
+ pub type BrowserCrypto ;
906
+
907
+ // TODO: these `structural` annotations here ideally wouldn't be here to
908
+ // avoid a JS shim, but for now with feature detection they're
909
+ // unavoidable.
910
+ #[ wasm_bindgen( method, js_name = getRandomValues, structural, getter) ]
911
+ pub fn get_random_values_fn ( me : & BrowserCrypto ) -> JsValue ;
912
+ #[ wasm_bindgen( method, js_name = getRandomValues, structural) ]
913
+ pub fn get_random_values ( me : & BrowserCrypto , buf : & mut [ u8 ] ) ;
914
+
915
+ #[ wasm_bindgen( js_name = require) ]
916
+ pub fn node_require ( s : & str ) -> NodeCrypto ;
917
+
918
+ pub type NodeCrypto ;
919
+
920
+ #[ wasm_bindgen( method, js_name = randomFillSync, structural) ]
921
+ pub fn random_fill_sync ( me : & NodeCrypto , buf : & mut [ u8 ] ) ;
922
+ }
923
+
924
+ // TODO: replace with derive once rustwasm/wasm-bindgen#400 is merged
925
+ impl Clone for BrowserCrypto {
926
+ fn clone ( & self ) -> BrowserCrypto {
927
+ BrowserCrypto { obj : self . obj . clone ( ) }
928
+ }
929
+ }
930
+
931
+ impl Clone for NodeCrypto {
932
+ fn clone ( & self ) -> NodeCrypto {
933
+ NodeCrypto { obj : self . obj . clone ( ) }
934
+ }
935
+ }
936
+ }
937
+
877
938
#[ cfg( test) ]
878
939
mod test {
879
940
use rngs:: mock:: StepRng ;
@@ -936,25 +997,25 @@ mod test {
936
997
}
937
998
}
938
999
}
939
-
1000
+
940
1001
#[ test]
941
1002
fn test_fill ( ) {
942
1003
let x = 9041086907909331047 ; // a random u64
943
1004
let mut rng = StepRng :: new ( x, 0 ) ;
944
-
1005
+
945
1006
// Convert to byte sequence and back to u64; byte-swap twice if BE.
946
1007
let mut array = [ 0u64 ; 2 ] ;
947
1008
rng. fill ( & mut array[ ..] ) ;
948
1009
assert_eq ! ( array, [ x, x] ) ;
949
1010
assert_eq ! ( rng. next_u64( ) , x) ;
950
-
1011
+
951
1012
// Convert to bytes then u32 in LE order
952
1013
let mut array = [ 0u32 ; 2 ] ;
953
1014
rng. fill ( & mut array[ ..] ) ;
954
1015
assert_eq ! ( array, [ x as u32 , ( x >> 32 ) as u32 ] ) ;
955
1016
assert_eq ! ( rng. next_u32( ) , x as u32 ) ;
956
1017
}
957
-
1018
+
958
1019
#[ test]
959
1020
fn test_fill_empty ( ) {
960
1021
let mut array = [ 0u32 ; 0 ] ;
@@ -1029,7 +1090,7 @@ mod test {
1029
1090
assert_eq ! ( r. gen_range( 0 , 1 ) , 0 ) ;
1030
1091
let _c: u8 = Standard . sample ( & mut r) ;
1031
1092
}
1032
-
1093
+
1033
1094
#[ test]
1034
1095
#[ cfg( feature="std" ) ]
1035
1096
fn test_random ( ) {
0 commit comments