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
use rand::distributions::{Distribution, Standard};
#[derive(Clone, Copy, Debug, Hash, PartialEq, crate::Deserialize, crate::Serialize)]
pub struct StdGenerator<T>(T);
impl<T> StdGenerator<T>
where
Standard: Distribution<T>,
{
fn constructor(data: T) -> Result<Self, crate::BoxError> {
Ok(Self(data))
}
pub fn new(data: T) -> Self {
match Self::constructor(data) {
Ok(v) => v,
Err(e) => panic!("Generator Error: {}", e),
}
}
pub fn random() -> Self {
Self::new(crate::random_number::<T>())
}
}
impl<T> Default for StdGenerator<T>
where
Standard: Distribution<T>,
{
fn default() -> Self {
Self::new(crate::random_number::<T>())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generator() {
let actual = StdGenerator::<f64>::default();
let expected = actual.clone();
assert_eq!(actual, expected)
}
}