Code Monkey home page Code Monkey logo

fsm-rs's Introduction

A simple Finite State Machine library in Rust. Provide State and Event types (usually enums), then generate a machine with an initial state, give it some transition behaviours and you have your state machine!

Usage

Using the simple coin-operated turnstyle example from the FSM wikipedia entry:

Define your states and events:

// states and events must be C-like enums (copyable and easily converted to primitives)
#[derive(Copy, Clone)]
enum TurnStyleState {
	Locked,
	Unlocked,
}

#[derive(Copy, Clone)]
enum TurnStyleEvent {
	Push,
	InsertCoin,
}

// implement the EnumTag trait for states and events
impl EnumTag for TurnStyleState {
	fn tag_number(&self) -> usize {
		*self as usize
	}
	fn max_tag_number() -> usize {
		TurnStyleState::Unlocked as usize
	}
}

impl EnumTag for TurnStyleEvent {
	fn tag_number(&self) -> usize {
		*self as usize
	}
	fn max_tag_number() -> usize {
		TurnStyleEvent::InsertCoin as usize
	}
}

Create your machine and define your transitions:

// create the machine initially in the Locked state
let mut machine = Machine::new(TurnStyleState::Locked);
// create the transition from Locked -> Unlocked upon inserting a coin
machine.add_transition(
	TurnStyleState::Locked, TurnStyleEvent::InsertCoin,
	TurnStyleState::Unlocked, |_,_| println!("unlocked")
);
// create the transition from Unlocked -> Locked upon pushing the turnstyle
machine.add_transition(
	TurnStyleState::Unlocked, TurnStyleEvent::Push,
	TurnStyleState::Locked, |_,_| println!("locked")
);

Trigger events as needed and huzzah, off you go:

// initially we're in the Locked state
machine.on_event(TurnStyleEvent::InsertCoin);
// now we're Unlocked, ("unlocked" was just printed)
machine.on_event(TurnStyleEvent::Push);
// now we're Locked again, ("locked" was just printed)

This example is also the test case for the library, although here I've ommitted the test-related details.

To Do

  • Expose predicate interface and write unit tests for it

Alternatives

Macro based solutions

Other

fsm-rs's People

Contributors

omaskery avatar issacsonjj avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.