Summary
Change CryptoRng such that it can be used as a random number generator through a trait object, by making it a subtrait of RngCore.
This would break existing CryptoRng implementations, which were previously not implementing RngCore, but those were likely of limited use.
Details
Change the CryptoRng trait from this
to this
pub trait CryptoRng: RngCore {}
Motivation
Currently the use of RngCore is supported in trait objects, however because of how trait objects are implemented, it is not possible to use CryptoRng in addition in the trait object, thus making it impossible to actually use a CryptoRng as a random number generator in trait objects.
This change is really small and likely has very little negative impact on users who don't need this change, but it can enable another way to use the API for users who do need it.
Alternatives
-
Add a CryptoRngCore trait to the rand_core crate:
trait CryptoRngCore: RngCore + CryptoRng {}
impl<T: RngCore + CryptoRng> CryptoRngCore for T {}
This would allow users to use RngCore + CryptoRng types through trait objects as well, but at the cost of an additional trait in the (I presume intentionally) small rand_core crate.
-
Do nothing and let users implement the CryptoRngCore trait above if they need it.
This allows them to use RngCore + CryptoRng types through trait objects, but since it isn't a unified solution, multiple equivalent traits with the exact same purpose and definition might pop up over time.
Optionally this pattern could also be documented in the CryptoRng trait.
Summary
Change
CryptoRngsuch that it can be used as a random number generator through a trait object, by making it a subtrait ofRngCore.This would break existing
CryptoRngimplementations, which were previously not implementingRngCore, but those were likely of limited use.Details
Change the
CryptoRngtrait from thisto this
Motivation
Currently the use of
RngCoreis supported in trait objects, however because of how trait objects are implemented, it is not possible to useCryptoRngin addition in the trait object, thus making it impossible to actually use aCryptoRngas a random number generator in trait objects.This change is really small and likely has very little negative impact on users who don't need this change, but it can enable another way to use the API for users who do need it.
Alternatives
Add a
CryptoRngCoretrait to therand_corecrate:This would allow users to use
RngCore + CryptoRngtypes through trait objects as well, but at the cost of an additional trait in the (I presume intentionally) smallrand_corecrate.Do nothing and let users implement the
CryptoRngCoretrait above if they need it.This allows them to use
RngCore + CryptoRngtypes through trait objects, but since it isn't a unified solution, multiple equivalent traits with the exact same purpose and definition might pop up over time.Optionally this pattern could also be documented in the
CryptoRngtrait.