Skip to content

Commit 51d1365

Browse files
committed
rand_core: Add CryptoRngCore to support CryptoRng trait objects
Fixes #1143.
1 parent eac985f commit 51d1365

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

rand_core/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ pub trait RngCore {
208208
/// [`BlockRngCore`]: block::BlockRngCore
209209
pub trait CryptoRng {}
210210

211+
/// An extension trait to support trait objects that implement [`RngCore`] and
212+
/// [`CryptoRng`]. Upcasting to [`RngCore`] is supported via the
213+
/// [`CryptoRngCore::upcast`] method.
214+
pub trait CryptoRngCore: RngCore {
215+
/// Upcast to an [`RngCore`] trait object.
216+
fn upcast(&mut self) -> &mut dyn RngCore;
217+
}
218+
219+
impl<T: CryptoRng + RngCore> CryptoRngCore for T {
220+
fn upcast(&mut self) -> &mut dyn RngCore {
221+
self
222+
}
223+
}
224+
211225
/// A random number generator that can be explicitly seeded.
212226
///
213227
/// This trait encapsulates the low-level functionality common to all

0 commit comments

Comments
 (0)