trellis_runner/watchers/
mod.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::sync::{Arc, Mutex};

// #[cfg(feature = "writing")]
// mod file;
//
// #[cfg(feature = "writing")]
// pub use file::FileWriter;
//
// #[cfg(feature = "plotting")]
// mod plot;
// #[cfg(feature = "plotting")]
// pub use plot::PlotGenerator;
//
mod tracing;
pub use tracing::Tracer;

pub enum Target {
    Param,
    Measure,
}

#[derive(Copy, Clone)]
pub enum Stage {
    Initialisation,
    WrapUp,
    Iteration,
}

#[derive(Clone)]
#[allow(clippy::type_complexity)]
pub(crate) struct ObserverVec<S>(Vec<(Arc<Mutex<dyn Observer<S>>>, Frequency)>);

impl<S> ObserverVec<S> {
    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }
}

impl<S> Default for ObserverVec<S> {
    fn default() -> Self {
        Self(vec![])
    }
}

impl<S> ObserverVec<S> {
    pub(crate) fn as_slice(&self) -> ObserverSlice<'_, S> {
        ObserverSlice(&self.0[..])
    }
}

#[allow(clippy::type_complexity)]
pub(crate) struct ObserverSlice<'a, S>(&'a [(Arc<Mutex<dyn Observer<S>>>, Frequency)]);

pub trait Observer<S>: Send + Sync {
    fn observe(&self, ident: &'static str, subject: &S, stage: Stage);
}

pub trait Observable<S> {
    type Observer;
    fn update(&self, ident: &'static str, subject: &S, stage: Stage);
    fn attach(&mut self, observer: Self::Observer, frequency: Frequency);
    fn detach(&mut self, observer: Self::Observer);
}

#[derive(Clone)]
pub(crate) struct Subject<D> {
    pub(crate) data: D,
    pub(crate) observers: ObserverVec<D>,
}

impl<S> Observable<S> for ObserverVec<S> {
    type Observer = Arc<Mutex<dyn Observer<S>>>;
    fn update(&self, ident: &'static str, subject: &S, stage: Stage) {
        self.0
            .iter()
            .map(|o| o.0.lock().unwrap())
            .for_each(|o| o.observe(ident, subject, stage));
    }
    fn attach(&mut self, observer: Self::Observer, frequency: Frequency) {
        self.0.push((observer, frequency));
    }
    fn detach(&mut self, observer: Self::Observer) {
        self.0.retain(|f| !Arc::ptr_eq(&f.0, &observer));
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ObservationError {
    #[error("error in writer")]
    Writer(Box<dyn std::error::Error + 'static>), // We don't wrap the actual error, as we don't want to import the deps unless requested
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
// How often the observations should take place
pub enum Frequency {
    // An observer that never observes
    Never,
    // Observations occur on every iteration
    Always,
    // Observations occur on every nth iteration
    Every(usize),
    // The observer runs during the wrap up stage only
    OnExit,
}

impl Default for Frequency {
    fn default() -> Self {
        Self::Never
    }
}