Skip to main content

Module

Trait Module 

Source
pub trait Module: MaybeSend + 'static {
    // Required methods
    fn name(&self) -> &'static str;
    fn accepts(&self, tick: &Tick) -> bool;
    fn handle(
        &mut self,
        tick: &Tick,
        now_ms: u64,
        out: &mut EffectSink,
    ) -> Result<(), CoreError>;

    // Provided methods
    fn on_start(&mut self, out: &mut EffectSink) -> Result<(), CoreError> { ... }
    fn on_stop(&mut self, out: &mut EffectSink) -> Result<(), CoreError> { ... }
}
Expand description

确定性业务模块的完整契约。

§实现约束(不可违反)

  • accepts 和 handle 必须是纯函数(无 I/O,无 await,无 spawn)
  • handle 通过 &mut EffectSink 输出 Effect,不直接调用任何 port
  • 周期心跳:在 on_start 吐 ScheduleTimer{id, after_ms}, 在 handle(Tick::Timer{id}) 重新 arm,实现无限循环心跳

Required Methods§

Source

fn name(&self) -> &'static str

模块唯一名称(用于日志 / 调试 / error 报告)

Source

fn accepts(&self, tick: &Tick) -> bool

路由判定:此模块是否处理该 Tick。

§约束
  • 仅用于 Tick::Inbound 和 Tick::Command 的路由
  • Tick::PortReply / Tick::PortProgress 和 Tick::Timer 不调用此方法,走映射定向路由
  • 必须是纯函数(不修改 self)
Source

fn handle( &mut self, tick: &Tick, now_ms: u64, out: &mut EffectSink, ) -> Result<(), CoreError>

处理一个 Tick,将 Effect 写入 out。

§不变量(不可违反)
  • 严格同步:零 await,零 I/O,零 spawn
  • 不直接调用任何 port trait
  • 所有副作用通过 out.push(Effect::...) 表达

Provided Methods§

Source

fn on_start(&mut self, out: &mut EffectSink) -> Result<(), CoreError>

模块启动钩子。

在 engine.start() 时调用,输出初始 Effect:

  • Effect::Send{hello 帧}(WS 握手)
  • Effect::Persist{get cursor, corr}(读取持久化状态)
  • Effect::ScheduleTimer{ping, 8s}(arm 初始心跳)
Source

fn on_stop(&mut self, out: &mut EffectSink) -> Result<(), CoreError>

模块停止钩子。

输出清理 Effect:

  • Effect::CancelTimer 所有 timer
  • Effect::Send{close 帧}

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§