Skip to content

Commit

Permalink
rand_core: Add CryptoRngCore to support CryptoRng trait objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Sep 17, 2021
1 parent eac985f commit 51d1365
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ pub trait RngCore {
/// [`BlockRngCore`]: block::BlockRngCore
pub trait CryptoRng {}

/// An extension trait to support trait objects that implement [`RngCore`] and
/// [`CryptoRng`]. Upcasting to [`RngCore`] is supported via the
/// [`CryptoRngCore::upcast`] method.
pub trait CryptoRngCore: RngCore {
/// Upcast to an [`RngCore`] trait object.
fn upcast(&mut self) -> &mut dyn RngCore;
}

impl<T: CryptoRng + RngCore> CryptoRngCore for T {
fn upcast(&mut self) -> &mut dyn RngCore {
self
}
}

/// A random number generator that can be explicitly seeded.
///
/// This trait encapsulates the low-level functionality common to all
Expand Down

0 comments on commit 51d1365

Please sign in to comment.