-
-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rand_core: add blanket impl of TryRngCore for RngCore #1499
Conversation
c700007
to
da637dd
Compare
@@ -250,12 +250,12 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> { | |||
} | |||
|
|||
#[inline(always)] | |||
fn from_rng(rng: impl RngCore) -> Self { | |||
fn from_rng(rng: &mut impl RngCore) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, this change is not needed, but I made this signature symmetric with try_from_rng
, which has to explicitly accept &mut impl TryRngCore
. Plus, in our RustCrypto experience, it's easier for new users to understand &mut impl RngCore
than to find that impl RngCore
can accept &mut R
.
The rationale behind the current design was:
|
@josephlr or @TheIronBorn or @vks: it would be good to get a third opinion on this. Admittedly the
Tolerable, yes, but it's something I'd like to be able to fix later without serious risk of breakage (assessing the impact of adding these impls later would be hard). Of course, there is the question of whether Rust will ever get specialization or negative-trait-impls or some other mechanism allowing potentially-overlapping-trait-impls, but I would hope so (it's a significant limitation of the language currently). |
Assuming that in future we will get specialization or negative impls, I think we should be able to add a blanket |
impl<T: DerefMut> RngCore for T
where
T::Target: RngCore, My experience with this type of impl is that it sometimes works, and sometimes results in false conflicts. A problematic case is here: the given impl covers the first three macro-impls (containers deref'ing to A use of this that I haven't (yet) found a problem with can be found here (see autoimpl docs). |
With negative trait impls, adding the blanket impl would be a breaking change since some downstream crate could make a local
That sounds kinda like a lang-wide automatic impl of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, on the whole, I'm willing to accept this despite my arguments against, since in the (somewhat likely) case that Rust does not get specialization/negative-trait-bounds "reasonably soon" it is a little better than the status quo, and without causing significant issues.
I don't think the "hacky macros" being removed are a big deal though (due to being limited to RNG implementors).
In this case the main goal of the blanket impl is to make RNG implementations generic over |
This PR removes the hacky
impl_try_rng_from_rng_core
andimpl_try_crypto_rng_from_crypto_rng
macros and replaces them with blanket impls ofTryRngCore
forRngCore
andTryCryptoRng
forCryptoRng
.This change means that
TryRngCore
/TryCryptoRng
no longer can have blanket impls for&mut R
andBox<R>
. But I think it should be tolerable since most users will be usingRngCore
/CryptoRng
, which have blanket impl forDerefMut
(it covers both&mut R
andBox<R>
).