Crate state_shift

Crate state_shift 

Source
Expand description

state-shift is a procedural macro crate designed to:

  • hide away the complexities come with type-state pattern,
  • make your code more readable and maintainable,
  • and still benefit from the power of type-state pattern.

Type-state is a design pattern that leverages the type system to enforce valid states and transitions at compile time. This crate provides attribute macros to transform structures and methods into type-safe stateful components, ensuring that methods are only callable in valid states, and enforcing transitions between them.

Macros:

  • #[require]: Enforces that a method can only be called when the provided state is active.
  • #[switch_to]: Modifies the return type of methods to switch between states.
  • #[impl_state]: Defines the valid states for a given type and generates corresponding marker structs and trait implementations.
  • #[type_state]: Transforms the struct into type-state compatible form, using state slots and default states.

Attribute Macrosยง

impl_state
Modifies the methods in an impl block to work with the type-state pattern.
require
Denotes which state is required for this method to be called.
switch_to
Denotes to which state will the object transition into after this method
type_state
Turns your struct into type-state compatible version.