Context
I'm trying to build a list of Distribution<T>
s in a library that consumes said distributions. I'd like to use rand_distr::Distribution
to accept any trait object that implements Distribution in my collection.
Ultimately, what I want to do is push()
any instance of a struct that implements Distribution
, such as Normal
, then go through every entry in my Vec
and call sample()
.
Problem
If I try to use a Vec<dyn Distribution<T>>
, I'm met with an error that tells me I can't do this because the trait is not object safe. This makes sense, because the function I want to use from the trait, sample()
is generic over R: Rng
. However, I know exactly what Rng I want to use, so I don't want to compiler to monomorphise the function for multiple types of Rng.
Is there some way for me to tell this to the compiler? I think it might be possible if there was an associated type defined in sample()
?