Skip to main content

Component

Trait Component 

Source
pub trait Component:
    Any
    + Send
    + Sync {
    // Required methods
    fn component_name() -> &'static str;
    fn from_container() -> impl Future<Output = &'static Self> + Send;

    // Provided method
    fn destroy(&self) -> impl Future<Output = ()> + Send { ... }
}
Expand description

组件 trait,所有可被依赖注入的组件都必须实现此 trait 该 trait 要求类型必须是线程安全的(Send + Sync),并且可以是任何类型(Any)

Required Methods§

Source

fn component_name() -> &'static str

获取组件的名称,用于在容器中唯一标识该组件 默认实现使用结构体的下划线命名格式

Source

fn from_container() -> impl Future<Output = &'static Self> + Send

初始化组件 这是一个异步方法,返回一个 Future,最终产生一个静态引用的组件实例 该方法在容器预热或首次请求时调用

Provided Methods§

Source

fn destroy(&self) -> impl Future<Output = ()> + Send

销毁组件时的清理操作(可选实现) 这是一个异步方法,在组件销毁前调用 默认实现为空操作

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§