state-m
The library implements convenient state distribution and management mechanisms, facilitating collaborative work between components.
Features
- Separation of read-write, initiators and responders of state changes hold different data structures.
- Duplicate filtering, by default, duplicate states do not trigger state changes.
- State transition, supports type conversion of subscription state changes.
- Timing control, supports waiting for all responders to complete their responses.
Usage
- Define 'Tag' enum to distinguish different initiators or responders, all initiators must use different tag values, all responders, and all responders do the same, a same tag value can be used by an initiator and a responder in the same state machine.
- Implement 'HasStateMachine' trait for you data structure, whether it's the initiator or responder of state change, maybe you should add some fields to your data structure.
- If your data structure is also a responder of some state change, implement 'HasStateHandle' trait for your data structure. Then subscribe sources as needed. Unsubscription is optional, after your state machine is dropped, subscriptions are auto cleaned.
unit_target
.clone
.subscribe
.await;
unit_target
.clone
.
.await;
- Add state change initiators to your state machine, after added, you can get it from state machine by tag. Then change state as needed.
// add source to state machine
unit_source..await;
unit_source..await;
// change state by need
unit_source
.
.await?;
unit_source..await?;
unit_source
.modify
.await?;
unit_source
.
.await?;
unit_source
.wait_modify
.await?;
- Do unsubscrption as needed
unit_target..await;
unit_target..await;
unit_source.del_source.await;