1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::{Rng, Uncertain};
use std::marker::PhantomData;

/// Wraps a [`Distribution`](rand::distributions::Distribution) and implements
/// [`Uncertain`](Uncertain).
///
/// Uncertain values need to observe the correct `epoch` semantics if
/// they implement [`Copy`] or [`Clone`], or want to implement [`Uncertain`](Uncertain)
/// for references.
/// Since most uncertain values model distributions but distributions
/// themselves do not require any special `epoch` semantics when they can be `Cloned` or
/// are implemented on references, this wrapper type exists.
///
/// See [`Uncertain::sample`](Uncertain::sample) for more details on the required semantics.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use uncertain::{Uncertain, Distribution};
/// use rand_distr::StandardNormal;
///
/// let x = Distribution::from(StandardNormal);
/// assert!(x.map(|x: f64| x.abs() < 1.0).pr(0.68));
/// ```
pub struct Distribution<T, D>
where
    D: rand::distributions::Distribution<T>,
{
    dist: D,
    _p: PhantomData<T>,
}

impl<T, D> Uncertain for Distribution<T, D>
where
    D: rand::distributions::Distribution<T>,
{
    type Value = T;

    fn sample(&self, rng: &mut Rng, _epoch: usize) -> Self::Value {
        self.dist.sample(rng)
    }
}

impl<T, D> From<D> for Distribution<T, D>
where
    D: rand::distributions::Distribution<T>,
{
    fn from(dist: D) -> Self {
        Self {
            dist,
            _p: PhantomData {},
        }
    }
}