Skip to content

Commit 7dc370f

Browse files
committed
Make Rng::gen() and random() use Uniform.
This breaks rand_derive because gen() no longer supports derived types.
1 parent 18ae3fb commit 7dc370f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/distributions/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! bounds. Distributions use the `Distribution` trait to yield values: call
1616
//! `distr.sample(&mut rng)` to get a random variable.
1717
18-
use {Rng, Rand};
18+
use Rng;
1919

2020
pub use self::float::{Open01, Closed01};
2121
pub use self::range::Range;
@@ -180,7 +180,8 @@ impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D {
180180
#[derive(Debug)]
181181
pub struct Uniform;
182182

183-
impl<T> Rand for T where Uniform: Distribution<T> {
183+
#[allow(deprecated)]
184+
impl<T> ::Rand for T where Uniform: Distribution<T> {
184185
fn rand<R: Rng>(rng: &mut R) -> Self {
185186
Uniform.sample(rng)
186187
}

src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
//! Utilities for random number generation
1212
//!
1313
//! The key functions are `random()` and `Rng::gen()`. These are polymorphic and
14-
//! so can be used to generate any type that implements `Rand`. Type inference
14+
//! so can be used to generate any type supporting the [`Uniform`] distribution
15+
//! (i.e. `T` where `Uniform`: `Distribution<T>`). Type inference
1516
//! means that often a simple call to `rand::random()` or `rng.gen()` will
1617
//! suffice, but sometimes an annotation is required, e.g.
1718
//! `rand::random::<f64>()`.
@@ -236,6 +237,8 @@
236237
//! keep_wins as f32 / total_keeps as f32);
237238
//! }
238239
//! ```
240+
//!
241+
//! [`Uniform`]: distributions/struct.Uniform.html
239242
240243
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
241244
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
@@ -286,7 +289,7 @@ use prng::IsaacRng as IsaacWordRng;
286289
#[cfg(target_pointer_width = "64")]
287290
use prng::Isaac64Rng as IsaacWordRng;
288291

289-
use distributions::{Distribution, Range};
292+
use distributions::{Distribution, Uniform, Range};
290293
use distributions::range::SampleRange;
291294
#[cfg(feature="std")] use reseeding::ReseedingRng;
292295

@@ -321,6 +324,7 @@ mod prng;
321324
/// convenience and backwards-compatibility.
322325
///
323326
/// [`Uniform`]: distributions/struct.Uniform.html
327+
#[deprecated(since="0.5.0", note="replaced by distributions::Uniform")]
324328
pub trait Rand : Sized {
325329
/// Generates a random instance of this type using the specified source of
326330
/// randomness.
@@ -519,7 +523,9 @@ pub trait Rng: RngCore + Sized {
519523
distr.sample(self)
520524
}
521525

522-
/// Return a random value of a `Rand` type.
526+
/// Return a random value supporting the [`Uniform`] distribution.
527+
///
528+
/// [`Uniform`]: struct.Uniform.html
523529
///
524530
/// # Example
525531
///
@@ -532,8 +538,8 @@ pub trait Rng: RngCore + Sized {
532538
/// println!("{:?}", rng.gen::<(f64, bool)>());
533539
/// ```
534540
#[inline(always)]
535-
fn gen<T: Rand>(&mut self) -> T {
536-
Rand::rand(self)
541+
fn gen<T>(&mut self) -> T where Uniform: Distribution<T> {
542+
Uniform.sample(self)
537543
}
538544

539545
/// Return an iterator that will yield an infinite number of randomly
@@ -550,7 +556,7 @@ pub trait Rng: RngCore + Sized {
550556
/// println!("{:?}", rng.gen_iter::<(f64, bool)>().take(5)
551557
/// .collect::<Vec<(f64, bool)>>());
552558
/// ```
553-
fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self> {
559+
fn gen_iter<'a, T>(&'a mut self) -> Generator<'a, T, Self> where Uniform: Distribution<T> {
554560
Generator { rng: self, _marker: marker::PhantomData }
555561
}
556562

@@ -752,7 +758,7 @@ pub struct Generator<'a, T, R:'a> {
752758
_marker: marker::PhantomData<fn() -> T>,
753759
}
754760

755-
impl<'a, T: Rand, R: RngCore> Iterator for Generator<'a, T, R> {
761+
impl<'a, T, R: RngCore> Iterator for Generator<'a, T, R> where Uniform: Distribution<T> {
756762
type Item = T;
757763

758764
fn next(&mut self) -> Option<T> {
@@ -1203,7 +1209,7 @@ impl RngCore for EntropyRng {
12031209
/// [`Rand`]: trait.Rand.html
12041210
#[cfg(feature="std")]
12051211
#[inline]
1206-
pub fn random<T: Rand>() -> T {
1212+
pub fn random<T>() -> T where Uniform: Distribution<T> {
12071213
thread_rng().gen()
12081214
}
12091215

0 commit comments

Comments
 (0)