injection 0.1.1

A lightweight dependency injection container for Rust applications
Documentation
use crate::container::InjectionContainer;
use serde::Serialize;
use std::any::Any;

/// 组件 trait,所有可被依赖注入的组件都必须实现此 trait
/// 该 trait 要求类型必须是线程安全的(Send + Sync),并且可以是任何类型(Any)
pub trait Component: Any + Send + Sync {
    /// 获取组件的名称,用于在容器中唯一标识该组件
    /// 默认实现使用结构体的下划线命名格式
    fn component_name() -> &'static str;
    
    /// 初始化组件
    /// 这是一个异步方法,返回一个 Future,最终产生一个静态引用的组件实例
    /// 该方法在容器预热或首次请求时调用
    fn from_container() -> impl Future<Output = &'static Self> + Send;

    /// 销毁组件时的清理操作(可选实现)
    /// 这是一个异步方法,在组件销毁前调用
    /// 默认实现为空操作
    fn destroy(&self) -> impl Future<Output = ()> + Send{
        async{}
    }
}

/// 配置 trait,用于标记可以从配置文件加载的配置结构体
/// 仅在启用 "config" 特性时可用
#[cfg(feature = "config")]
pub trait Configure: serde::de::DeserializeOwned+ Serialize + Default + Send + Sync + 'static {
    /// 获取配置的前缀键名
    /// 用于从配置文件中提取对应的配置节
    fn prefix() -> &'static str;
    
    /// 从容器中加载配置
    /// 默认实现从 InjectionContainer 中获取对应前缀的配置
    fn from_container() -> Self {
        InjectionContainer::get_config()
    }
}