Skip to content

Support CryptoRng in RngCore trait objects #1143

@aticu

Description

@aticu

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

pub trait CryptoRng {}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions