Skip to content

Commit 2a0b42e

Browse files
authored
Let RngCore be a sub-trait of TryRngCore<Error = Infallible> (rust-random#45)
1 parent 3a1a11f commit 2a0b42e

4 files changed

Lines changed: 225 additions & 203 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Replace `le::read_u32_into` and `le::read_u64_into` with `utils::read_words` ([#38])
2828
- Replace fn `BlockRng::index` with `word_offset` ([#44])
2929
- Rename fn `BlockRng::generate_and_set` -> `reset_and_skip`; remove fn `reset` ([#44])
30+
- `RngCore` is now an extension trait of `TryRngCore<Error = Infallible>` ([#45])
31+
- Remove `UnwrapMut` ([#45])
32+
- Add error handling to `utils` functions over `TryRngCore` or via closure ([#45])
3033

3134
### Other
3235
- Changed repository from [rust-random/rand] to [rust-random/core].
@@ -46,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4649
[#36]: https://github.com/rust-random/rand-core/pull/36
4750
[#38]: https://github.com/rust-random/rand-core/pull/38
4851
[#44]: https://github.com/rust-random/rand-core/pull/44
52+
[#45]: https://github.com/rust-random/rand-core/pull/45
4953

5054
[rust-random/rand]: https://github.com/rust-random/rand
5155
[rust-random/core]: https://github.com/rust-random/core

src/block.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
//!
77
//! The struct [`BlockRng`] wraps such a [`Generator`] together with an output
88
//! buffer and implements several methods (e.g. [`BlockRng::next_word`]) to
9-
//! assist in the implementation of [`RngCore`]. Note that (unlike in earlier
10-
//! versions of `rand_core`) [`BlockRng`] itself does not implement [`RngCore`]
9+
//! assist in the implementation of [`TryRngCore`]. Note that (unlike in earlier
10+
//! versions of `rand_core`) [`BlockRng`] itself does not implement [`TryRngCore`]
1111
//! since in practice we found it was always beneficial to use a wrapper type
1212
//! over [`BlockRng`].
1313
//!
1414
//! # Example
1515
//!
1616
//! ```
17-
//! use rand_core::{RngCore, SeedableRng};
17+
//! use core::convert::Infallible;
18+
//! use rand_core::{RngCore, SeedableRng, TryRngCore};
1819
//! use rand_core::block::{Generator, BlockRng};
1920
//!
2021
//! struct MyRngCore {
@@ -45,24 +46,26 @@
4546
//! }
4647
//! }
4748
//!
48-
//! impl RngCore for MyRng {
49+
//! impl TryRngCore for MyRng {
50+
//! type Error = Infallible;
51+
//!
4952
//! #[inline]
50-
//! fn next_u32(&mut self) -> u32 {
51-
//! self.0.next_word()
53+
//! fn try_next_u32(&mut self) -> Result<u32, Infallible> {
54+
//! Ok(self.0.next_word())
5255
//! }
5356
//!
5457
//! #[inline]
55-
//! fn next_u64(&mut self) -> u64 {
56-
//! self.0.next_u64_from_u32()
58+
//! fn try_next_u64(&mut self) -> Result<u64, Infallible> {
59+
//! Ok(self.0.next_u64_from_u32())
5760
//! }
5861
//!
5962
//! #[inline]
60-
//! fn fill_bytes(&mut self, bytes: &mut [u8]) {
61-
//! self.0.fill_bytes(bytes)
63+
//! fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Infallible> {
64+
//! Ok(self.0.fill_bytes(bytes))
6265
//! }
6366
//! }
6467
//!
65-
//! // And if applicable: impl CryptoRng for MyRng {}
68+
//! // And if applicable: impl TryCryptoRng for MyRng {}
6669
//!
6770
//! let mut rng = MyRng::seed_from_u64(0);
6871
//! println!("First value: {}", rng.next_u32());
@@ -74,10 +77,10 @@
7477
//! The [`Generator`] trait supports usage of [`rand::rngs::ReseedingRng`].
7578
//! This requires that [`SeedableRng`] be implemented on the "core" generator.
7679
//! Additionally, it may be useful to implement [`CryptoGenerator`].
77-
//! (This is in addition to any implementations on an [`RngCore`] type.)
80+
//! (This is in addition to any implementations on an [`TryRngCore`] type.)
7881
//!
7982
//! [`Generator`]: crate::block::Generator
80-
//! [`RngCore`]: crate::RngCore
83+
//! [`TryRngCore`]: crate::TryRngCore
8184
//! [`SeedableRng`]: crate::SeedableRng
8285
//! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html
8386
@@ -115,7 +118,7 @@ pub trait Generator {
115118
/// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
116119
/// used in production.
117120
///
118-
/// See [`CryptoRng`](crate::CryptoRng) docs for more information.
121+
/// See [`TryCryptoRng`](crate::TryCryptoRng) docs for more information.
119122
pub trait CryptoGenerator: Generator {}
120123

121124
/// RNG functionality for a block [`Generator`]

0 commit comments

Comments
 (0)