Skip to main content

MutatorMiddleware

Trait MutatorMiddleware 

Source
pub trait MutatorMiddleware<Evt>: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn process(&self, event: Evt) -> MutatorAction<Evt>;
}
Expand description

可变处理器——接收 Evt,选择放行/修改/阻止。

InspectorMiddleware 不同,mutator 在事件流程中同步顺序执行, 其返回的 MutatorAction 直接决定事件是否继续传递。

§示例

struct Censor;
impl MutatorMiddleware<String> for Censor {
    fn name(&self) -> &str { "censor" }
    fn process(&self, event: String) -> MutatorAction<String> {
        if event.contains("bad") {
            MutatorAction::Modify(event.replace("bad", "***"))
        } else {
            MutatorAction::Block { reason: "contains bad word".into() }
        }
    }
}

Required Methods§

Source

fn name(&self) -> &str

返回此 mutator 的唯一标识名称。

Source

fn process(&self, event: Evt) -> MutatorAction<Evt>

处理事件。支持三种决策:

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§