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>,
) -> BehaviorResult<()> { ... }
fn before_update(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()> { ... }
fn before_delete(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()> { ... }
fn after_find(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()> { ... }
}Expand description
行为 trait — 可插拔代码复用单元
每个 Behavior 订阅一组生命周期事件,在事件触发时自动执行逻辑。 默认所有方法都是空实现,Behavior 只需重写关心的方法。
Required Methods§
Provided Methods§
Sourcefn before_insert(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_insert( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 insert 前触发(默认空实现)
Sourcefn before_update(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_update( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 update 前触发(默认空实现)
Sourcefn before_delete(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_delete( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 delete 前触发(默认空实现)
Sourcefn after_find(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn after_find( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 find 后触发(默认空实现,可用于字段后处理)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".