#rand #compatibility #compat #random

无std rand-compat

rand_core不同版本的兼容层

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 [email protected] fn something<R: RngCore + CryptoRng>(r: &mut R) -> u32 { r.next_u32() }

let mut rng = OsRng; // OsRng from [email protected] ([email protected])

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 [email protected]
fn something<R: RngCore + CryptoRng>(r: &mut R) -> u32 {
    r.next_u32()
}

let mut rng = OsRng;    // OsRng from [email protected] ([email protected])

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

依赖项

~1.4–2.2MB
~40K SLoC