]>
Commit | Line | Data |
---|---|---|
dc9dc135 XL |
1 | use std::any::TypeId; |
2 | use std::collections::HashMap; | |
3 | use std::hash::Hash; | |
4 | ||
5 | trait State { | |
6 | type EventType; | |
7 | fn get_type_id_of_state(&self) -> TypeId; | |
8 | } | |
9 | ||
10 | struct StateMachine<EventType: Hash + Eq> { | |
11 | current_state: Box<dyn State<EventType = EventType>>, | |
12 | transition_table: | |
13 | HashMap<TypeId, HashMap<EventType, fn() -> Box<dyn State<EventType = EventType>>>>, | |
14 | } | |
15 | ||
16 | impl<EventType: Hash + Eq> StateMachine<EventType> { | |
17 | fn inner_process_event(&mut self, event: EventType) -> Result<(), i8> { | |
18 | let new_state_creation_function = self | |
19 | .transition_table | |
20 | .iter() | |
21 | .find(|(&event_typeid, _)| event_typeid == self.current_state.get_type_id_of_state()) | |
22 | .ok_or(1)? | |
23 | .1 | |
24 | .iter() | |
25 | .find(|(&event_type, _)| event == event_type) | |
26 | //~^ ERROR cannot move out of a shared reference | |
27 | .ok_or(2)? | |
28 | .1; | |
29 | ||
30 | self.current_state = new_state_creation_function(); | |
31 | Ok(()) | |
32 | } | |
33 | } | |
34 | ||
35 | fn main() {} |