From b9d885ede4379ed56cc61beb4579339f7d5b439b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 9 Mar 2018 11:08:15 +0000 Subject: [PATCH 1/2] Rng: remove Sized constraint Suggested in #287 and appears to work --- src/lib.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 928129e5c2a..c44d129aad4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,16 +342,11 @@ pub trait Rand : Sized { /// ```rust /// use rand::Rng; /// -/// fn use_rng(rng: &mut R) -> f32 { +/// fn use_rng(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(&mut self, dest: &mut T) where Self: Sized { + fn fill(&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(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized { + fn try_fill(&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>(&mut self, distr: D) -> T where Self: Sized { + fn sample>(&mut self, distr: D) -> T { distr.sample(self) } From 5ac9f5499e3f3a13b751fc8696648b4e5a7610d8 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 9 Mar 2018 10:21:46 +0000 Subject: [PATCH 2/2] Use DerefMut instead of direct Box/&mut impls --- rand-core/src/lib.rs | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/rand-core/src/lib.rs b/rand-core/src/lib.rs index c29a00573fd..15ff848f040 100644 --- a/rand-core/src/lib.rs +++ b/rand-core/src/lib.rs @@ -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 RngCore for Box { +impl> RngCore for T { #[inline] fn next_u32(&mut self) -> u32 { (**self).next_u32()