|
10 | 10 | //! A wrapper around another PRNG that reseeds it after it |
11 | 11 | //! generates a certain number of random bytes. |
12 | 12 |
|
| 13 | +use core::convert::Infallible; |
13 | 14 | use core::mem::size_of_val; |
14 | 15 |
|
15 | 16 | use rand_core::block::{BlockRng, CryptoGenerator, Generator}; |
16 | | -use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore}; |
| 17 | +use rand_core::{SeedableRng, TryCryptoRng, TryRngCore}; |
17 | 18 |
|
18 | 19 | /// A wrapper around any PRNG that implements [`Generator`], that adds the |
19 | 20 | /// ability to reseed it. |
@@ -93,34 +94,37 @@ where |
93 | 94 | /// |
94 | 95 | /// This discards any remaining random data in the cache. |
95 | 96 | pub fn reseed(&mut self) -> Result<(), Rsdr::Error> { |
96 | | - self.0.reset(); |
| 97 | + self.0.reset_and_skip(0); |
97 | 98 | self.0.core.reseed() |
98 | 99 | } |
99 | 100 | } |
100 | 101 |
|
101 | 102 | // TODO: this should be implemented for any type where the inner type |
102 | | -// implements RngCore, but we can't specify that because ReseedingCore is private |
103 | | -impl<const N: usize, G, Rsdr> RngCore for ReseedingRng<G, Rsdr> |
| 103 | +// implements TryRngCore, but we can't specify that because ReseedingCore is private |
| 104 | +impl<const N: usize, G, Rsdr> TryRngCore for ReseedingRng<G, Rsdr> |
104 | 105 | where |
105 | 106 | G: Generator<Output = [u32; N]> + SeedableRng, |
106 | 107 | Rsdr: TryRngCore, |
107 | 108 | { |
| 109 | + type Error = Infallible; |
| 110 | + |
108 | 111 | #[inline(always)] |
109 | | - fn next_u32(&mut self) -> u32 { |
110 | | - self.0.next_word() |
| 112 | + fn try_next_u32(&mut self) -> Result<u32, Infallible> { |
| 113 | + Ok(self.0.next_word()) |
111 | 114 | } |
112 | 115 |
|
113 | 116 | #[inline(always)] |
114 | | - fn next_u64(&mut self) -> u64 { |
115 | | - self.0.next_u64_from_u32() |
| 117 | + fn try_next_u64(&mut self) -> Result<u64, Infallible> { |
| 118 | + Ok(self.0.next_u64_from_u32()) |
116 | 119 | } |
117 | 120 |
|
118 | | - fn fill_bytes(&mut self, dest: &mut [u8]) { |
119 | | - self.0.fill_bytes(dest) |
| 121 | + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible> { |
| 122 | + self.0.fill_bytes(dest); |
| 123 | + Ok(()) |
120 | 124 | } |
121 | 125 | } |
122 | 126 |
|
123 | | -impl<const N: usize, G, Rsdr> CryptoRng for ReseedingRng<G, Rsdr> |
| 127 | +impl<const N: usize, G, Rsdr> TryCryptoRng for ReseedingRng<G, Rsdr> |
124 | 128 | where |
125 | 129 | G: Generator<Output = [u32; N]> + SeedableRng + CryptoGenerator, |
126 | 130 | Rsdr: TryCryptoRng, |
|
0 commit comments