Skip to main content

StateMutation

Trait StateMutation 

Source
pub trait StateMutation<S>:
    Sized
    + Send
    + Sync
    + Debug {
    // Required method
    fn apply(self, state: &mut S);

    // Provided method
    fn combine(self, _other: Self) -> Option<Self> { ... }
}
Expand description

状态变更命令 — 描述一次对 State 的确定性修改。

Mutation 自己知道如何修改对应的 State(apply(self, &mut S))。 State 只是数据,Mutation 是变更逻辑,Executor 负责调度。

§设计原则

  • Command 而非 PatchAppendMessage 而非 SetMessages
  • Enum 分发:顶层 enum 只做一层 match,具体逻辑在各 variant 的 apply()
  • 无 Serialize 强制:只有需要 Replay 的运行时才加 Serialize bound

Required Methods§

Source

fn apply(self, state: &mut S)

将此 Mutation 应用到目标 State。

这是 Mutation 的核心职责:每个 variant 独立实现, 顶层 enum 只做一层 dispatch。

Provided Methods§

Source

fn combine(self, _other: Self) -> Option<Self>

将此 Mutation 合并到另一个同类型 Mutation 中(可选)。

用于批量场景:多个 Mutation 合并为一个,减少 apply 次数。 默认返回 None 表示不可合并。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§