Skip to content

Conversation

@tarcieri
Copy link
Contributor

Since RngCore has been renamed to Rng, and TryRngCore to TryRng, this adds some deprecated stub traits with the old names that have a note about the new names.

They have supertrait bounds on the new names, along with blanket impls for the new names:

  • RngCore: Rng
  • TryRngCore: TryRng

These can be released in v0.10.x and removed before v1.x.

Though they don't have any methods, due to an odd quirk of how Rust resolves methods in a generic context (effectively uniting a trait and all of its supertraits into a single logical method table), the methods of the new traits are callable from a generic context even without the new traits in the bounds or even in scope whatsoever:

use rand_core::RngCore;

pub fn foo<R: RngCore>(mut rng: R) -> [u8; 8] {
    let mut ret = [0u8; 8];
    rng.fill_bytes(&mut ret);
    ret
}

This will actually compile with the following warnings:

warning: use of deprecated trait `rand_core::RngCore`: use `Rng` instead
 --> src/main.rs:1:16
  |
1 | use rand_core::RngCore;
  |                ^^^^^^^
  |
  = note: `#[warn(deprecated)]` on by default

warning: use of deprecated trait `rand_core::RngCore`: use `Rng` instead
 --> src/main.rs:3:15
  |
3 | pub fn foo<R: RngCore>(mut rng: R) -> [u8; 8] {
  |               ^^^^^^^

I think this will help people upgrade because it will allow a significant amount of code to continue to compile while also informing people of the new names and what code needs to be changed.

@tarcieri tarcieri requested a review from dhardy January 30, 2026 23:12
@tarcieri tarcieri force-pushed the add-forwarding-traits-with-deprecations branch 2 times, most recently from 81f88e2 to 5584f04 Compare January 30, 2026 23:14
Since `RngCore` has been renamed to `Rng`, and `TryRngCore` to `TryRng`,
this adds some deprecated stub traits with the old names that have a
`note` about the new names.

They have supertrait bounds on the new names, along with blanket impls
for the new names:

- `RngCore: Rng`
- `TryRngCore: TryRng`

These can be released in v0.10.x and removed before v1.x.

Though they don't have any methods, due to an odd quirk of how Rust
resolves methods in a generic context (effectively uniting a trait and
all of its supertraits into a single logical method table), the methods
of the new traits are callable from a generic context even without the
new traits in the bounds or even in scope whatsoever:

    use rand_core::RngCore;

    pub fn foo<R: RngCore>(mut rng: R) -> [u8; 8] {
	let mut ret = [0u8; 8];
	rng.fill_bytes(&mut ret);
	ret
    }

This will actually compile with the following warnings:

warning: use of deprecated trait `rand_core::RngCore`: use `Rng` instead
 --> src/main.rs:1:16
  |
1 | use rand_core::RngCore;
  |                ^^^^^^^
  |
  = note: `#[warn(deprecated)]` on by default

warning: use of deprecated trait `rand_core::RngCore`: use `Rng` instead
 --> src/main.rs:3:15
  |
3 | pub fn foo<R: RngCore>(mut rng: R) -> [u8; 8] {
  |               ^^^^^^^

I think this will help people upgrade because it will allow a
significant amount of code to continue to compile while also informing
people of the new names and what code needs to be changed.
@tarcieri tarcieri force-pushed the add-forwarding-traits-with-deprecations branch from 5584f04 to 1a059c7 Compare January 30, 2026 23:14
Comment on lines +266 to +271
/// Error type.
type Error: core::error::Error;
}
#[allow(deprecated)]
impl<R: TryRng> TryRngCore for R {
type Error = R::Error;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same trick doesn't apply to associated types though, so I forwarded this one.

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

This doesn't help when it comes to implementing RNGs, but should help upgrade use-sites at least.

@dhardy
Copy link
Member

dhardy commented Jan 31, 2026

It would also be useful to have a deprecation error for the methods SeedableRng::{try_,}from_os_rng; unfortunately we can't implement them (except by panicking).

@dhardy dhardy merged commit f9763d8 into master Jan 31, 2026
13 checks passed
@dhardy dhardy deleted the add-forwarding-traits-with-deprecations branch January 31, 2026 17:47
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.

3 participants