typestate-enum 0.0.1

A macro to help build simple Typestate APIs.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 5.03 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.07 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • gwllx/typestate-enum
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • gwllx

typestate-enum

A Rust macro to help build simple Typestate APIs.

Example

The following example defines a trait State and 3 zero-sized types which implement it: Ready, Working, and Complete. The types can then be used to build simple Typestate APIs.

use typestate_enum::typestate_enum;
use std::marker::PhantomData;

typestate_enum! {
    pub State {
        Ready,
        Working,
        Complete
    }
}

struct Action<S: State>(PhantomData<S>);

impl<S: State> Action<S> {
    fn new() -> Self {
        Action::<S>(PhantomData)
    }
}

impl Action<Ready> {
    fn start_work(self) -> Action<Working> {
        Action::new()
    }
}

impl Action<Working> {
    fn complete_work(self) -> Action<Complete> {
        Action::new()
    }
}