Trait UpdateHandler

Source
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§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn finalize(&mut self)

Cleanup upon the end of updating.

This method is called when all updating is complete. Uses for this method include cleanup, report generation, etc.

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.

Implementations on Foreign Types§

Source§

impl UpdateHandler for ()

Source§

impl<A> UpdateHandler for (A,)
where A: UpdateHandler,

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B> UpdateHandler for (A, B)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C> UpdateHandler for (A, B, C)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D> UpdateHandler for (A, B, C, D)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D, E> UpdateHandler for (A, B, C, D, E)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D, E, F> UpdateHandler for (A, B, C, D, E, F)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D, E, F, G> UpdateHandler for (A, B, C, D, E, F, G)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D, E, F, G, H> UpdateHandler for (A, B, C, D, E, F, G, H)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<A, B, C, D, E, F, G, H, I> UpdateHandler for (A, B, C, D, E, F, G, H, I)

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn stop_state(&self, state_id: usize) -> bool

Source§

fn finalize(&mut self)

Source§

impl<T> UpdateHandler for Vec<T>
where T: UpdateHandler,

Source§

fn global_init(&mut self, config: &EngineUpdateConfig, states: &[State])

Source§

fn state_updated(&mut self, state_id: usize, state: &State)

Source§

fn stop_engine(&self) -> bool

Source§

fn new_state_init(&mut self, state_id: usize, state: &State)

Source§

fn state_complete(&mut self, state_id: usize, state: &State)

Source§

fn stop_state(&self, _state_id: usize) -> bool

Source§

fn finalize(&mut self)

Implementors§