|
6 | 6 | //! |
7 | 7 | //! The struct [`BlockRng`] wraps such a [`Generator`] together with an output |
8 | 8 | //! 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`] |
11 | 11 | //! since in practice we found it was always beneficial to use a wrapper type |
12 | 12 | //! over [`BlockRng`]. |
13 | 13 | //! |
14 | 14 | //! # Example |
15 | 15 | //! |
16 | 16 | //! ``` |
17 | | -//! use rand_core::{RngCore, SeedableRng}; |
| 17 | +//! use core::convert::Infallible; |
| 18 | +//! use rand_core::{RngCore, SeedableRng, TryRngCore}; |
18 | 19 | //! use rand_core::block::{Generator, BlockRng}; |
19 | 20 | //! |
20 | 21 | //! struct MyRngCore { |
|
45 | 46 | //! } |
46 | 47 | //! } |
47 | 48 | //! |
48 | | -//! impl RngCore for MyRng { |
| 49 | +//! impl TryRngCore for MyRng { |
| 50 | +//! type Error = Infallible; |
| 51 | +//! |
49 | 52 | //! #[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()) |
52 | 55 | //! } |
53 | 56 | //! |
54 | 57 | //! #[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()) |
57 | 60 | //! } |
58 | 61 | //! |
59 | 62 | //! #[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)) |
62 | 65 | //! } |
63 | 66 | //! } |
64 | 67 | //! |
65 | | -//! // And if applicable: impl CryptoRng for MyRng {} |
| 68 | +//! // And if applicable: impl TryCryptoRng for MyRng {} |
66 | 69 | //! |
67 | 70 | //! let mut rng = MyRng::seed_from_u64(0); |
68 | 71 | //! println!("First value: {}", rng.next_u32()); |
|
74 | 77 | //! The [`Generator`] trait supports usage of [`rand::rngs::ReseedingRng`]. |
75 | 78 | //! This requires that [`SeedableRng`] be implemented on the "core" generator. |
76 | 79 | //! 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.) |
78 | 81 | //! |
79 | 82 | //! [`Generator`]: crate::block::Generator |
80 | | -//! [`RngCore`]: crate::RngCore |
| 83 | +//! [`TryRngCore`]: crate::TryRngCore |
81 | 84 | //! [`SeedableRng`]: crate::SeedableRng |
82 | 85 | //! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html |
83 | 86 |
|
@@ -115,7 +118,7 @@ pub trait Generator { |
115 | 118 | /// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be |
116 | 119 | /// used in production. |
117 | 120 | /// |
118 | | -/// See [`CryptoRng`](crate::CryptoRng) docs for more information. |
| 121 | +/// See [`TryCryptoRng`](crate::TryCryptoRng) docs for more information. |
119 | 122 | pub trait CryptoGenerator: Generator {} |
120 | 123 |
|
121 | 124 | /// RNG functionality for a block [`Generator`] |
|
0 commit comments