enum-update
enum-update
is a set of macros and traits for representing state changes as enums.
Note:
enum-update
is currently atv0.1.0
and is not at stablev1.0.0
yet. Overall functionality should stay the same, but trait and macro names may change between versions.
Adding enum-update
to your project
Import enum-update
into your project by running this command.
All enum-update-derive
macros are re-exported by enum-update
. There is no need to manually add enum-update-derive
to your project dependency list.
Macros
The following derive macros are provided by enum-update
Derive macro | Description |
---|---|
EnumUpdate |
Creates a new enum representing updates to a struct |
EnumUpdateSetters |
Add setter methods to a struct that also return enums generated by EnumUpdate |
Examples
EnumUpdate
Basic example
use *;
// `EnumUpdate` will generate:
// pub enum MyStateUpdate {
// Value(String),
// AnotherValue(String),
// }
// impl EnumUpdate<MyStateUpdate> for MyState {
// fn apply(&mut self, update: MyStateUpdate) {
// match update {
// MyStateUpdate::Value(value) => {
// self.value = value;
// },
// MyStateUpdate::AnotherValue(another_value) => {
// self.another_value = another_value;
// }
// }
// }
// }
let mut state = MyState ;
let first_change = Value;
state.apply;
assert_eq!;
EnumUpdateSetters
Basic example
use *;
// `EnumUpdateSetters` will generate:
// impl<'a> MyState<'a> {
// fn modify_value(&mut self, value: String) -> MyStateUpdate {
// self.value = value.clone();
// }
// fn modify_my_reference(&mut self, my_reference: &'a str) -> MyStateUpdate {
// self.my_reference = my_reference;
// }
// }
let mut state: = MyState ;
state.modify_my_reference;
assert_eq!;
Contributing
Contributions and bug fixes are always welcome. Please open an issue first before implementing a feature or fixing a bug. It is possible that I am already implementing it locally.