jeudi 2 juillet 2020

How do use rand crate for random sampling on my type (i.e. impl Distribution

I have defined a type for 2D points:

struct Point(f64, f64)

I would like to be able to generate random points using rand crate. To avoid manually implementing Distribution<Point> for all the distributions in the rand crate, I tried to implement this trait for all distributions that implement Distribution<f64>.

impl<D: Distribution<f64>> Distribution<Point> for D {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point {
        Point(self.sample(rng), self.sample(rng))
    }
}

However, I get an error saying that there are conflicting implementations for the type &_. Is there a way around this?




Aucun commentaire:

Enregistrer un commentaire