Skip to main content

Behavior

Trait Behavior 

Source
pub trait Behavior: Send + Sync {
    // Required method
    fn name(&self) -> &'static str;

    // Provided methods
    fn before_insert(
        &self,
        _ctx: &HookContext,
        _attrs: &mut HashMap<String, Value>,
    ) -> Result<(), DbError> { ... }
    fn before_update(
        &self,
        _ctx: &HookContext,
        _attrs: &mut HashMap<String, Value>,
    ) -> Result<(), DbError> { ... }
    fn before_delete(
        &self,
        _ctx: &HookContext,
        _attrs: &mut HashMap<String, Value>,
    ) -> Result<(), DbError> { ... }
    fn after_find(
        &self,
        _ctx: &HookContext,
        _attrs: &mut HashMap<String, Value>,
    ) -> Result<(), DbError> { ... }
}
Expand description

行为 trait — 可插拔代码复用单元

每个 Behavior 订阅一组生命周期事件,在事件触发时自动执行逻辑。 默认所有方法都是空实现,Behavior 只需重写关心的方法。

Required Methods§

Source

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

Behavior 名称(用于识别、去重、调试)

Provided Methods§

Source

fn before_insert( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>

在 insert 前触发(默认空实现)

Source

fn before_update( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>

在 update 前触发(默认空实现)

Source

fn before_delete( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>

在 delete 前触发(默认空实现)

Source

fn after_find( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>

在 find 后触发(默认空实现,可用于字段后处理)

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§