pub trait UpdateHandler:
Clone
+ Send
+ Sync {
// Provided methods
fn global_init(&mut self, _config: &EngineUpdateConfig, _states: &[State]) { ... }
fn new_state_init(&mut self, _state_id: usize, _state: &State) { ... }
fn state_updated(&mut self, _state_id: usize, _state: &State) { ... }
fn state_complete(&mut self, _state_id: usize, _state: &State) { ... }
fn stop_engine(&self) -> bool { ... }
fn stop_state(&self, _state_id: usize) -> bool { ... }
fn finalize(&mut self) { ... }
}
Expand description
Custom state inspector for Engine::update
.
This trait can be used to implement progress capture and early stopping.
§Example
The following example will store timing and then prints them to STDOUT.
use std::sync::{Arc, Mutex};
use std::time::Instant;
use lace::update_handler::UpdateHandler;
use lace::EngineUpdateConfig;
use lace::cc::state::State;
use lace::examples::Example;
#[derive(Debug, Clone)]
pub struct TimingsHandler {
timings: Arc<Mutex<Vec<Instant>>>,
}
impl TimingsHandler {
pub fn new() -> Self {
Self { timings: Arc::new(Mutex::new(Vec::new())) }
}
}
impl UpdateHandler for TimingsHandler {
fn state_updated(&mut self, _state_id: usize, _state: &State) {
self.timings.lock().unwrap().push(Instant::now());
}
fn finalize(&mut self) {
let timings = self.timings.lock().unwrap();
let mean_time_between_updates =
timings.iter().zip(timings.iter().skip(1))
.map(|(&a, b)| b.duration_since(a).as_secs_f64())
.sum::<f64>() / (timings.len() as f64);
eprintln!("Mean time between updates = {mean_time_between_updates}");
}
}
let mut engine = Example::Animals.engine().unwrap();
engine.update(
EngineUpdateConfig::with_default_transitions().n_iters(100),
TimingsHandler::new()
).unwrap();
Provided Methods§
Sourcefn global_init(&mut self, _config: &EngineUpdateConfig, _states: &[State])
fn global_init(&mut self, _config: &EngineUpdateConfig, _states: &[State])
Initialize the handler, for all states (globally).
This method is called after the states have been loaded but before any updating has occured.
Sourcefn new_state_init(&mut self, _state_id: usize, _state: &State)
fn new_state_init(&mut self, _state_id: usize, _state: &State)
Initialize for a new state.
This method is called after a specific state is loaded but before it’s individual updates have occured. Other states may be initialized before this.
Sourcefn state_updated(&mut self, _state_id: usize, _state: &State)
fn state_updated(&mut self, _state_id: usize, _state: &State)
Handler for each state update.
This method is called after each state update is complete.
Sourcefn state_complete(&mut self, _state_id: usize, _state: &State)
fn state_complete(&mut self, _state_id: usize, _state: &State)
Handle complete state updates.
This method is called after a state has completed all of its updates.
Sourcefn stop_engine(&self) -> bool
fn stop_engine(&self) -> bool
Should the Engine
stop running.
The method is called after each state update. If a true is returned, all additional updates will be canceled.
Sourcefn stop_state(&self, _state_id: usize) -> bool
fn stop_state(&self, _state_id: usize) -> bool
Should the State
stop updating.
The method is called after each state update. If a true is returned, all additional updates for the specified state will be canceled.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.