Code Monkey home page Code Monkey logo

super-simple-state-machines's Introduction

Super Simple State Machines

Learn Rust with super simple state machines. These are meant to showcase various design patters by building simple state machines.

Concepts

The primary goal of this is to showcase the use of generics and associated types in a practical environment. Some of these patterns may seem foreign, but are akin to some concepts found within the Substrate codebase.

Parallels to Substrate/FRAME Development

When developing within the context of a runtime environment in Substrate or FRAME, it is very often one would expect to do the following:

  1. Define context-specific errors via an enum.
  2. Evolving systems that invovle multi-trait designs.
  3. Implementing generic interfaces is common, i.e., pallets in a runtime
  4. Interaction with associated types as a means for context-specific typing (a Peer having a Status type accessible to any "parent" trait).
  5. Emphasis on proper error handling (aka, handling Option and Result as needed).

This project is intentionally abstract, which better prepares for the development paradigms that Susbtrate usually adopts.

Traits

  • Peer - The "peers" who are participating in the system. Peers get rewarded for changing the state, but only if they are an Author. If they aren't - they get put in Timeout and cannot author new changes. Each peer has a Status, which is generic.

  • FiniteStateMachine - THe base trait for how our state machine may operate. The state is abstracted away. This allows for mutliple state machines to be created with the same expectations, which could also lead to them communicating with one another. It accepts three generic parameters to denote the peer, state, and error.

Machines

  • Boolean status machine (machine_simple_impl.rs) - simply tracks status as on (true) or off (false). Picks the next participant in the peer list, and keeps cycling forever.

Running & Testing

For running a simple state machine example:

cargo run

Which should yield the following output:

For running tests:

# For all test
cargo test

# For a specific test
cargo test machine_simple_impl

super-simple-state-machines's People

Contributors

crackthecode016 avatar

Watchers

James Cloos avatar  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.