I’m starting with a regular &mut ThreadRng
in all my functions, but it limits dependency injection capacities. There are SeedableRng
ofc, but how to use it in places where initially it was a ThreadRng
?
Coming from other languages I’d like to specify some interface in the fn declaration, but &mut dyn Rng
doesn’t work because size is not defined at compile-time.
What I ended up with is passing &mut StdRng
everywhere and
let mut rng = match config.seed {
Some(seed) => StdRng::seed_from_u64(seed),
_ => StdRng::from_rng(rand::thread_rng()).unwrap_or(StdRng::from_entropy())
};
But it’s awkward to init 1 Rng
only to setup another Rng
I also searched the pattern StdRng::from_rng(rand::thread_rng())
and it’s just 56 samples in github, from 141k of using rand::thread_rng()
So what’s the best signature for a fn accepting Rng and the best way to init with an optional seed?
p.s. I don’t care about wasm/etc runtimes, it’s just a regular server-side app
Vladimir Bokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.