Skip to content

Commit 5584f04

Browse files
committed
Add deprecated RngCore/TryRngCore forwarding traits
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.
1 parent ae88096 commit 5584f04

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,24 @@ where
249249
pub trait TryCryptoRng: TryRng {}
250250

251251
impl<R: DerefMut> TryCryptoRng for R where R::Target: TryCryptoRng {}
252+
253+
/// DEPRECATED: stub trait to print a deprecation warning and aid discovering that [`Rng`] is the
254+
/// replacement.
255+
// TODO: remove prior to v1.x.
256+
#[deprecated(since = "0.10.0", note = "use `Rng` instead")]
257+
pub trait RngCore: Rng {}
258+
#[allow(deprecated)]
259+
impl<R: Rng> RngCore for R {}
260+
261+
/// DEPRECATED: stub trait to print a deprecation warning and aid discovering that [`Rng`] is the
262+
/// replacement.
263+
// TODO: remove prior to v1.x.
264+
#[deprecated(since = "0.10.0", note = "use `TryRng` instead")]
265+
pub trait TryRngCore: TryRng {
266+
/// Error type.
267+
type Error: core::error::Error;
268+
}
269+
#[allow(deprecated)]
270+
impl<R: TryRng> TryRngCore for R {
271+
type Error = R::Error;
272+
}

0 commit comments

Comments
 (0)