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
use rand::Rng;
mod binary_symmetric_channel;
pub use binary_symmetric_channel::BinarySymmetricChannel;
pub trait NoiseModel {
type Error;
fn sample_error_of_length<R: Rng>(&self, length: usize, rng: &mut R) -> Self::Error;
}
pub struct Probability(f64);
impl Probability {
pub fn new(probability: f64) -> Self {
Self::try_new(probability).expect("probability is not between 0 and 1")
}
pub fn try_new(probability: f64) -> Option<Self> {
if 0.0 <= probability && probability <= 1.0 {
Some(Self(probability))
} else {
None
}
}
pub fn value(&self) -> f64 {
self.0
}
}