Skip to content

Commit

Permalink
Rng: remove Sized constraint
Browse files Browse the repository at this point in the history
Suggested in #287 and appears to work
  • Loading branch information
dhardy committed Mar 9, 2018
1 parent f37e851 commit 1c009af
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,23 +341,6 @@ pub trait Rand : Sized {
/// generators, and is the "back end", to be implemented by generators.
/// End users should normally use [`Rng`] instead.
///
/// Unlike [`Rng`], this trait is object-safe. To use a type-erased [`Rng`] —
/// i.e. dynamic dispatch — this trait must be used, along with [`Rng`] to
/// use its generic functions:
///
/// ```
/// use rand::{Rng, RngCore};
///
/// fn use_rng(mut rng: &mut RngCore) -> u32 {
/// rng.gen_range(1, 7)
/// }
///
/// // or:
/// fn use_any_rng<R: RngCore>(rng: &mut R) -> char {
/// rng.gen()
/// }
/// ```
///
/// Several extension traits exist:
///
/// * [`Rng`] provides high-level functionality using generic functions
Expand Down Expand Up @@ -476,16 +459,11 @@ pub trait CryptoRng: RngCore {}
/// ```rust
/// use rand::Rng;
///
/// fn use_rng<R: Rng>(rng: &mut R) -> f32 {
/// fn use_rng<R: Rng + ?Sized>(rng: &mut R) -> f32 {
/// rng.gen()
/// }
/// ```
///
/// Since this trait exclusively uses generic methods, it is marked `Sized`.
/// Should it be necessary to support trait objects, use [`RngCore`].
/// Since `Rng` extends `RngCore` and every `RngCore` implements `Rng`, usage
/// of the two traits is somewhat interchangeable.
///
/// Iteration over an `Rng` can be achieved using `iter::repeat` as follows:
///
/// ```rust
Expand All @@ -512,7 +490,7 @@ pub trait CryptoRng: RngCore {}
/// ```
///
/// [`RngCore`]: trait.RngCore.html
pub trait Rng: RngCore + Sized {
pub trait Rng: RngCore {
/// Fill `dest` entirely with random bytes (uniform value distribution),
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
Expand All @@ -536,7 +514,7 @@ pub trait Rng: RngCore + Sized {
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes
/// [`try_fill`]: trait.Rng.html#method.try_fill
/// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) {
self.fill_bytes(dest.as_byte_slice_mut());
dest.to_le();
}
Expand Down Expand Up @@ -572,7 +550,7 @@ pub trait Rng: RngCore + Sized {
/// [`try_fill_bytes`]: trait.RngCore.html#method.try_fill_bytes
/// [`fill`]: trait.Rng.html#method.fill
/// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> {
self.try_fill_bytes(dest.as_byte_slice_mut())?;
dest.to_le();
Ok(())
Expand All @@ -589,7 +567,7 @@ pub trait Rng: RngCore + Sized {
/// let mut rng = thread_rng();
/// let x: i32 = rng.sample(Range::new(10, 15));
/// ```
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T where Self: Sized {
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T {
distr.sample(self)
}

Expand Down

0 comments on commit 1c009af

Please sign in to comment.