A trait for constructing an object from an CSPRNG.
use mc_from_random::{CryptoRng, FromRandom, RngCore};
use rand_chacha::{ChaChaRng, rand_core::SeedableRng};
struct MyStruct {
pub bytes: [u8; 32],
}
impl FromRandom for MyStruct {
fn from_random<R: CryptoRng + RngCore>(csprng: &mut R) -> Self {
MyStruct {
bytes: <[u8; 32]>::from_random(csprng),
}
}
}
const SEED: [u8; 32] = [0b0101_0101; 32];
let mut csprng = ChaChaRng::from_seed(SEED);
let myobj = MyStruct::from_random(&mut csprng);
let expected: [u8; 32] = [
0xb0, 0x23, 0x58, 0xb3, 0x7e, 0x4f, 0x68, 0x68, 0xfb, 0x48, 0xaf, 0x8a, 0xb2, 0x75, 0x0b, 0x06,
0x2a, 0xa9, 0x5a, 0x83, 0x1b, 0xc2, 0x19, 0x78, 0xa9, 0x8e, 0x94, 0x42, 0x64, 0xfa, 0x0e, 0x75
];
assert_eq!(expected, myobj.bytes);