Skip to content
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

Move OsRng to rand_core #863

Merged
merged 3 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 15 additions & 3 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,50 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.1] - 2019-09-02
### Added
- `OsRng` added to `rand_core` (#863)

## [0.5.0] - 2019-06-06
### Changed
- Enable testing with Miri and fix incorrect pointer usages (#779, #780, #781, #783, #784)
- Rewrite `Error` type and adjust API (#800)
- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64`

## [0.4.0] - 2019-01-24
### Changed
- Disable the `std` feature by default (#702)

## [0.3.0] - 2018-09-24
### Added
- Add `SeedableRng::seed_from_u64` for convenient seeding. (#537)

## [0.2.1] - 2018-06-08
### Added
- References to a `CryptoRng` now also implement `CryptoRng`. (#470)

## [0.2.0] - 2018-05-21
### Changed
- Enable the `std` feature by default. (#409)
- Remove `BlockRng{64}::inner` and `BlockRng::inner_mut`; instead making `core` public
- Add `BlockRng{64}::index` and `BlockRng{64}::generate_and_set`. (#374, #419)
- Change `BlockRngCore::Results` bound to also require `AsMut<[Self::Item]>`. (#419)
### Added
- Add `BlockRng{64}::index` and `BlockRng{64}::generate_and_set`. (#374, #419)
- Implement `std::io::Read` for RngCore. (#434)

## [0.1.0] - 2018-04-17
(Split out of the Rand crate, changes here are relative to rand 0.4.2)
(Split out of the Rand crate, changes here are relative to rand 0.4.2.)
### Added
- `RngCore` and `SeedableRng` are now part of `rand_core`. (#288)
- Add modules to help implementing RNGs `impl` and `le`. (#209, #228)
- Add `Error` and `ErrorKind`. (#225)
- Add `CryptoRng` marker trait. (#273)
- Add `BlockRngCore` trait. (#281)
- Add `BlockRng` and `BlockRng64` wrappers to help implementations. (#281, #325)
- Add `RngCore::try_fill_bytes`. (#225)
### Changed
- Revise the `SeedableRng` trait. (#233)
- Remove default implementations for `RngCore::next_u64` and `RngCore::fill_bytes`. (#288)
- Add `RngCore::try_fill_bytes`. (#225)

## [0.0.1] - 2017-09-14 (yanked)
Experimental version as part of the rand crate refactor.
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand_core"
version = "0.5.0"
version = "0.5.1"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ use core::ptr::copy_nonoverlapping;
#[cfg(all(feature="alloc", not(feature="std")))] use alloc::boxed::Box;

pub use error::Error;
#[cfg(feature="getrandom")] pub use os::OsRng;


mod error;
pub mod block;
pub mod impls;
pub mod le;
#[cfg(feature="getrandom")] mod os;


/// The core of a random number generator.
Expand Down
22 changes: 6 additions & 16 deletions src/rngs/os.rs → rand_core/src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
//! Interface to the random number generator of the operating system.
// Note: keep this code in sync with the rand_os crate!

use crate::getrandom::getrandom;
use rand_core::{CryptoRng, RngCore, Error, impls};
use getrandom::getrandom;
use crate::{CryptoRng, RngCore, Error, impls};

/// A random number generator that retrieves randomness from from the
/// operating system.
Expand All @@ -20,6 +20,9 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
/// The implementation is provided by the [getrandom] crate. Refer to
/// [getrandom] documentation for details.
///
/// This struct is only available when specifying the crate feature `getrandom`
/// or `std`. When using the `rand` lib, it is also available as `rand::rngs::OsRng`.
///
/// # Blocking and error handling
///
/// It is possible that when used during early boot the first call to `OsRng`
Expand All @@ -33,30 +36,17 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
///
/// # Usage example
/// ```
/// use rand::rngs::{StdRng, OsRng};
/// use rand::{RngCore, SeedableRng};
/// use rand_core::{RngCore, OsRng};
///
/// let mut key = [0u8; 16];
/// OsRng.fill_bytes(&mut key);
/// let random_u64 = OsRng.next_u64();
///
/// // OsRng is especially useful for seeding other RNGs (see also from_entropy)
/// let mut rng = StdRng::from_rng(OsRng).unwrap();
/// let _ = rng.next_u32();
/// ```
///
/// [getrandom]: https://crates.io/crates/getrandom
#[derive(Clone, Copy, Debug, Default)]
pub struct OsRng;

impl OsRng {
/// Create a new `OsRng`.
#[deprecated(since="0.7.0", note="replace OsRng::new().unwrap() with just OsRng")]
pub fn new() -> Result<OsRng, Error> {
Ok(OsRng)
}
}

impl CryptoRng for OsRng {}

impl RngCore for OsRng {
Expand Down
4 changes: 4 additions & 0 deletions rand_os/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.2] - 2019-09-02
### Changed
- `OsRng` added to `rand_core`, rendering this crate deprecated (#863)

## [0.2.1] - 2019-08-08
### Fixed
- Fix `no_std` support.
Expand Down
2 changes: 1 addition & 1 deletion rand_os/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand_os"
version = "0.2.1"
version = "0.2.2"
authors = ["The Rand Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions rand_os/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
A random number generator that retrieves randomness straight from the
operating system.

**This crate is deprecated:** `OsRng` is available in `rand_core` since version 0.5.1.

This crate provides `OsRng` as a shim around
[getrandom](https://crates.io/crates/getrandom)
implementing `RngCore` from [rand_core](https://crates.io/crates/rand_core).
Expand Down
2 changes: 2 additions & 0 deletions rand_os/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#![no_std] // but see getrandom crate

#![deprecated(since="0.2.2", note="OsRng is now provided by rand_core and rand")]

pub use rand_core; // re-export

use getrandom::getrandom;
Expand Down
3 changes: 1 addition & 2 deletions src/rngs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,4 @@ pub use self::small::SmallRng;
pub use self::std::StdRng;
#[cfg(feature="std")] pub use self::thread::ThreadRng;

#[cfg(feature="getrandom")] mod os;
#[cfg(feature="getrandom")] pub use self::os::OsRng;
#[cfg(feature="getrandom")] pub use rand_core::OsRng;