Hi fellas I'm a little bit puzzled with this one and asking for your help.
use rand;
trait Material {
fn calculate(&self, data: &mut Data, rng: Option<&mut dyn rand::RngCore>)
}
impl Material for Foo {
fn calculate(&self, data: &mut Data, rng: Option<&mut dyn rand::RngCore>) {
// How to use use the Some part of the Option or create a new one (rand::thread_rng()) if it's None?
// Avoiding unnecessary allocations of course.
let mut rng = ?
}
}
I tried with Cow
:
let rng = rng.map_or_else(||Cow::Owned(rand::thread_rng()), |v| Cow::Borrow(v));
error[E0271]: type mismatch resolving `<dyn rand_core::RngCore as std::borrow::ToOwned>::Owned == rand::rngs::thread::ThreadRng`
|
21 | let mut rng= rng.map_or_else(||Cow::Owned(rand::thread_rng()), |v| Cow::Borrowed(v));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn rand_core::RngCore`, found struct `rand::rngs::thread::ThreadRng`
|
= note: expected type `dyn rand_core::RngCore`
found struct `rand::rngs::thread::ThreadRng`
What is the best way to solve this?
I'm using dyn rand::RngCore
instead of a generic parameter, because the calculate is a trait object method and we can't have trait objects with generics. Please correct me if I'm wrong.