Crate enum_update

Source
Expand description

This crate provides several derive macros that help with representing changes to structs using enums.

For api documentation, see the EnumUpdate documentation. Otherwise, here is an example of two threads using the generated enum type to keep their state in sync.

#[derive(Debug, EnumUpdate, Clone, EnumUpdateSetters)]
#[enum_update(derive(Debug, Clone, PartialEq))]
pub struct SharedState {
    value: String,
}
let mut thread_a_state = SharedState { value: "Hello".to_string() };
let mut thread_b_state = thread_a_state.clone();

let (sender, receiver) = std::sync::mpsc::sync_channel(1);
let thread_a = std::thread::Builder::new()
    .spawn(move || {
        let change = thread_a_state.modify_value("World".to_string());
        sender.send(change).unwrap();
    })
    .unwrap();
let thread_b = std::thread::Builder::new()
    .spawn(move || {
        assert_eq!(thread_b_state.value, "H
ello".to_string());
        // now, we receive the change
        let change = receiver.recv().unwrap();
        assert_eq!(change, SharedStateUpdate::Value("World".to_string()));
        // applying the change
        thread_b_state.apply(change);
        // it becomes true
        assert_eq!(thread_b_state.value, "World".to_string());
    })
    .unwrap();

Traits§

EnumUpdate
Implemented on structs that have their updates represented by some enum U. Implement this trait using the derive macro EnumUpdate.

Derive Macros§

EnumUpdate
Generates an enum representing updates to a given struct. See the README.md for an overview.
EnumUpdateSetters
Generates setter methods that also return enum updates.