thesis/
mismatch.rs

1#[derive(Debug)]
2/// Type passed to the `on_mismatch` function, which is called when the control
3/// and experimental methods create different values.
4pub struct Mismatch<T> {
5    /// The value generated by the control method
6    pub control: T,
7
8    /// The value generated by the experimental method
9    pub experimental: T,
10}
11
12/// A `MismatchHandler` resolves differences between the control and experimental
13/// values. If the two values differ, then `on_mismatch` is called. This function
14/// is used to determine which value should be passed back to the aplication, and
15/// possibly do some extra logging or recording if desired.
16pub trait MismatchHandler<T> {
17    fn on_mismatch(self, mismatch: Mismatch<T>) -> T;
18}
19
20/// A mismatch handler which always returns the value from the control function
21/// and does nothing else.
22pub struct AlwaysControl;
23
24impl<T> MismatchHandler<T> for AlwaysControl {
25    fn on_mismatch(self, mismatch: Mismatch<T>) -> T {
26        mismatch.control
27    }
28}
29
30/// FnTrait is a MismatchHandler that wraps a closure
31pub struct FnTrait<F>(pub(crate) F);
32
33impl<F, T> MismatchHandler<T> for FnTrait<F>
34where
35    F: FnOnce(Mismatch<T>) -> T,
36{
37    fn on_mismatch(self, mismatch: Mismatch<T>) -> T {
38        self.0(mismatch)
39    }
40}