An open API service indexing awesome lists of open source software.

https://github.com/gwllx/typestate-enum

A Rust macro to help build simple Typestate APIs.
https://github.com/gwllx/typestate-enum

rust rust-macros typestate

Last synced: about 2 months ago
JSON representation

A Rust macro to help build simple Typestate APIs.

Awesome Lists containing this project

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.

```rust
use typestate_enum::typestate_enum;
use std::marker::PhantomData;

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

struct Action(PhantomData);

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

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

impl Action {
fn complete_work(self) -> Action {
Action::new()
}
}
```