diff --git a/README.md b/README.md index c4704f3dc44..21064b3e482 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A Rust library for random number generation, featuring: -- Easy random value generation and usage via the [`Rng`](https://docs.rs/rand/*/rand/trait.Rng.html), +- Easy random value generation and usage via the [`RngExt`](https://docs.rs/rand/*/rand/trait.RngExt.html), [`SliceRandom`](https://docs.rs/rand/*/rand/seq/trait.SliceRandom.html) and [`IteratorRandom`](https://docs.rs/rand/*/rand/seq/trait.IteratorRandom.html) traits - Secure seeding via the [`getrandom` crate](https://crates.io/crates/getrandom) @@ -82,7 +82,7 @@ and breaking releases are infrequent. Rand libs have inter-dependencies and make use of the [semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits -compatible across crate versions. (This is especially important for `RngCore` +compatible across crate versions. (This is especially important for `Rng` and `SeedableRng`.) A few crate releases are thus compatibility shims, depending on the *next* lib version (e.g. `rand_core` versions `0.2.2` and `0.3.1`). This means, for example, that `rand_core_0_4_0::SeedableRng` and diff --git a/benches/seq_choose.rs b/benches/seq_choose.rs index 2c34d77ced7..965878ac222 100644 --- a/benches/seq_choose.rs +++ b/benches/seq_choose.rs @@ -22,12 +22,12 @@ pub fn bench(c: &mut Criterion) { bench_rng::(c, "Pcg64"); } -fn bench_rng(c: &mut Criterion, rng_name: &'static str) { +fn bench_rng(c: &mut Criterion, rng_name: &'static str) { for length in [1, 2, 3, 10, 100, 1000].map(|x| black_box(x)) { c.bench_function( format!("choose_size-hinted_from_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); b.iter(|| choose_size_hinted(length, &mut rng)) }, ); @@ -35,7 +35,7 @@ fn bench_rng(c: &mut Criterion, rng_name: &'static s c.bench_function( format!("choose_stable_from_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); b.iter(|| choose_stable(length, &mut rng)) }, ); @@ -43,7 +43,7 @@ fn bench_rng(c: &mut Criterion, rng_name: &'static s c.bench_function( format!("choose_unhinted_from_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); b.iter(|| choose_unhinted(length, &mut rng)) }, ); @@ -51,7 +51,7 @@ fn bench_rng(c: &mut Criterion, rng_name: &'static s c.bench_function( format!("choose_windowed_from_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); b.iter(|| choose_windowed(length, 7, &mut rng)) }, ); diff --git a/benches/shuffle.rs b/benches/shuffle.rs index 3d6878219f7..9e5eb387b31 100644 --- a/benches/shuffle.rs +++ b/benches/shuffle.rs @@ -22,10 +22,10 @@ pub fn bench(c: &mut Criterion) { bench_rng::(c, "Pcg64"); } -fn bench_rng(c: &mut Criterion, rng_name: &'static str) { +fn bench_rng(c: &mut Criterion, rng_name: &'static str) { for length in [1, 2, 3, 10, 100, 1000, 10000].map(|x| black_box(x)) { c.bench_function(format!("shuffle_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); let mut vec: Vec = (0..length).collect(); b.iter(|| { vec.shuffle(&mut rng); @@ -37,7 +37,7 @@ fn bench_rng(c: &mut Criterion, rng_name: &'static s c.bench_function( format!("partial_shuffle_{length}_{rng_name}").as_str(), |b| { - let mut rng = Rng::seed_from_u64(123); + let mut rng = R::seed_from_u64(123); let mut vec: Vec = (0..length).collect(); b.iter(|| { vec.partial_shuffle(&mut rng, length / 2); diff --git a/benches/weighted.rs b/benches/weighted.rs index 68722908a9e..d0226d99f6f 100644 --- a/benches/weighted.rs +++ b/benches/weighted.rs @@ -11,7 +11,7 @@ extern crate test; use rand::distributions::WeightedIndex; -use rand::Rng; +use rand::RngExt; use test::Bencher; #[bench] diff --git a/examples/monty-hall.rs b/examples/monty-hall.rs index 23e11178969..167dd3f0bcc 100644 --- a/examples/monty-hall.rs +++ b/examples/monty-hall.rs @@ -29,7 +29,7 @@ #![cfg(all(feature = "std", feature = "std_rng"))] use rand::distributions::{Distribution, Uniform}; -use rand::Rng; +use rand::{Rng, RngExt}; struct SimulationResult { win: bool, diff --git a/examples/rayon-monte-carlo.rs b/examples/rayon-monte-carlo.rs index 7e703c01d2d..eaf9a9a0c2b 100644 --- a/examples/rayon-monte-carlo.rs +++ b/examples/rayon-monte-carlo.rs @@ -34,7 +34,7 @@ //! //! Instead, we do our own batching, so that a Rayon work item becomes a //! batch. Then we can fix our rng stream to the batched work item. -//! Batching amortizes the cost of constructing the Rng from a fixed seed +//! Batching amortizes the cost of constructing the RNG from a fixed seed //! over BATCH_SIZE trials. Manually batching also turns out to be faster //! for the nondeterministic version of this program as well. diff --git a/rand_chacha/src/chacha.rs b/rand_chacha/src/chacha.rs index ebc28a8ab04..c6d6f1e71d5 100644 --- a/rand_chacha/src/chacha.rs +++ b/rand_chacha/src/chacha.rs @@ -14,7 +14,7 @@ use self::core::fmt; use crate::guts::ChaCha; use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng}; -use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; +use rand_core::{CryptoRng, Error, Rng, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer}; @@ -132,7 +132,7 @@ macro_rules! chacha_impl { /// ``` /// /// This implementation uses an output buffer of sixteen `u32` words, and uses - /// [`BlockRng`] to implement the [`RngCore`] methods. + /// [`BlockRng`] to implement the [`Rng`] methods. /// /// [^1]: D. J. Bernstein, [*ChaCha, a variant of Salsa20*]( /// https://cr.yp.to/chacha.html) @@ -155,7 +155,7 @@ macro_rules! chacha_impl { } } - impl RngCore for $ChaChaXRng { + impl Rng for $ChaChaXRng { #[inline] fn next_u32(&mut self) -> u32 { self.rng.next_u32() @@ -340,7 +340,7 @@ chacha_impl!(ChaCha8Core, ChaCha8Rng, 4, "ChaCha with 8 rounds", abstract8); #[cfg(test)] mod test { - use rand_core::{RngCore, SeedableRng}; + use rand_core::{Rng, SeedableRng}; #[cfg(feature = "serde1")] use super::{ChaCha20Rng, ChaCha12Rng, ChaCha8Rng}; diff --git a/rand_core/README.md b/rand_core/README.md index 4174ff19484..54b67a40489 100644 --- a/rand_core/README.md +++ b/rand_core/README.md @@ -10,12 +10,12 @@ Core traits and error types of the [rand] library, plus tools for implementing RNGs. -This crate is intended for use when implementing the core trait, `RngCore`; it +This crate is intended for use when implementing the core trait, `Rng`; it defines the core traits to be implemented as well as several small functions to aid in their implementation and types required for error handling. The main [rand] crate re-exports most items defined in this crate, along with -tools to convert the integer samples generated by `RngCore` to many different +tools to convert the integer samples generated by `Rng` to many different applications (including sampling from restricted ranges, conversion to floating point, list permutations and secure initialisation of RNGs). Most users should prefer to use the main [rand] crate. @@ -48,7 +48,7 @@ rand_core = "0.6.4" Rand libs have inter-dependencies and make use of the [semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits -compatible across crate versions. (This is especially important for `RngCore` +compatible across crate versions. (This is especially important for `Rng` and `SeedableRng`.) A few crate releases are thus compatibility shims, depending on the *next* lib version (e.g. `rand_core` versions `0.2.2` and `0.3.1`). This means, for example, that `rand_core_0_4_0::SeedableRng` and @@ -59,8 +59,8 @@ cause build errors. Usually, running `cargo update` is enough to fix any issues. `rand_core` supports `no_std` and `alloc`-only configurations, as well as full `std` functionality. The differences between `no_std` and full `std` are small, -comprising `RngCore` support for `Box` types where `R: RngCore`, -`std::io::Read` support for types supporting `RngCore`, and +comprising `Rng` support for `Box` types where `R: Rng`, +`std::io::Read` support for types supporting `Rng`, and extensions to the `Error` type's functionality. The `std` feature is *not enabled by default*. This is primarily to avoid build diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 5ad459d0fb4..3e323dc331f 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -14,7 +14,7 @@ //! //! Usage of this trait is optional, but provides two advantages: //! implementations only need to concern themselves with generation of the -//! block, not the various [`RngCore`] methods (especially [`fill_bytes`], where +//! block, not the various [`Rng`] methods (especially [`fill_bytes`], where //! the optimal implementations are not trivial), and this allows //! `ReseedingRng` (see [`rand`](https://docs.rs/rand) crate) perform periodic //! reseeding with very low overhead. @@ -22,7 +22,7 @@ //! # Example //! //! ```no_run -//! use rand_core::{RngCore, SeedableRng}; +//! use rand_core::{Rng, SeedableRng}; //! use rand_core::block::{BlockRngCore, BlockRng}; //! //! struct MyRngCore; @@ -51,10 +51,10 @@ //! ``` //! //! [`BlockRngCore`]: crate::block::BlockRngCore -//! [`fill_bytes`]: RngCore::fill_bytes +//! [`fill_bytes`]: Rng::fill_bytes use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks}; -use crate::{Error, CryptoRng, RngCore, SeedableRng}; +use crate::{Error, CryptoRng, Rng, SeedableRng}; use core::convert::AsRef; use core::fmt; #[cfg(feature = "serde1")] @@ -77,13 +77,13 @@ pub trait BlockRngCore { fn generate(&mut self, results: &mut Self::Results); } -/// A marker trait used to indicate that an [`RngCore`] implementation is +/// A marker trait used to indicate that an [`Rng`] implementation is /// supposed to be cryptographically secure. /// /// See [`CryptoRng`][crate::CryptoRng] docs for more information. pub trait CryptoBlockRng: BlockRngCore { } -/// A wrapper type implementing [`RngCore`] for some type implementing +/// A wrapper type implementing [`Rng`] for some type implementing /// [`BlockRngCore`] with `u32` array buffer; i.e. this can be used to implement /// a full RNG from just a `generate` function. /// @@ -91,11 +91,11 @@ pub trait CryptoBlockRng: BlockRngCore { } /// PRNG implementations can simply use a type alias /// (`pub type MyRng = BlockRng;`) but might prefer to use a /// wrapper type (`pub struct MyRng(BlockRng);`); the latter must -/// re-implement `RngCore` but hides the implementation details and allows +/// re-implement `Rng` but hides the implementation details and allows /// extra functionality to be defined on the RNG /// (e.g. `impl MyRng { fn set_stream(...){...} }`). /// -/// `BlockRng` has heavily optimized implementations of the [`RngCore`] methods +/// `BlockRng` has heavily optimized implementations of the [`Rng`] methods /// reading values from the results buffer, as well as /// calling [`BlockRngCore::generate`] directly on the output array when /// [`fill_bytes`] / [`try_fill_bytes`] is called on a large array. These methods @@ -114,10 +114,10 @@ pub trait CryptoBlockRng: BlockRngCore { } /// /// For easy initialization `BlockRng` also implements [`SeedableRng`]. /// -/// [`next_u32`]: RngCore::next_u32 -/// [`next_u64`]: RngCore::next_u64 -/// [`fill_bytes`]: RngCore::fill_bytes -/// [`try_fill_bytes`]: RngCore::try_fill_bytes +/// [`next_u32`]: Rng::next_u32 +/// [`next_u64`]: Rng::next_u64 +/// [`fill_bytes`]: Rng::fill_bytes +/// [`try_fill_bytes`]: Rng::try_fill_bytes #[derive(Clone)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] #[cfg_attr( @@ -184,7 +184,7 @@ impl BlockRng { } } -impl> RngCore for BlockRng { +impl> Rng for BlockRng { #[inline] fn next_u32(&mut self) -> u32 { if self.index >= self.results.as_ref().len() { @@ -257,14 +257,14 @@ impl SeedableRng for BlockRng { } #[inline(always)] - fn from_rng(rng: S) -> Result { + fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } } impl> CryptoRng for BlockRng {} -/// A wrapper type implementing [`RngCore`] for some type implementing +/// A wrapper type implementing [`Rng`] for some type implementing /// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement /// a full RNG from just a `generate` function. /// @@ -282,10 +282,10 @@ impl> CryptoRng for BlockRng {} /// values. If the requested length is not a multiple of 8, some bytes will be /// discarded. /// -/// [`next_u32`]: RngCore::next_u32 -/// [`next_u64`]: RngCore::next_u64 -/// [`fill_bytes`]: RngCore::fill_bytes -/// [`try_fill_bytes`]: RngCore::try_fill_bytes +/// [`next_u32`]: Rng::next_u32 +/// [`next_u64`]: Rng::next_u64 +/// [`fill_bytes`]: Rng::fill_bytes +/// [`try_fill_bytes`]: Rng::try_fill_bytes #[derive(Clone)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct BlockRng64 { @@ -351,7 +351,7 @@ impl BlockRng64 { } } -impl> RngCore for BlockRng64 { +impl> Rng for BlockRng64 { #[inline] fn next_u32(&mut self) -> u32 { let mut index = self.index - self.half_used as usize; @@ -425,7 +425,7 @@ impl SeedableRng for BlockRng64 { } #[inline(always)] - fn from_rng(rng: S) -> Result { + fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } } @@ -434,7 +434,7 @@ impl> CryptoRng for BlockRng64 { #[cfg(test)] mod test { - use crate::{SeedableRng, RngCore}; + use crate::{SeedableRng, Rng}; use crate::block::{BlockRng, BlockRng64, BlockRngCore}; #[derive(Debug, Clone)] diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 8f99ef813a5..7c2986c9faa 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Helper functions for implementing `RngCore` functions. +//! Helper functions for implementing `Rng` functions. //! //! For cross-platform reproducibility, these functions all use Little Endian: //! least-significant part first. For example, `next_u64_via_u32` takes `u32` @@ -17,11 +17,11 @@ //! to/from byte sequences, and since its purpose is reproducibility, //! non-reproducible sources (e.g. `OsRng`) need not bother with it. -use crate::RngCore; +use crate::Rng; use core::cmp::min; /// Implement `next_u64` via `next_u32`, little-endian order. -pub fn next_u64_via_u32(rng: &mut R) -> u64 { +pub fn next_u64_via_u32(rng: &mut R) -> u64 { // Use LE; we explicitly generate one value before the next. let x = u64::from(rng.next_u32()); let y = u64::from(rng.next_u32()); @@ -34,7 +34,7 @@ pub fn next_u64_via_u32(rng: &mut R) -> u64 { /// integers. That is why this method mostly uses `next_u64`, and only when /// there are 4 or less bytes remaining at the end of the slice it uses /// `next_u32` once. -pub fn fill_bytes_via_next(rng: &mut R, dest: &mut [u8]) { +pub fn fill_bytes_via_next(rng: &mut R, dest: &mut [u8]) { let mut left = dest; while left.len() >= 8 { let (l, r) = { left }.split_at_mut(8); @@ -159,14 +159,14 @@ pub fn fill_via_u64_chunks(src: &mut [u64], dest: &mut [u8]) -> (usize, usize) { } /// Implement `next_u32` via `fill_bytes`, little-endian order. -pub fn next_u32_via_fill(rng: &mut R) -> u32 { +pub fn next_u32_via_fill(rng: &mut R) -> u32 { let mut buf = [0; 4]; rng.fill_bytes(&mut buf); u32::from_le_bytes(buf) } /// Implement `next_u64` via `fill_bytes`, little-endian order. -pub fn next_u64_via_fill(rng: &mut R) -> u64 { +pub fn next_u64_via_fill(rng: &mut R) -> u64 { let mut buf = [0; 8]; rng.fill_bytes(&mut buf); u64::from_le_bytes(buf) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 9a6c0baa13f..615093f3dd7 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -10,10 +10,10 @@ //! Random number generation traits //! //! This crate is mainly of interest to crates publishing implementations of -//! [`RngCore`]. Other users are encouraged to use the [`rand`] crate instead +//! [`Rng`]. Other users are encouraged to use the [`rand`] crate instead //! which re-exports the main traits and error types. //! -//! [`RngCore`] is the core trait implemented by algorithmic pseudo-random number +//! [`Rng`] is the core trait implemented by algorithmic pseudo-random number //! generators and external random-number sources. //! //! [`SeedableRng`] is an extension trait for construction from fixed seeds and @@ -23,7 +23,7 @@ //! environments. //! //! The [`impls`] and [`le`] sub-modules include a few small functions to assist -//! implementation of [`RngCore`]. +//! implementation of [`Rng`]. //! //! [`rand`]: https://docs.rs/rand @@ -61,7 +61,7 @@ pub mod le; /// This trait encapsulates the low-level functionality common to all /// generators, and is the "back end", to be implemented by generators. /// End users should normally use the `Rng` trait from the [`rand`] crate, -/// which is automatically implemented for every type implementing `RngCore`. +/// which is automatically implemented for every type implementing `Rng`. /// /// Three different methods for generating random data are provided since the /// optimal implementation of each is dependent on the type of generator. There @@ -110,11 +110,11 @@ pub mod le; /// /// ``` /// #![allow(dead_code)] -/// use rand_core::{RngCore, Error, impls}; +/// use rand_core::{Rng, Error, impls}; /// /// struct CountingRng(u64); /// -/// impl RngCore for CountingRng { +/// impl Rng for CountingRng { /// fn next_u32(&mut self) -> u32 { /// self.next_u64() as u32 /// } @@ -135,11 +135,11 @@ pub mod le; /// ``` /// /// [`rand`]: https://docs.rs/rand -/// [`try_fill_bytes`]: RngCore::try_fill_bytes -/// [`fill_bytes`]: RngCore::fill_bytes -/// [`next_u32`]: RngCore::next_u32 -/// [`next_u64`]: RngCore::next_u64 -pub trait RngCore { +/// [`try_fill_bytes`]: Rng::try_fill_bytes +/// [`fill_bytes`]: Rng::fill_bytes +/// [`next_u32`]: Rng::next_u32 +/// [`next_u64`]: Rng::next_u64 +pub trait Rng { /// Return the next random `u32`. /// /// RNGs must implement at least one method from this trait directly. In @@ -159,7 +159,7 @@ pub trait RngCore { /// RNGs must implement at least one method from this trait directly. In /// the case this method is not implemented directly, it can be implemented /// via [`impls::fill_bytes_via_next`] or - /// via [`RngCore::try_fill_bytes`]; if this generator can + /// via [`Rng::try_fill_bytes`]; if this generator can /// fail the implementation must choose how best to handle errors here /// (e.g. panic with a descriptive message or log a warning and retry a few /// times). @@ -177,13 +177,13 @@ pub trait RngCore { /// by external (true) RNGs (e.g. `OsRng`) which can fail. It may be used /// directly to generate keys and to seed (infallible) PRNGs. /// - /// Other than error handling, this method is identical to [`RngCore::fill_bytes`]; + /// Other than error handling, this method is identical to [`Rng::fill_bytes`]; /// thus this may be implemented using `Ok(self.fill_bytes(dest))` or /// `fill_bytes` may be implemented with /// `self.try_fill_bytes(dest).unwrap()` or more specific error handling. fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>; - /// Convert an [`RngCore`] to a [`RngReadAdapter`]. + /// Convert an [`Rng`] to a [`RngReadAdapter`]. #[cfg(feature = "std")] fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> where Self: Sized { @@ -191,7 +191,7 @@ pub trait RngCore { } } -/// A marker trait used to indicate that an [`RngCore`] implementation is +/// A marker trait used to indicate that an [`Rng`] implementation is /// supposed to be cryptographically secure. /// /// *Cryptographically secure generators*, also known as *CSPRNGs*, should @@ -213,7 +213,7 @@ pub trait RngCore { /// weaknesses such as seeding from a weak entropy source or leaking state. /// /// [`BlockRngCore`]: block::BlockRngCore -pub trait CryptoRng: RngCore {} +pub trait CryptoRng: Rng {} /// A random number generator that can be explicitly seeded. /// @@ -365,7 +365,7 @@ pub trait SeedableRng: Sized { /// (in prior versions this was not required). /// /// [`rand`]: https://docs.rs/rand - fn from_rng(mut rng: R) -> Result { + fn from_rng(mut rng: R) -> Result { let mut seed = Self::Seed::default(); rng.try_fill_bytes(seed.as_mut())?; Ok(Self::from_seed(seed)) @@ -396,10 +396,10 @@ pub trait SeedableRng: Sized { } } -// Implement `RngCore` for references to an `RngCore`. -// Force inlining all functions, so that it is up to the `RngCore` +// Implement `Rng` for references to an `Rng`. +// Force inlining all functions, so that it is up to the `Rng` // implementation and the optimizer to decide on inlining. -impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R { +impl<'a, R: Rng + ?Sized> Rng for &'a mut R { #[inline(always)] fn next_u32(&mut self) -> u32 { (**self).next_u32() @@ -421,11 +421,11 @@ impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R { } } -// Implement `RngCore` for boxed references to an `RngCore`. -// Force inlining all functions, so that it is up to the `RngCore` +// Implement `Rng` for boxed references to an `Rng`. +// Force inlining all functions, so that it is up to the `Rng` // implementation and the optimizer to decide on inlining. #[cfg(feature = "alloc")] -impl RngCore for Box { +impl Rng for Box { #[inline(always)] fn next_u32(&mut self) -> u32 { (**self).next_u32() @@ -447,24 +447,24 @@ impl RngCore for Box { } } -/// Adapter that enables reading through a [`io::Read`](std::io::Read) from a [`RngCore`]. +/// Adapter that enables reading through a [`io::Read`](std::io::Read) from a [`Rng`]. /// /// # Examples /// /// ```rust /// # use std::{io, io::Read}; /// # use std::fs::File; -/// # use rand_core::{OsRng, RngCore}; +/// # use rand_core::{OsRng, Rng}; /// /// io::copy(&mut OsRng.read_adapter().take(100), &mut File::create("/tmp/random.bytes").unwrap()).unwrap(); /// ``` #[cfg(feature = "std")] -pub struct RngReadAdapter<'a, R: RngCore + ?Sized> { +pub struct RngReadAdapter<'a, R: Rng + ?Sized> { inner: &'a mut R, } #[cfg(feature = "std")] -impl std::io::Read for RngReadAdapter<'_, R> { +impl std::io::Read for RngReadAdapter<'_, R> { fn read(&mut self, buf: &mut [u8]) -> Result { self.inner.try_fill_bytes(buf)?; Ok(buf.len()) @@ -472,7 +472,7 @@ impl std::io::Read for RngReadAdapter<'_, R> { } #[cfg(feature = "std")] -impl std::fmt::Debug for RngReadAdapter<'_, R> { +impl std::fmt::Debug for RngReadAdapter<'_, R> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ReadAdapter").finish() } diff --git a/rand_core/src/os.rs b/rand_core/src/os.rs index b43c9fdaf05..a30c86f7c45 100644 --- a/rand_core/src/os.rs +++ b/rand_core/src/os.rs @@ -8,7 +8,7 @@ //! Interface to the random number generator of the operating system. -use crate::{impls, CryptoRng, Error, RngCore}; +use crate::{impls, CryptoRng, Error, Rng}; use getrandom::getrandom; /// A random number generator that retrieves randomness from the @@ -36,7 +36,7 @@ use getrandom::getrandom; /// /// # Usage example /// ``` -/// use rand_core::{RngCore, OsRng}; +/// use rand_core::{Rng, OsRng}; /// /// let mut key = [0u8; 16]; /// OsRng.fill_bytes(&mut key); @@ -50,7 +50,7 @@ pub struct OsRng; impl CryptoRng for OsRng {} -impl RngCore for OsRng { +impl Rng for OsRng { fn next_u32(&mut self) -> u32 { impls::next_u32_via_fill(self) } diff --git a/rand_distr/src/binomial.rs b/rand_distr/src/binomial.rs index 0f49806aba1..3f2398a6ff3 100644 --- a/rand_distr/src/binomial.rs +++ b/rand_distr/src/binomial.rs @@ -10,7 +10,7 @@ //! The binomial distribution. use crate::{Distribution, Uniform}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; use core::cmp::Ordering; #[allow(unused_imports)] @@ -304,7 +304,7 @@ impl Distribution for Binomial { mod test { use super::Binomial; use crate::Distribution; - use rand::Rng; + use rand::{Rng, RngExt}; fn test_binomial_mean_and_variance(n: u64, p: f64, rng: &mut R) { let binomial = Binomial::new(n, p).unwrap(); diff --git a/rand_distr/src/cauchy.rs b/rand_distr/src/cauchy.rs index 9aff7e625f4..bf7792192ce 100644 --- a/rand_distr/src/cauchy.rs +++ b/rand_distr/src/cauchy.rs @@ -89,6 +89,7 @@ where F: Float + FloatConst, Standard: Distribution #[cfg(test)] mod test { use super::*; + use rand::RngExt; fn median(numbers: &mut [f64]) -> f64 { sort(numbers); diff --git a/rand_distr/src/exponential.rs b/rand_distr/src/exponential.rs index e3d2a8d1cf6..3ae73b637e4 100644 --- a/rand_distr/src/exponential.rs +++ b/rand_distr/src/exponential.rs @@ -12,7 +12,7 @@ use crate::utils::ziggurat; use num_traits::Float; use crate::{ziggurat_tables, Distribution}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// Samples floating-point numbers according to the exponential distribution, diff --git a/rand_distr/src/frechet.rs b/rand_distr/src/frechet.rs index 63205b40cbd..cd31f53402f 100644 --- a/rand_distr/src/frechet.rs +++ b/rand_distr/src/frechet.rs @@ -11,7 +11,7 @@ use crate::{Distribution, OpenClosed01}; use core::fmt; use num_traits::Float; -use rand::Rng; +use rand::{Rng, RngExt}; /// Samples floating-point numbers according to the Fréchet distribution /// diff --git a/rand_distr/src/gamma.rs b/rand_distr/src/gamma.rs index 1a575bd6a9f..cae9265b6d3 100644 --- a/rand_distr/src/gamma.rs +++ b/rand_distr/src/gamma.rs @@ -19,7 +19,7 @@ use self::GammaRepr::*; use crate::normal::StandardNormal; use num_traits::Float; use crate::{Distribution, Exp, Exp1, Open01}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; diff --git a/rand_distr/src/geometric.rs b/rand_distr/src/geometric.rs index 3ea8b8f3e13..4c5ea64b547 100644 --- a/rand_distr/src/geometric.rs +++ b/rand_distr/src/geometric.rs @@ -1,7 +1,7 @@ //! The geometric distribution. use crate::Distribution; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; #[allow(unused_imports)] use num_traits::Float; @@ -143,7 +143,7 @@ impl Distribution for Geometric /// /// See [`Geometric`](crate::Geometric) for the general geometric distribution. /// -/// Implemented via iterated [Rng::gen::().leading_zeros()]. +/// Implemented via iterated [`RngExt::gen::().leading_zeros()`]. /// /// # Example /// ``` diff --git a/rand_distr/src/gumbel.rs b/rand_distr/src/gumbel.rs index b254919f3b8..50e8227e767 100644 --- a/rand_distr/src/gumbel.rs +++ b/rand_distr/src/gumbel.rs @@ -11,7 +11,7 @@ use crate::{Distribution, OpenClosed01}; use core::fmt; use num_traits::Float; -use rand::Rng; +use rand::{Rng, RngExt}; /// Samples floating-point numbers according to the Gumbel distribution /// diff --git a/rand_distr/src/hypergeometric.rs b/rand_distr/src/hypergeometric.rs index a42a572d8d1..3c0c33da268 100644 --- a/rand_distr/src/hypergeometric.rs +++ b/rand_distr/src/hypergeometric.rs @@ -1,7 +1,7 @@ //! The hypergeometric distribution. use crate::Distribution; -use rand::Rng; +use rand::{Rng, RngExt}; use rand::distributions::uniform::Uniform; use core::fmt; #[allow(unused_imports)] diff --git a/rand_distr/src/inverse_gaussian.rs b/rand_distr/src/inverse_gaussian.rs index ba845fd1505..9bde531a31f 100644 --- a/rand_distr/src/inverse_gaussian.rs +++ b/rand_distr/src/inverse_gaussian.rs @@ -1,6 +1,6 @@ use crate::{Distribution, Standard, StandardNormal}; use num_traits::Float; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// Error type returned from `InverseGaussian::new` diff --git a/rand_distr/src/lib.rs b/rand_distr/src/lib.rs index c8fd298171d..8e62f3bb3b8 100644 --- a/rand_distr/src/lib.rs +++ b/rand_distr/src/lib.rs @@ -88,10 +88,6 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std; -// This is used for doc links: -#[allow(unused)] -use rand::Rng; - pub use rand::distributions::{ uniform, Alphanumeric, Bernoulli, BernoulliError, DistIter, Distribution, Open01, OpenClosed01, Standard, Uniform, @@ -160,7 +156,7 @@ mod test { // generated. This is redundant with vector and correctness tests. /// Construct a deterministic RNG with the given seed - pub fn rng(seed: u64) -> impl rand::RngCore { + pub fn rng(seed: u64) -> impl rand::Rng { // For tests, we want a statistically good, fast, reproducible RNG. // PCG32 will do fine, and will be easy to embed if we ever need to. const INC: u64 = 11634580027462260723; diff --git a/rand_distr/src/normal.rs b/rand_distr/src/normal.rs index b3b801dfed9..3d7214e3c3b 100644 --- a/rand_distr/src/normal.rs +++ b/rand_distr/src/normal.rs @@ -12,7 +12,7 @@ use crate::utils::ziggurat; use num_traits::Float; use crate::{ziggurat_tables, Distribution, Open01}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// Samples floating-point numbers according to the normal distribution diff --git a/rand_distr/src/normal_inverse_gaussian.rs b/rand_distr/src/normal_inverse_gaussian.rs index b1ba588ac8d..ded7ab21617 100644 --- a/rand_distr/src/normal_inverse_gaussian.rs +++ b/rand_distr/src/normal_inverse_gaussian.rs @@ -1,6 +1,6 @@ use crate::{Distribution, InverseGaussian, Standard, StandardNormal}; use num_traits::Float; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// Error type returned from `NormalInverseGaussian::new` diff --git a/rand_distr/src/pareto.rs b/rand_distr/src/pareto.rs index 25c8e0537dd..7a2ef085606 100644 --- a/rand_distr/src/pareto.rs +++ b/rand_distr/src/pareto.rs @@ -90,6 +90,7 @@ where F: Float, OpenClosed01: Distribution mod tests { use super::*; use core::fmt::{Debug, Display, LowerExp}; + use rand::RngExt; #[test] #[should_panic] diff --git a/rand_distr/src/poisson.rs b/rand_distr/src/poisson.rs index b0b3d15a8ae..616f8d136dc 100644 --- a/rand_distr/src/poisson.rs +++ b/rand_distr/src/poisson.rs @@ -11,7 +11,7 @@ use num_traits::{Float, FloatConst}; use crate::{Cauchy, Distribution, Standard}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// The Poisson distribution `Poisson(lambda)`. @@ -188,4 +188,4 @@ mod test { fn poisson_distributions_can_be_compared() { assert_eq!(Poisson::new(1.0), Poisson::new(1.0)); } -} \ No newline at end of file +} diff --git a/rand_distr/src/skew_normal.rs b/rand_distr/src/skew_normal.rs index 146b4ead125..3f4a76cb046 100644 --- a/rand_distr/src/skew_normal.rs +++ b/rand_distr/src/skew_normal.rs @@ -11,7 +11,7 @@ use crate::{Distribution, StandardNormal}; use core::fmt; use num_traits::Float; -use rand::Rng; +use rand::{Rng, RngExt}; /// The [skew normal distribution] `SN(location, scale, shape)`. /// diff --git a/rand_distr/src/triangular.rs b/rand_distr/src/triangular.rs index eef7d190133..345cceaa696 100644 --- a/rand_distr/src/triangular.rs +++ b/rand_distr/src/triangular.rs @@ -9,7 +9,7 @@ use num_traits::Float; use crate::{Distribution, Standard}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// The triangular distribution. @@ -101,7 +101,7 @@ where F: Float, Standard: Distribution #[cfg(test)] mod test { use super::*; - use rand::{rngs::mock, Rng}; + use rand::rngs::mock; #[test] fn test_triangular() { diff --git a/rand_distr/src/utils.rs b/rand_distr/src/utils.rs index 4638e3623d2..70f1b8deedc 100644 --- a/rand_distr/src/utils.rs +++ b/rand_distr/src/utils.rs @@ -10,7 +10,7 @@ use crate::ziggurat_tables; use rand::distributions::hidden_export::IntoFloat; -use rand::Rng; +use rand::{Rng, RngExt}; use num_traits::Float; /// Calculates ln(gamma(x)) (natural logarithm of the gamma diff --git a/rand_distr/src/weibull.rs b/rand_distr/src/weibull.rs index fe45eff6613..1b52bcaab23 100644 --- a/rand_distr/src/weibull.rs +++ b/rand_distr/src/weibull.rs @@ -10,7 +10,7 @@ use num_traits::Float; use crate::{Distribution, OpenClosed01}; -use rand::Rng; +use rand::{Rng, RngExt}; use core::fmt; /// Samples floating-point numbers according to the Weibull distribution diff --git a/rand_distr/src/weighted_alias.rs b/rand_distr/src/weighted_alias.rs index 170de80c4a5..d52518fcc8b 100644 --- a/rand_distr/src/weighted_alias.rs +++ b/rand_distr/src/weighted_alias.rs @@ -14,7 +14,7 @@ use crate::{uniform::SampleUniform, Distribution, Uniform}; use core::fmt; use core::iter::Sum; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; -use rand::Rng; +use rand::{Rng, RngExt}; use alloc::{boxed::Box, vec, vec::Vec}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; diff --git a/rand_distr/src/zipf.rs b/rand_distr/src/zipf.rs index e15b6cdd197..88665c11aa1 100644 --- a/rand_distr/src/zipf.rs +++ b/rand_distr/src/zipf.rs @@ -10,7 +10,7 @@ use num_traits::Float; use crate::{Distribution, Standard}; -use rand::{Rng, distributions::OpenClosed01}; +use rand::{Rng, RngExt, distributions::OpenClosed01}; use core::fmt; /// Samples integers according to the [zeta distribution]. diff --git a/rand_distr/tests/pdf.rs b/rand_distr/tests/pdf.rs index 14db18153a4..85a01d340b7 100644 --- a/rand_distr/tests/pdf.rs +++ b/rand_distr/tests/pdf.rs @@ -9,7 +9,7 @@ #![allow(clippy::float_cmp)] use average::Histogram; -use rand::{Rng, SeedableRng}; +use rand::{RngExt, SeedableRng}; use rand_distr::{Normal, SkewNormal}; const HIST_LEN: usize = 100; diff --git a/rand_distr/tests/value_stability.rs b/rand_distr/tests/value_stability.rs index d3754705db5..bff7da6046a 100644 --- a/rand_distr/tests/value_stability.rs +++ b/rand_distr/tests/value_stability.rs @@ -8,7 +8,7 @@ use average::assert_almost_eq; use core::fmt::Debug; -use rand::Rng; +use rand::RngExt; use rand_distr::*; fn get_rng(seed: u64) -> impl rand::Rng { diff --git a/rand_pcg/src/pcg128.rs b/rand_pcg/src/pcg128.rs index df2025dc444..9b27eaecfd6 100644 --- a/rand_pcg/src/pcg128.rs +++ b/rand_pcg/src/pcg128.rs @@ -14,7 +14,7 @@ const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645; use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, Rng, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; /// A PCG random number generator (XSL RR 128/64 (LCG) variant). @@ -135,7 +135,7 @@ impl SeedableRng for Lcg128Xsl64 { } } -impl RngCore for Lcg128Xsl64 { +impl Rng for Lcg128Xsl64 { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 @@ -245,7 +245,7 @@ impl SeedableRng for Mcg128Xsl64 { } } -impl RngCore for Mcg128Xsl64 { +impl Rng for Mcg128Xsl64 { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 diff --git a/rand_pcg/src/pcg128cm.rs b/rand_pcg/src/pcg128cm.rs index 7ac5187e4e0..ff180829331 100644 --- a/rand_pcg/src/pcg128cm.rs +++ b/rand_pcg/src/pcg128cm.rs @@ -14,7 +14,7 @@ const MULTIPLIER: u64 = 15750249268501108917; use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, Rng, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; /// A PCG random number generator (CM DXSM 128/64 (LCG) variant). @@ -140,7 +140,7 @@ impl SeedableRng for Lcg128CmDxsm64 { } } -impl RngCore for Lcg128CmDxsm64 { +impl Rng for Lcg128CmDxsm64 { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 diff --git a/rand_pcg/src/pcg64.rs b/rand_pcg/src/pcg64.rs index 365f1c0b117..0fcef173a85 100644 --- a/rand_pcg/src/pcg64.rs +++ b/rand_pcg/src/pcg64.rs @@ -11,7 +11,7 @@ //! PCG random number generators use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, Rng, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; // This is the default multiplier used by PCG for 64-bit state. @@ -134,7 +134,7 @@ impl SeedableRng for Lcg64Xsh32 { } } -impl RngCore for Lcg64Xsh32 { +impl Rng for Lcg64Xsh32 { #[inline] fn next_u32(&mut self) -> u32 { let state = self.state; diff --git a/src/distributions/bernoulli.rs b/src/distributions/bernoulli.rs index 676b79a5c10..2501ba34989 100644 --- a/src/distributions/bernoulli.rs +++ b/src/distributions/bernoulli.rs @@ -9,7 +9,7 @@ //! The Bernoulli distribution. use crate::distributions::Distribution; -use crate::Rng; +use crate::{Rng, RngExt}; use core::{fmt, u64}; #[cfg(feature = "serde1")] @@ -143,9 +143,7 @@ impl Distribution for Bernoulli { #[cfg(test)] mod test { - use super::Bernoulli; - use crate::distributions::Distribution; - use crate::Rng; + use super::*; #[test] #[cfg(feature = "serde1")] diff --git a/src/distributions/distribution.rs b/src/distributions/distribution.rs index c6eaf5ef7ab..afdf0ed524b 100644 --- a/src/distributions/distribution.rs +++ b/src/distributions/distribution.rs @@ -209,8 +209,8 @@ pub trait DistString { #[cfg(test)] mod tests { - use crate::distributions::{Distribution, Uniform}; - use crate::Rng; + use super::*; + use crate::distributions::Uniform; #[test] fn test_distributions_iter() { diff --git a/src/distributions/float.rs b/src/distributions/float.rs index 54aebad4dc5..d000835b4c1 100644 --- a/src/distributions/float.rs +++ b/src/distributions/float.rs @@ -10,7 +10,7 @@ use crate::distributions::utils::{IntAsSIMD, FloatAsSIMD, FloatSIMDUtils}; use crate::distributions::{Distribution, Standard}; -use crate::Rng; +use crate::{Rng, RngExt}; use core::mem; #[cfg(feature = "simd_support")] use core::simd::*; @@ -31,7 +31,7 @@ use serde::{Serialize, Deserialize}; /// /// # Example /// ``` -/// use rand::{thread_rng, Rng}; +/// use rand::{thread_rng, RngExt}; /// use rand::distributions::OpenClosed01; /// /// let val: f32 = thread_rng().sample(OpenClosed01); @@ -58,7 +58,7 @@ pub struct OpenClosed01; /// /// # Example /// ``` -/// use rand::{thread_rng, Rng}; +/// use rand::{thread_rng, RngExt}; /// use rand::distributions::Open01; /// /// let val: f32 = thread_rng().sample(Open01); diff --git a/src/distributions/integer.rs b/src/distributions/integer.rs index 418eea9ff13..6f19e3ae28e 100644 --- a/src/distributions/integer.rs +++ b/src/distributions/integer.rs @@ -9,7 +9,7 @@ //! The implementations of the `Standard` distribution for integer types. use crate::distributions::{Distribution, Standard}; -use crate::Rng; +use crate::{Rng, RngExt}; #[cfg(all(target_arch = "x86", feature = "simd_support"))] use core::arch::x86::__m512i; #[cfg(target_arch = "x86")] diff --git a/src/distributions/mod.rs b/src/distributions/mod.rs index a923f879d22..9b7cc715630 100644 --- a/src/distributions/mod.rs +++ b/src/distributions/mod.rs @@ -11,8 +11,8 @@ //! //! This module is the home of the [`Distribution`] trait and several of its //! implementations. It is the workhorse behind some of the convenient -//! functionality of the [`Rng`] trait, e.g. [`Rng::gen`] and of course -//! [`Rng::sample`]. +//! functionality of the [`RngExt`] trait, e.g. [`RngExt::gen`] and of course +//! [`RngExt::sample`]. //! //! Abstractly, a [probability distribution] describes the probability of //! occurrence of each value in its sample space. @@ -31,13 +31,13 @@ //! # The `Standard` distribution //! //! The [`Standard`] distribution is important to mention. This is the -//! distribution used by [`Rng::gen`] and represents the "default" way to +//! distribution used by [`RngExt::gen`] and represents the "default" way to //! produce a random value for many different types, including most primitive //! types, tuples, arrays, and a few derived types. See the documentation of //! [`Standard`] for more details. //! //! Implementing `Distribution` for [`Standard`] for user types `T` makes it -//! possible to generate type `T` with [`Rng::gen`], and by extension also +//! possible to generate type `T` with [`RngExt::gen`], and by extension also //! with the [`random`] function. //! //! ## Random characters @@ -54,16 +54,16 @@ //! space to be specified as an arbitrary range within its target type `T`. //! Both [`Standard`] and [`Uniform`] are in some sense uniform distributions. //! -//! Values may be sampled from this distribution using [`Rng::sample(Range)`] or +//! Values may be sampled from this distribution using [`RngExt::sample(Range)`] or //! by creating a distribution object with [`Uniform::new`], //! [`Uniform::new_inclusive`] or `From`. When the range limits are not //! known at compile time it is typically faster to reuse an existing -//! `Uniform` object than to call [`Rng::sample(Range)`]. +//! `Uniform` object than to call [`RngExt::sample(Range)`]. //! //! User types `T` may also implement `Distribution` for [`Uniform`], //! although this is less straightforward than for [`Standard`] (see the //! documentation in the [`uniform`] module). Doing so enables generation of -//! values of type `T` with [`Rng::sample(Range)`]. +//! values of type `T` with [`RngExt::sample(Range)`]. //! //! ## Open and half-open ranges //! @@ -76,7 +76,7 @@ //! # Non-uniform sampling //! //! Sampling a simple true/false outcome with a given probability has a name: -//! the [`Bernoulli`] distribution (this is used by [`Rng::gen_bool`]). +//! the [`Bernoulli`] distribution (this is used by [`RngExt::gen_bool`]). //! //! For weighted sampling from a sequence of discrete values, use the //! [`WeightedIndex`] distribution. @@ -129,7 +129,7 @@ pub use self::uniform::Uniform; pub use self::weighted_index::{WeightedError, WeightedIndex}; #[allow(unused)] -use crate::Rng; +use crate::{Rng, RngExt}; /// A generic random value distribution, implemented for many primitive types. /// Usually generates values with a numerically uniform distribution, and with a @@ -137,7 +137,7 @@ use crate::Rng; /// /// ## Provided implementations /// -/// Assuming the provided `Rng` is well-behaved, these implementations +/// Assuming the provided [`Rng`] is well-behaved, these implementations /// generate values with the following ranges and distributions: /// /// * Integers (`i32`, `u32`, `isize`, `usize`, etc.): Uniformly distributed @@ -163,9 +163,9 @@ use crate::Rng; /// /// * Tuples (up to 12 elements): each element is generated sequentially. /// * Arrays: each element is generated sequentially; -/// see also [`Rng::fill`] which supports arbitrary array length for integer +/// see also [`RngExt::fill`] which supports arbitrary array length for integer /// and float types and tends to be faster for `u32` and smaller types. -/// Note that [`Rng::fill`] and `Standard`'s array support are *not* equivalent: +/// Note that [`RngExt::fill`] and `Standard`'s array support are *not* equivalent: /// the former is optimised for integer types (using fewer RNG calls for /// element types smaller than the RNG word size), while the latter supports /// any element type supported by `Standard`. @@ -178,7 +178,7 @@ use crate::Rng; /// /// ``` /// # #![allow(dead_code)] -/// use rand::Rng; +/// use rand::{Rng, RngExt}; /// use rand::distributions::{Distribution, Standard}; /// /// struct MyF32 { @@ -211,7 +211,7 @@ use crate::Rng; /// multiplicative method: `(rng.gen::<$uty>() >> N) as $ty * (ε/2)`. /// /// See also: [`Open01`] which samples from `(0, 1)`, [`OpenClosed01`] which -/// samples from `(0, 1]` and `Rng::gen_range(0..1)` which also samples from +/// samples from `(0, 1]` and `RngExt::gen_range(0..1)` which also samples from /// `[0, 1)`. Note that `Open01` uses transmute-based methods which yield 1 bit /// less precision but may perform faster on some architectures (on modern Intel /// CPUs all methods have approximately equal performance). diff --git a/src/distributions/other.rs b/src/distributions/other.rs index 9ddce76366e..00a9a0228db 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -16,7 +16,7 @@ use alloc::string::String; use crate::distributions::{Distribution, Standard, Uniform}; #[cfg(feature = "alloc")] use crate::distributions::DistString; -use crate::Rng; +use crate::{Rng, RngExt}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; @@ -33,7 +33,7 @@ use core::simd::*; /// # Example /// /// ``` -/// use rand::{Rng, thread_rng}; +/// use rand::{RngExt, thread_rng}; /// use rand::distributions::Alphanumeric; /// /// let mut rng = thread_rng(); @@ -277,12 +277,12 @@ where Standard: Distribution #[cfg(test)] mod tests { use super::*; - use crate::RngCore; + use crate::Rng; #[cfg(feature = "alloc")] use alloc::string::String; #[test] fn test_misc() { - let rng: &mut dyn RngCore = &mut crate::test::rng(820); + let rng: &mut dyn Rng = &mut crate::test::rng(820); rng.sample::(Standard); rng.sample::(Standard); diff --git a/src/distributions/slice.rs b/src/distributions/slice.rs index 398cad18b2c..d30a79e9203 100644 --- a/src/distributions/slice.rs +++ b/src/distributions/slice.rs @@ -28,7 +28,7 @@ use crate::distributions::{Distribution, Uniform}; /// # Example /// /// ``` -/// use rand::Rng; +/// use rand::{RngExt}; /// use rand::distributions::Slice; /// /// let vowels = ['a', 'e', 'i', 'o', 'u']; diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index b4856ff6131..22b707087ef 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -11,7 +11,7 @@ //! //! [`Uniform`] is the standard distribution to sample uniformly from a range; //! e.g. `Uniform::new_inclusive(1, 6).unwrap()` can sample integers from 1 to 6, like a -//! standard die. [`Rng::gen_range`] supports any type supported by [`Uniform`]. +//! standard die. [`RngExt::gen_range`] supports any type supported by [`Uniform`]. //! //! This distribution is provided with support for several primitive types //! (all integer and floating-point types) as well as [`std::time::Duration`], @@ -26,7 +26,7 @@ //! # Example usage //! //! ``` -//! use rand::{Rng, thread_rng}; +//! use rand::{RngExt, thread_rng}; //! use rand::distributions::Uniform; //! //! let mut rng = thread_rng(); @@ -52,7 +52,7 @@ //! `low < high`). The example below merely wraps another back-end. //! //! The `new`, `new_inclusive` and `sample_single` functions use arguments of -//! type SampleBorrow to support passing in values by reference or +//! type `SampleBorrow` to support passing in values by reference or //! by value. In the implementation of these functions, you can choose to //! simply use the reference returned by [`SampleBorrow::borrow`], or you can choose //! to copy or clone the value, whatever is appropriate for your type. @@ -113,7 +113,7 @@ use crate::distributions::utils::{BoolAsSIMD, FloatAsSIMD, FloatSIMDUtils, IntAs use crate::distributions::Distribution; #[cfg(feature = "simd_support")] use crate::distributions::Standard; -use crate::{Rng, RngCore}; +use crate::{RngExt, Rng}; #[cfg(not(feature = "std"))] #[allow(unused_imports)] // rustc doesn't detect that this is actually used @@ -151,7 +151,7 @@ use serde::{Serialize, Deserialize}; /// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform /// distribution sampling from the given range; these functions may do extra /// work up front to make sampling of multiple values faster. If only one sample -/// from the range is required, [`Rng::gen_range`] can be more efficient. +/// from the range is required, [`RngExt::gen_range`] can be more efficient. /// /// When sampling from a constant range, many calculations can happen at /// compile-time and all methods should be fast; for floating-point ranges and @@ -183,10 +183,10 @@ use serde::{Serialize, Deserialize}; /// println!("{}", sum); /// ``` /// -/// For a single sample, [`Rng::gen_range`] may be preferred: +/// For a single sample, [`RngExt::gen_range`] may be preferred: /// /// ``` -/// use rand::Rng; +/// use rand::RngExt; /// /// let mut rng = rand::thread_rng(); /// println!("{}", rng.gen_range(0..10)); @@ -194,7 +194,7 @@ use serde::{Serialize, Deserialize}; /// /// [`new`]: Uniform::new /// [`new_inclusive`]: Uniform::new_inclusive -/// [`Rng::gen_range`]: Rng::gen_range +/// [`RngExt::gen_range`]: RngExt::gen_range #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde1", serde(bound(serialize = "X::Sampler: Serialize")))] @@ -376,10 +376,10 @@ where Borrowed: SampleUniform /// Range that supports generating a single sample efficiently. /// /// Any type implementing this trait can be used to specify the sampled range -/// for `Rng::gen_range`. +/// for `RngExt::gen_range`. pub trait SampleRange { /// Generate a sample from the given range. - fn sample_single(self, rng: &mut R) -> Result; + fn sample_single(self, rng: &mut R) -> Result; /// Check whether the range is empty. fn is_empty(&self) -> bool; @@ -387,7 +387,7 @@ pub trait SampleRange { impl SampleRange for Range { #[inline] - fn sample_single(self, rng: &mut R) -> Result { + fn sample_single(self, rng: &mut R) -> Result { T::Sampler::sample_single(self.start, self.end, rng) } @@ -399,7 +399,7 @@ impl SampleRange for Range { impl SampleRange for RangeInclusive { #[inline] - fn sample_single(self, rng: &mut R) -> Result { + fn sample_single(self, rng: &mut R) -> Result { T::Sampler::sample_single_inclusive(self.start(), self.end(), rng) } @@ -1137,7 +1137,7 @@ impl UniformSampler for UniformDuration { max_nanos, secs, } => { - // constant folding means this is at least as fast as `Rng::sample(Range)` + // constant folding means this is at least as fast as `RngExt::sample(Range)` let nano_range = Uniform::new(0, 1_000_000_000).unwrap(); loop { let s = secs.sample(rng); diff --git a/src/distributions/weighted_index.rs b/src/distributions/weighted_index.rs index 4c57edc5f60..8484b15521a 100644 --- a/src/distributions/weighted_index.rs +++ b/src/distributions/weighted_index.rs @@ -49,7 +49,7 @@ use serde::{Serialize, Deserialize}; /// /// Sampling from `WeightedIndex` will result in a single call to /// `Uniform::sample` (method of the [`Distribution`] trait), which typically -/// will request a single value from the underlying [`RngCore`], though the +/// will request a single value from the underlying [`Rng`], though the /// exact number depends on the implementation of `Uniform::sample`. /// /// # Example @@ -76,7 +76,7 @@ use serde::{Serialize, Deserialize}; /// ``` /// /// [`Uniform`]: crate::distributions::Uniform -/// [`RngCore`]: crate::RngCore +/// [`Rng`]: crate::Rng #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] @@ -239,6 +239,7 @@ where X: SampleUniform + PartialOrd #[cfg(test)] mod test { use super::*; + use crate::RngExt; #[cfg(feature = "serde1")] #[test] diff --git a/src/lib.rs b/src/lib.rs index 755b5ba6e9c..379ca02587d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ //! //! To get you started quickly, the easiest and highest-level way to get //! a random value is to use [`random()`]; alternatively you can use -//! [`thread_rng()`]. The [`Rng`] trait provides a useful API on all RNGs, while +//! [`thread_rng()`]. The [`RngExt`] trait provides a useful API on all RNGs, while //! the [`distributions`] and [`seq`] modules provide further //! functionality on top of RNGs. //! @@ -91,7 +91,7 @@ macro_rules! error { ($($x:tt)*) => ( ) } // Re-exports from rand_core -pub use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; +pub use rand_core::{CryptoRng, Error, Rng, SeedableRng}; // Public modules pub mod distributions; @@ -103,7 +103,7 @@ pub mod seq; // Public exports #[cfg(all(feature = "std", feature = "std_rng"))] pub use crate::rngs::thread::thread_rng; -pub use rng::{Fill, Rng}; +pub use rng::{Fill, RngExt}; #[cfg(all(feature = "std", feature = "std_rng"))] use crate::distributions::{Distribution, Standard}; @@ -133,7 +133,7 @@ use crate::distributions::{Distribution, Standard}; /// following example can increase performance. /// /// ``` -/// use rand::Rng; +/// use rand::RngExt; /// /// let mut v = vec![1, 2, 3]; /// @@ -165,7 +165,7 @@ mod test { use super::*; /// Construct a deterministic RNG with the given seed - pub fn rng(seed: u64) -> impl RngCore { + pub fn rng(seed: u64) -> impl Rng { // For tests, we want a statistically good, fast, reproducible RNG. // PCG32 will do fine, and will be easy to embed if we ever need to. const INC: u64 = 11634580027462260723; diff --git a/src/prelude.rs b/src/prelude.rs index 51c457e3f9e..e7e8bef7b1e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -31,4 +31,4 @@ pub use crate::rngs::ThreadRng; #[doc(no_inline)] #[cfg(all(feature = "std", feature = "std_rng"))] pub use crate::{random, thread_rng}; -#[doc(no_inline)] pub use crate::{CryptoRng, Rng, RngCore, SeedableRng}; +#[doc(no_inline)] pub use crate::{CryptoRng, Rng, RngExt, SeedableRng}; diff --git a/src/rng.rs b/src/rng.rs index 1b53298d511..9cd364dbd45 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -7,15 +7,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! [`Rng`] trait +//! [`RngExt`] trait -use rand_core::{Error, RngCore}; +use rand_core::{Error, Rng}; use crate::distributions::uniform::{SampleRange, SampleUniform}; use crate::distributions::{self, Distribution, Standard}; use core::num::Wrapping; use core::{mem, slice}; -/// An automatically-implemented extension trait on [`RngCore`] providing high-level +/// An automatically-implemented extension trait on [`Rng`] providing high-level /// generic methods for sampling values and other convenience methods. /// /// This is the primary trait to use when generating random values. @@ -25,10 +25,10 @@ use core::{mem, slice}; /// The basic pattern is `fn foo(rng: &mut R)`. Some /// things are worth noting here: /// -/// - Since `Rng: RngCore` and every `RngCore` implements `Rng`, it makes no -/// difference whether we use `R: Rng` or `R: RngCore`. +/// - Since `RngExt: Rng` and every `Rng` implements `RngExt`, it makes no +/// difference whether we use `R: RngExt` or `R: Rng`. /// - The `+ ?Sized` un-bounding allows functions to be called directly on -/// type-erased references; i.e. `foo(r)` where `r: &mut dyn RngCore`. Without +/// type-erased references; i.e. `foo(r)` where `r: &mut dyn Rng`. Without /// this it would be necessary to write `foo(&mut r)`. /// /// An alternative pattern is possible: `fn foo(rng: R)`. This has some @@ -44,21 +44,21 @@ use core::{mem, slice}; /// /// ``` /// # use rand::thread_rng; -/// use rand::Rng; +/// use rand::RngExt; /// -/// fn foo(rng: &mut R) -> f32 { +/// fn foo(rng: &mut R) -> f32 { /// rng.gen() /// } /// /// # let v = foo(&mut thread_rng()); /// ``` -pub trait Rng: RngCore { +pub trait RngExt: Rng { /// Return a random value via the [`Standard`] distribution. /// /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut rng = thread_rng(); /// let x: u32 = rng.gen(); @@ -73,10 +73,10 @@ pub trait Rng: RngCore { /// generated. /// /// For arrays of integers, especially for those with small element types - /// (< 64 bit), it will likely be faster to instead use [`Rng::fill`]. + /// (< 64 bit), it will likely be faster to instead use [`RngExt::fill`]. /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut rng = thread_rng(); /// let tuple: (u8, i32, char) = rng.gen(); // arbitrary tuple support @@ -108,7 +108,7 @@ pub trait Rng: RngCore { /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut rng = thread_rng(); /// @@ -138,7 +138,7 @@ pub trait Rng: RngCore { /// ### Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// use rand::distributions::Uniform; /// /// let mut rng = thread_rng(); @@ -162,7 +162,7 @@ pub trait Rng: RngCore { /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// use rand::distributions::{Alphanumeric, Uniform, Standard}; /// /// let mut rng = thread_rng(); @@ -205,16 +205,16 @@ pub trait Rng: RngCore { /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut arr = [0i8; 20]; /// thread_rng().fill(&mut arr[..]); /// ``` /// - /// [`fill_bytes`]: RngCore::fill_bytes - /// [`try_fill`]: Rng::try_fill + /// [`fill_bytes`]: Rng::fill_bytes + /// [`try_fill`]: RngExt::try_fill fn fill(&mut self, dest: &mut T) { - dest.try_fill(self).unwrap_or_else(|_| panic!("Rng::fill failed")) + dest.try_fill(self).unwrap_or_else(|_| panic!("RngExt::fill failed")) } /// Fill any type implementing [`Fill`] with random data @@ -228,7 +228,7 @@ pub trait Rng: RngCore { /// /// ``` /// # use rand::Error; - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// # fn try_inner() -> Result<(), Error> { /// let mut arr = [0u64; 4]; @@ -239,8 +239,8 @@ pub trait Rng: RngCore { /// # try_inner().unwrap() /// ``` /// - /// [`try_fill_bytes`]: RngCore::try_fill_bytes - /// [`fill`]: Rng::fill + /// [`try_fill_bytes`]: Rng::try_fill_bytes + /// [`fill`]: RngExt::fill fn try_fill(&mut self, dest: &mut T) -> Result<(), Error> { dest.try_fill(self) } @@ -253,7 +253,7 @@ pub trait Rng: RngCore { /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut rng = thread_rng(); /// println!("{}", rng.gen_bool(1.0 / 3.0)); @@ -286,7 +286,7 @@ pub trait Rng: RngCore { /// # Example /// /// ``` - /// use rand::{thread_rng, Rng}; + /// use rand::{thread_rng, RngExt}; /// /// let mut rng = thread_rng(); /// println!("{}", rng.gen_ratio(2, 3)); @@ -300,7 +300,7 @@ pub trait Rng: RngCore { } } -impl Rng for R {} +impl RngExt for R {} /// Types which may be filled with random data /// @@ -530,7 +530,7 @@ mod test { fn test_rng_trait_object() { use crate::distributions::{Distribution, Standard}; let mut rng = rng(109); - let mut r = &mut rng as &mut dyn RngCore; + let mut r = &mut rng as &mut dyn Rng; r.next_u32(); r.gen::(); assert_eq!(r.gen_range(0..1), 0); @@ -542,7 +542,7 @@ mod test { fn test_rng_boxed_trait() { use crate::distributions::{Distribution, Standard}; let rng = rng(110); - let mut r = Box::new(rng) as Box; + let mut r = Box::new(rng) as Box; r.next_u32(); r.gen::(); assert_eq!(r.gen_range(0..1), 0); diff --git a/src/rngs/adapter/read.rs b/src/rngs/adapter/read.rs index 25a9ca7fca4..582c036125a 100644 --- a/src/rngs/adapter/read.rs +++ b/src/rngs/adapter/read.rs @@ -14,7 +14,7 @@ use std::fmt; use std::io::Read; -use rand_core::{impls, Error, RngCore}; +use rand_core::{impls, Error, Rng}; /// An RNG that reads random bytes straight from any type supporting @@ -30,10 +30,10 @@ use rand_core::{impls, Error, RngCore}; /// `ReadRng` uses [`std::io::Read::read_exact`], which retries on interrupts. /// All other errors from the underlying reader, including when it does not /// have enough data, will only be reported through [`try_fill_bytes`]. -/// The other [`RngCore`] methods will panic in case of an error. +/// The other [`Rng`] methods will panic in case of an error. /// /// [`OsRng`]: crate::rngs::OsRng -/// [`try_fill_bytes`]: RngCore::try_fill_bytes +/// [`try_fill_bytes`]: Rng::try_fill_bytes #[derive(Debug)] #[deprecated(since="0.8.4", note="removal due to lack of usage")] pub struct ReadRng { @@ -47,7 +47,7 @@ impl ReadRng { } } -impl RngCore for ReadRng { +impl Rng for ReadRng { fn next_u32(&mut self) -> u32 { impls::next_u32_via_fill(self) } @@ -99,7 +99,7 @@ mod test { use std::println; use super::ReadRng; - use crate::RngCore; + use crate::Rng; #[test] fn test_reader_rng_u64() { diff --git a/src/rngs/adapter/reseeding.rs b/src/rngs/adapter/reseeding.rs index a47ab7c7484..83119839385 100644 --- a/src/rngs/adapter/reseeding.rs +++ b/src/rngs/adapter/reseeding.rs @@ -13,7 +13,7 @@ use core::mem::size_of; use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng}; -use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; +use rand_core::{CryptoRng, Error, Rng, SeedableRng}; /// A wrapper around any PRNG that implements [`BlockRngCore`], that adds the /// ability to reseed it. @@ -85,12 +85,12 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; pub struct ReseedingRng(BlockRng>) where R: BlockRngCore + SeedableRng, - Rsdr: RngCore; + Rsdr: Rng; impl ReseedingRng where R: BlockRngCore + SeedableRng, - Rsdr: RngCore, + Rsdr: Rng, { /// Create a new `ReseedingRng` from an existing PRNG, combined with a RNG /// to use as reseeder. @@ -109,8 +109,8 @@ where } // TODO: this should be implemented for any type where the inner type -// implements RngCore, but we can't specify that because ReseedingCore is private -impl RngCore for ReseedingRng +// implements Rng, but we can't specify that because ReseedingCore is private +impl Rng for ReseedingRng where R: BlockRngCore + SeedableRng, { @@ -136,7 +136,7 @@ where impl Clone for ReseedingRng where R: BlockRngCore + SeedableRng + Clone, - Rsdr: RngCore + Clone, + Rsdr: Rng + Clone, { fn clone(&self) -> ReseedingRng { // Recreating `BlockRng` seems easier than cloning it and resetting @@ -164,7 +164,7 @@ struct ReseedingCore { impl BlockRngCore for ReseedingCore where R: BlockRngCore + SeedableRng, - Rsdr: RngCore, + Rsdr: Rng, { type Item = ::Item; type Results = ::Results; @@ -186,7 +186,7 @@ where impl ReseedingCore where R: BlockRngCore + SeedableRng, - Rsdr: RngCore, + Rsdr: Rng, { /// Create a new `ReseedingCore`. fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self { @@ -263,7 +263,7 @@ where impl Clone for ReseedingCore where R: BlockRngCore + SeedableRng + Clone, - Rsdr: RngCore + Clone, + Rsdr: Rng + Clone, { fn clone(&self) -> ReseedingCore { ReseedingCore { @@ -344,7 +344,7 @@ mod test { use super::ReseedingRng; use crate::rngs::mock::StepRng; use crate::rngs::std::Core; - use crate::{Rng, SeedableRng}; + use crate::{RngExt, SeedableRng}; #[test] fn test_reseeding() { diff --git a/src/rngs/mock.rs b/src/rngs/mock.rs index a1745a490dd..6f4f1347209 100644 --- a/src/rngs/mock.rs +++ b/src/rngs/mock.rs @@ -8,19 +8,19 @@ //! Mock random number generator -use rand_core::{impls, Error, RngCore}; +use rand_core::{impls, Error, Rng}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; -/// A simple implementation of `RngCore` for testing purposes. +/// A simple implementation of `Rng` for testing purposes. /// /// This generates an arithmetic sequence (i.e. adds a constant each step) /// over a `u64` number, using wrapping arithmetic. If the increment is 0 /// the generator yields a constant. /// /// ``` -/// use rand::Rng; +/// use rand::RngExt; /// use rand::rngs::mock::StepRng; /// /// let mut my_rng = StepRng::new(2, 1); @@ -45,7 +45,7 @@ impl StepRng { } } -impl RngCore for StepRng { +impl Rng for StepRng { #[inline] fn next_u32(&mut self) -> u32 { self.next_u64() as u32 diff --git a/src/rngs/mod.rs b/src/rngs/mod.rs index ac3c2c595da..7d90da5c4a9 100644 --- a/src/rngs/mod.rs +++ b/src/rngs/mod.rs @@ -32,8 +32,8 @@ //! //! ## Traits and functionality //! -//! All RNGs implement the [`RngCore`] trait, as a consequence of which the -//! [`Rng`] extension trait is automatically implemented. Secure RNGs may +//! All RNGs implement the [`Rng`] trait, as a consequence of which the +//! [`RngExt`] extension trait is automatically implemented. Secure RNGs may //! additionally implement the [`CryptoRng`] trait. //! //! All PRNGs require a seed to produce their random number sequence. The @@ -84,8 +84,8 @@ //! Some suggestions are: [`rand_chacha`], [`rand_pcg`], [`rand_xoshiro`]. //! A full list can be found by searching for crates with the [`rng` tag]. //! +//! [`RngExt`]: crate::RngExt //! [`Rng`]: crate::Rng -//! [`RngCore`]: crate::RngCore //! [`CryptoRng`]: crate::CryptoRng //! [`SeedableRng`]: crate::SeedableRng //! [`thread_rng`]: crate::thread_rng diff --git a/src/rngs/small.rs b/src/rngs/small.rs index a3261757847..a92c24ea6af 100644 --- a/src/rngs/small.rs +++ b/src/rngs/small.rs @@ -8,12 +8,12 @@ //! A small fast RNG -use rand_core::{Error, RngCore, SeedableRng}; +use rand_core::{Error, Rng, SeedableRng}; #[cfg(target_pointer_width = "64")] -type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus; +type Base = super::xoshiro256plusplus::Xoshiro256PlusPlus; #[cfg(not(target_pointer_width = "64"))] -type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus; +type Base = super::xoshiro128plusplus::Xoshiro128PlusPlus; /// A small-state, fast non-crypto PRNG /// @@ -46,7 +46,7 @@ type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus; /// Initializing `SmallRng` with a random seed can be done using [`SeedableRng::from_entropy`]: /// /// ``` -/// use rand::{Rng, SeedableRng}; +/// use rand::{RngExt, SeedableRng}; /// use rand::rngs::SmallRng; /// /// // Create small, cheap to initialize and fast RNG with a random seed. @@ -78,9 +78,9 @@ type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus; /// [rand_xoshiro]: https://crates.io/crates/rand_xoshiro #[cfg_attr(doc_cfg, doc(cfg(feature = "small_rng")))] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct SmallRng(Rng); +pub struct SmallRng(Base); -impl RngCore for SmallRng { +impl Rng for SmallRng { #[inline(always)] fn next_u32(&mut self) -> u32 { self.0.next_u32() @@ -103,20 +103,20 @@ impl RngCore for SmallRng { } impl SeedableRng for SmallRng { - type Seed = ::Seed; + type Seed = ::Seed; #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { - SmallRng(Rng::from_seed(seed)) + SmallRng(Base::from_seed(seed)) } #[inline(always)] - fn from_rng(rng: R) -> Result { - Rng::from_rng(rng).map(SmallRng) + fn from_rng(rng: R) -> Result { + Base::from_rng(rng).map(SmallRng) } #[inline(always)] fn seed_from_u64(state: u64) -> Self { - SmallRng(Rng::seed_from_u64(state)) + SmallRng(Base::seed_from_u64(state)) } } diff --git a/src/rngs/std.rs b/src/rngs/std.rs index cdae8fab01c..ac7b04e363c 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -8,11 +8,11 @@ //! The standard RNG -use crate::{CryptoRng, Error, RngCore, SeedableRng}; +use crate::{CryptoRng, Error, Rng, SeedableRng}; pub(crate) use rand_chacha::ChaCha12Core as Core; -use rand_chacha::ChaCha12Rng as Rng; +use rand_chacha::ChaCha12Rng as Base; /// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient /// on the current platform, to be statistically strong and unpredictable @@ -31,9 +31,9 @@ use rand_chacha::ChaCha12Rng as Rng; /// [rand issue]: https://github.com/rust-random/rand/issues/932 #[cfg_attr(doc_cfg, doc(cfg(feature = "std_rng")))] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct StdRng(Rng); +pub struct StdRng(Base); -impl RngCore for StdRng { +impl Rng for StdRng { #[inline(always)] fn next_u32(&mut self) -> u32 { self.0.next_u32() @@ -56,16 +56,16 @@ impl RngCore for StdRng { } impl SeedableRng for StdRng { - type Seed = ::Seed; + type Seed = ::Seed; #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { - StdRng(Rng::from_seed(seed)) + StdRng(Base::from_seed(seed)) } #[inline(always)] - fn from_rng(rng: R) -> Result { - Rng::from_rng(rng).map(StdRng) + fn from_rng(rng: R) -> Result { + Base::from_rng(rng).map(StdRng) } } @@ -75,7 +75,7 @@ impl CryptoRng for StdRng {} #[cfg(test)] mod test { use crate::rngs::StdRng; - use crate::{RngCore, SeedableRng}; + use crate::{Rng, SeedableRng}; #[test] fn test_stdrng_construction() { diff --git a/src/rngs/thread.rs b/src/rngs/thread.rs index 78cecde5755..a043bb16754 100644 --- a/src/rngs/thread.rs +++ b/src/rngs/thread.rs @@ -16,7 +16,7 @@ use std::fmt; use super::std::Core; use crate::rngs::adapter::ReseedingRng; use crate::rngs::OsRng; -use crate::{CryptoRng, Error, RngCore, SeedableRng}; +use crate::{CryptoRng, Error, Rng, SeedableRng}; // Rationale for using `UnsafeCell` in `ThreadRng`: // @@ -97,7 +97,7 @@ thread_local!( /// /// Example usage: /// ``` -/// use rand::Rng; +/// use rand::RngExt; /// /// # fn main() { /// // rand::random() may be used instead of rand::thread_rng().gen(): @@ -119,7 +119,7 @@ impl Default for ThreadRng { } } -impl RngCore for ThreadRng { +impl Rng for ThreadRng { #[inline(always)] fn next_u32(&mut self) -> u32 { // SAFETY: We must make sure to stop using `rng` before anyone else @@ -158,7 +158,7 @@ impl CryptoRng for ThreadRng {} mod test { #[test] fn test_thread_rng() { - use crate::Rng; + use crate::RngExt; let mut r = crate::thread_rng(); r.gen::(); assert_eq!(r.gen_range(0..1), 0); diff --git a/src/rngs/xoshiro128plusplus.rs b/src/rngs/xoshiro128plusplus.rs index ece98fafd6a..fa91073cef6 100644 --- a/src/rngs/xoshiro128plusplus.rs +++ b/src/rngs/xoshiro128plusplus.rs @@ -9,7 +9,7 @@ #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next}; use rand_core::le::read_u32_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{SeedableRng, Rng, Error}; /// A xoshiro128++ random number generator. /// @@ -58,7 +58,7 @@ impl SeedableRng for Xoshiro128PlusPlus { } } -impl RngCore for Xoshiro128PlusPlus { +impl Rng for Xoshiro128PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { let result_starstar = self.s[0] diff --git a/src/rngs/xoshiro256plusplus.rs b/src/rngs/xoshiro256plusplus.rs index 8ffb18b8033..3afbdbcea6a 100644 --- a/src/rngs/xoshiro256plusplus.rs +++ b/src/rngs/xoshiro256plusplus.rs @@ -9,7 +9,7 @@ #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{SeedableRng, Rng, Error}; /// A xoshiro256++ random number generator. /// @@ -58,7 +58,7 @@ impl SeedableRng for Xoshiro256PlusPlus { } } -impl RngCore for Xoshiro256PlusPlus { +impl Rng for Xoshiro256PlusPlus { #[inline] fn next_u32(&mut self) -> u32 { // The lowest bits have some linear dependencies, so we use the diff --git a/src/seq/coin_flipper.rs b/src/seq/coin_flipper.rs index 05f18d71b2e..8697255d3e0 100644 --- a/src/seq/coin_flipper.rs +++ b/src/seq/coin_flipper.rs @@ -6,15 +6,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::RngCore; +use crate::Rng; -pub(crate) struct CoinFlipper { +pub(crate) struct CoinFlipper { pub rng: R, chunk: u32, //TODO(opt): this should depend on RNG word size chunk_remaining: u32, } -impl CoinFlipper { +impl CoinFlipper { pub fn new(rng: R) -> Self { Self { rng, diff --git a/src/seq/increasing_uniform.rs b/src/seq/increasing_uniform.rs index 3208c656fb5..bc0ab4df61c 100644 --- a/src/seq/increasing_uniform.rs +++ b/src/seq/increasing_uniform.rs @@ -6,11 +6,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use crate::{Rng, RngCore}; +use crate::{Rng, RngExt}; /// Similar to a Uniform distribution, /// but after returning a number in the range [0,n], n is increased by 1. -pub(crate) struct IncreasingUniform { +pub(crate) struct IncreasingUniform { pub rng: R, n: u32, // Chunk is a random number in [0, (n + 1) * (n + 2) *..* (n + chunk_remaining) ) @@ -18,7 +18,7 @@ pub(crate) struct IncreasingUniform { chunk_remaining: u8, } -impl IncreasingUniform { +impl IncreasingUniform { /// Create a dice roller. /// The next item returned will be a random number in the range [0,n] pub fn new(rng: R, n: u32) -> Self { diff --git a/src/seq/index.rs b/src/seq/index.rs index 50523cc47c4..707b2cd8c58 100644 --- a/src/seq/index.rs +++ b/src/seq/index.rs @@ -20,7 +20,7 @@ use alloc::collections::BTreeSet; use crate::distributions::WeightedError; #[cfg(feature = "alloc")] -use crate::{Rng, distributions::{uniform::SampleUniform, Distribution, Uniform}}; +use crate::{Rng, RngExt, distributions::{uniform::SampleUniform, Distribution, Uniform}}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; diff --git a/src/seq/mod.rs b/src/seq/mod.rs index d9b38e920d7..6bec434eb2f 100644 --- a/src/seq/mod.rs +++ b/src/seq/mod.rs @@ -41,7 +41,7 @@ use alloc::vec::Vec; use crate::distributions::uniform::{SampleBorrow, SampleUniform}; #[cfg(feature = "alloc")] use crate::distributions::WeightedError; -use crate::Rng; +use crate::{Rng, RngExt}; use self::coin_flipper::CoinFlipper; use self::increasing_uniform::IncreasingUniform;