Skip to content

Conversation

@dhardy
Copy link
Member

@dhardy dhardy commented Jan 12, 2026

  • Added a CHANGELOG.md entry

Summary

Revise the core traits according to @newpavlov's suggestion and similar to #41 but keeping the infallible methods.

No trait renames are done in this PR. Also not sure if we need any.

Details

  • TryRngCore is implemented for any R: DerefMut where Target: TryRngCore.
  • RngCore: TryRngCore<Error = Infallible> with automatic implementation over TryRngCore
  • UnwrapMut is removed
  • CryptoRng = RngCore + TryCryptoRng or as close as possible without sum traits; it is kept since I believe some people need dyn CryptoRng
  • utils::{next_u64_via_u32, next_word_via_fill} now use closures instead of R: RngCore

Questions

Should utils fns support potentially-fallible uses (try_next_u64_via_u32 takes FnMut() -> Result<u32; E> for generic E)? I think unnecessary.

@dhardy dhardy mentioned this pull request Jan 12, 2026
3 tasks
@dhardy
Copy link
Member Author

dhardy commented Jan 13, 2026

Updated. Some things need further discussion (see above).

@dhardy dhardy changed the title Let RngCore: TryRngCore Let RngCore: TryRngCore<Error = Infallible> Jan 14, 2026
@dhardy dhardy merged commit 48d9605 into master Jan 14, 2026
13 checks passed
@dhardy dhardy deleted the push-zqznkttsmlvp branch January 14, 2026 18:37
tarcieri pushed a commit to RustCrypto/traits that referenced this pull request Jan 19, 2026
This fixes changes made in #2195

This is due to the refactor made in rand_core in
rust-random/rand_core#45 which dropped the
"trait dependency" between CryptoRng and RngCore
@tarcieri
Copy link
Contributor

We noticed something a little weird here:

So:

pub trait CryptoRng: TryCryptoRng<Error = Infallible> {}
impl<R: TryRngCore<Error = Infallible>> RngCore for R { ...

I would expect that means any type which impls CryptoRng also impls RngCore, but in RustCrypto/traits#2197 we had to add explicit RngCore bounds (or reborrow as in RustCrypto/traits#2195)

@newpavlov
Copy link
Member

I guess we should revert my suggestion above and add a test for this.

@tarcieri
Copy link
Contributor

I don't get it... I tried to make a contrived version in the playground and it... works?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=be7a3fdc3b7bc93fb22c1529d99d4f03

@tarcieri
Copy link
Contributor

tarcieri commented Jan 19, 2026

Aha, the ?Sized bound breaks the blanket impl of RngCore for TryRngCore.

I think adding ?Sized to the bounds of the blanket impl should fix the problem?

Updated contrived example:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a352bef7b0f1c475ed0715326782d5dc

tarcieri added a commit that referenced this pull request Jan 19, 2026
Adds `?Sized` to the bounds of the blanket impls, so unsized types can
also be used by way of the blanket impl.

See also: #45
@tarcieri
Copy link
Contributor

Opened a PR to add ?Sized to the bounds of the blanket impl: #51

/// Wrap RNG with the [`UnwrapMut`] wrapper.
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self> {
UnwrapMut(self)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping UnwrapMut causes me trouble in dhkem. UnwrapErr requires R to be Sized which may not be possible if the Rng comes from a trait.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see failures "failed to load source for dependency rand_core". Can you point me to a job failing because of UnwrapErr?

Note that UnwrapErr itself does not require the Sized bound and its only field is public, so you should be able to create it using UnwrapErr(rng).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we already have test which test the ?Sized case, see: https://github.com/rust-random/rand_core/blob/master/src/lib.rs#L620-L623

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baloo (&mut rng).unwrap_err() should be equivalent to the old rng.unwrap_mut().

(We could perhaps add unwrap_mut(&mut self) -> UnwrapErr<&mut Self>, but it shouldn't be necessary.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just remove the unwrap_err method in favor of wrapping in UnwrapErr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #53

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, UnwrapErr(rng) worked, I was convinced I tried it out yesterday night, but I guess not. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants