use super::query::TrailQuery;
pub trait Invariant: 'static + Send {
fn name(&self) -> &str;
fn observe(&self, q: &dyn TrailQuery, sim_time_ms: u64);
fn reset(&mut self) {}
}
type InvariantFn = Box<dyn Fn(&dyn TrailQuery, u64) + Send>;
struct FnInvariant {
name: String,
check: InvariantFn,
}
impl Invariant for FnInvariant {
fn name(&self) -> &str {
&self.name
}
fn observe(&self, q: &dyn TrailQuery, sim_time_ms: u64) {
(self.check)(q, sim_time_ms);
}
}
pub fn invariant_fn(
name: impl Into<String>,
f: impl Fn(&dyn TrailQuery, u64) + Send + 'static,
) -> Box<dyn Invariant + Send> {
Box::new(FnInvariant {
name: name.into(),
check: Box::new(f),
})
}