Skip to content

No sizing #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions rand-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -43,8 +43,7 @@

use core::default::Default;
use core::convert::AsMut;

#[cfg(all(feature="alloc", not(feature="std")))] use alloc::boxed::Box;
use core::ops::DerefMut;

pub use error::{ErrorKind, Error};

@@ -262,30 +261,7 @@ pub trait SeedableRng: Sized {
}


impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
#[inline]
fn next_u32(&mut self) -> u32 {
(**self).next_u32()
}

#[inline]
fn next_u64(&mut self) -> u64 {
(**self).next_u64()
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
(**self).fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
(**self).try_fill_bytes(dest)
}
}

#[cfg(any(feature="std", feature="alloc"))]
impl<R: RngCore + ?Sized> RngCore for Box<R> {
impl<R: RngCore + ?Sized, T: DerefMut<Target = R>> RngCore for T {
#[inline]
fn next_u32(&mut self) -> u32 {
(**self).next_u32()
15 changes: 5 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -342,16 +342,11 @@ pub trait Rand : Sized {
/// ```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
@@ -378,7 +373,7 @@ pub trait Rand : Sized {
/// ```
///
/// [`RngCore`]: https://docs.rs/rand-core/0.1/rand-core/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.).
@@ -402,7 +397,7 @@ pub trait Rng: RngCore + Sized {
/// [`fill_bytes`]: https://docs.rs/rand-core/0.1/rand-core/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();
}
@@ -438,7 +433,7 @@ pub trait Rng: RngCore + Sized {
/// [`try_fill_bytes`]: https://docs.rs/rand-core/0.1/rand-core/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(())
@@ -455,7 +450,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)
}