2个版本

0.1.1 2022年7月26日
0.1.0 2022年7月24日

#compat中的第6

MIT许可

9KB
90

rand_compat

rand和rand_core不同版本特性之间的适配层,使用方法请参阅文档

状态

GitHub tag Build Status Crates.io Docs.rs


lib.rs:

为rand和rand_core提供适配不同版本特性的兼容层

前向兼容性(使用rand/std的rand_0_7::OsRng

use rand_0_7::rngs::OsRng; use rand_core_0_6::{RngCore, CryptoRng}; use rand_compat::ForwardCompat;

// RngCore + CryptoRng from rand_core@0.6.x fn something<R: RngCore + CryptoRng>(r: &mut R) -> u32 { r.next_u32() }

let mut rng = OsRng; // OsRng from rand@0.7.x (rand_core@0.5.x)

let n = something(&mut rng.forward());


## Backward compatibility (using `rand/std` for `rand_0_8::OsRng`)

use rand_0_8::rngs::OsRng;
use rand_core_0_5::{RngCore, CryptoRng};
use rand_compat::BackwardCompat;

// RngCore + CryptoRng from rand_core@0.5.x
fn something<R: RngCore + CryptoRng>(r: &mut R) -> u32 {
    r.next_u32()
}

let mut rng = OsRng;    // OsRng from rand@0.8.x (rand_core@0.6.x)

let n = something(&mut rng.backward());

依赖项

~1.4–2.2MB
~40K SLoC