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§
Sourcefn component_name() -> &'static str
fn component_name() -> &'static str
获取组件的名称,用于在容器中唯一标识该组件 默认实现使用结构体的下划线命名格式
Sourcefn from_container() -> impl Future<Output = &'static Self> + Send
fn from_container() -> impl Future<Output = &'static Self> + Send
初始化组件 这是一个异步方法,返回一个 Future,最终产生一个静态引用的组件实例 该方法在容器预热或首次请求时调用
Provided Methods§
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.