typestate-enum

Crates.iotypestate-enum
lib.rstypestate-enum
version0.0.1
sourcesrc
created_at2023-11-05 10:43:56.526111
updated_at2023-11-05 10:43:56.526111
descriptionA macro to help build simple Typestate APIs.
homepage
repositoryhttps://github.com/gwllx/typestate-enum
max_upload_size
id1025863
size5,339
Gareth Williamson (gwllx)

documentation

README

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()
    }
}
Commit count: 8

cargo fmt