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,68 @@ 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
+
892
+ // `extern { type Foo; }` isn't supported on 1.22 syntactically, so use a
893
+ // macro to work around that.
894
+ macro_rules! rust_122_compat {
895
+ ( $( $t: tt) * ) => ( $( $t) * )
896
+ }
897
+
898
+ rust_122_compat ! {
899
+ extern crate wasm_bindgen;
900
+
901
+ pub use wasm_bindgen:: prelude:: * ;
902
+
903
+ #[ wasm_bindgen]
904
+ extern {
905
+ pub type This ;
906
+ pub static this: This ;
907
+
908
+ #[ wasm_bindgen( method, getter, structural, js_name = self ) ]
909
+ pub fn self_( me: & This ) -> JsValue ;
910
+ #[ wasm_bindgen( method, getter, structural) ]
911
+ pub fn crypto( me: & This ) -> JsValue ;
912
+
913
+ pub type BrowserCrypto ;
914
+
915
+ // TODO: these `structural` annotations here ideally wouldn't be here to
916
+ // avoid a JS shim, but for now with feature detection they're
917
+ // unavoidable.
918
+ #[ wasm_bindgen( method, js_name = getRandomValues, structural, getter) ]
919
+ pub fn get_random_values_fn( me: & BrowserCrypto ) -> JsValue ;
920
+ #[ wasm_bindgen( method, js_name = getRandomValues, structural) ]
921
+ pub fn get_random_values( me: & BrowserCrypto , buf: & mut [ u8 ] ) ;
922
+
923
+ #[ wasm_bindgen( js_name = require) ]
924
+ pub fn node_require( s: & str ) -> NodeCrypto ;
925
+
926
+ pub type NodeCrypto ;
927
+
928
+ #[ wasm_bindgen( method, js_name = randomFillSync, structural) ]
929
+ pub fn random_fill_sync( me: & NodeCrypto , buf: & mut [ u8 ] ) ;
930
+ }
931
+
932
+ // TODO: replace with derive once rustwasm/wasm-bindgen#400 is merged
933
+ impl Clone for BrowserCrypto {
934
+ fn clone( & self ) -> BrowserCrypto {
935
+ BrowserCrypto { obj: self . obj. clone( ) }
936
+ }
937
+ }
938
+
939
+ impl Clone for NodeCrypto {
940
+ fn clone( & self ) -> NodeCrypto {
941
+ NodeCrypto { obj: self . obj. clone( ) }
942
+ }
943
+ }
944
+ }
945
+ }
946
+
877
947
#[ cfg( test) ]
878
948
mod test {
879
949
use rngs:: mock:: StepRng ;
@@ -936,25 +1006,25 @@ mod test {
936
1006
}
937
1007
}
938
1008
}
939
-
1009
+
940
1010
#[ test]
941
1011
fn test_fill ( ) {
942
1012
let x = 9041086907909331047 ; // a random u64
943
1013
let mut rng = StepRng :: new ( x, 0 ) ;
944
-
1014
+
945
1015
// Convert to byte sequence and back to u64; byte-swap twice if BE.
946
1016
let mut array = [ 0u64 ; 2 ] ;
947
1017
rng. fill ( & mut array[ ..] ) ;
948
1018
assert_eq ! ( array, [ x, x] ) ;
949
1019
assert_eq ! ( rng. next_u64( ) , x) ;
950
-
1020
+
951
1021
// Convert to bytes then u32 in LE order
952
1022
let mut array = [ 0u32 ; 2 ] ;
953
1023
rng. fill ( & mut array[ ..] ) ;
954
1024
assert_eq ! ( array, [ x as u32 , ( x >> 32 ) as u32 ] ) ;
955
1025
assert_eq ! ( rng. next_u32( ) , x as u32 ) ;
956
1026
}
957
-
1027
+
958
1028
#[ test]
959
1029
fn test_fill_empty ( ) {
960
1030
let mut array = [ 0u32 ; 0 ] ;
@@ -1029,7 +1099,7 @@ mod test {
1029
1099
assert_eq ! ( r. gen_range( 0 , 1 ) , 0 ) ;
1030
1100
let _c: u8 = Standard . sample ( & mut r) ;
1031
1101
}
1032
-
1102
+
1033
1103
#[ test]
1034
1104
#[ cfg( feature="std" ) ]
1035
1105
fn test_random ( ) {
0 commit comments