Struct state_governor::Governor[][src]

pub struct Governor<const N: usize> {
    pub states: LinearMap<u8, State, N>,
    pub current_state: Option<State>,
    pub previous_state: Option<State>,
    pub transition_function: Option<fn(_: Option<State>, _: Option<State>) -> bool>,
}
Expand description

Governor

Manages all state transitions

Fields

states: LinearMap<u8, State, N>current_state: Option<State>previous_state: Option<State>transition_function: Option<fn(_: Option<State>, _: Option<State>) -> bool>

Implementations

Returns new governor instance

 let governor = Governor::<3>::new(); // Three indicates the maximum amount of states that this governor can handle.

Adds new state to governor. Returns true if successful otherwise returns false

 let mut governor = Governor::<3>::new(); // Three indicates the maximum amount of states that this governor can handle.
 governor.add_state(State::new(0x1, "NEW"));

Changes current state to desired one. Returns true if successful otherwise returns false

 let mut governor = Governor::<3>::new(); // Three indicates the maximum amount of states that this governor can handle.
 governor.add_state(State::new(0x1, "NEW"));
 let mut result = governor.change_state_to(0x1);
 assert_eq!(result, true);
 result = governor.change_state_to(0x2);
 assert_eq!(result, false);

Returns current state information

 GOVERNOR.add_state(State::new(0x1, "NEW"));
 let mut result = GOVERNOR.change_state_to(0x1);
 let s = GOVERNOR.get_current_state();
 assert_eq!(s.id(), 0x1);

Sets state transition function. State transition function will be triggered on every state transition event automatically.

 let mut governor = Governor::<3>::new(); // Three indicates the maximum amount of states that this governor can handle.
 governor.add_state(State::new(0x1, "NEW"));
 governor.add_state(State::new(0x2, "OLD"));
 static mut CALLED: bool = false;
 let mut result = governor.change_state_to(0x1);
 fn on_transition(curr_state: Option<State>, next_state: Option<State>) -> bool {
     assert_eq!(curr_state.unwrap().id(), 0x1, "Check that if current state is expected one.");
     assert_eq!(next_state.unwrap().id(), 0x2, "Check that if next state is expected one.");
     unsafe { CALLED = true; }
     return true;
 }
 governor.set_state_transition_func(on_transition);
 result = governor.change_state_to(0x2);
 assert_eq!(result, true);
 unsafe { assert_eq!(CALLED, true); }

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.