@@ -237,6 +237,7 @@ use std::mem;
237
237
use std:: io;
238
238
use std:: rc:: Rc ;
239
239
use std:: num:: wrapping:: Wrapping as w;
240
+ use std:: num:: Int ;
240
241
241
242
pub use os:: OsRng ;
242
243
@@ -416,6 +417,14 @@ pub trait Rng : Sized {
416
417
Generator { rng : self , _marker : marker:: PhantomData }
417
418
}
418
419
420
+ fn sample < ' a , T , D : Distribution < T > > ( & ' a mut self , distribution : & ' a D ) -> T {
421
+ distribution. sample ( self )
422
+ }
423
+
424
+ fn sample_iter < ' a , T , D : Distribution < T > > ( & ' a mut self , distribution : & ' a D ) -> SampleIter < ' a , Self , D , T > {
425
+ SampleIter { rng : self , distribution : distribution, _marker : marker:: PhantomData }
426
+ }
427
+
419
428
/// Generate a random value in the range [`low`, `high`).
420
429
///
421
430
/// This is a convenience wrapper around
@@ -535,6 +544,24 @@ impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> {
535
544
}
536
545
}
537
546
547
+ pub struct SampleIter < ' a , R : ' a , D : ' a , T > {
548
+ rng : & ' a mut R ,
549
+ distribution : & ' a D ,
550
+ _marker : marker:: PhantomData < fn ( ) -> T > ,
551
+ }
552
+
553
+ impl < ' a , R : Rng , D : Distribution < T > , T > Iterator for SampleIter < ' a , R , D , T > {
554
+ type Item = T ;
555
+
556
+ fn next ( & mut self ) -> Option < T > {
557
+ Some ( self . distribution . sample ( self . rng ) )
558
+ }
559
+
560
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
561
+ ( Int :: max_value ( ) , None )
562
+ }
563
+ }
564
+
538
565
/// Iterator which will continuously generate random ascii characters.
539
566
///
540
567
/// This iterator is created via the `gen_ascii_chars` method on `Rng`.
0 commit comments