Skip to main content

ComponentDescriptor

Trait ComponentDescriptor 

Source
pub trait ComponentDescriptor: CompInit {
    const DEP_IDS: &'static [fn() -> TypeId];
    const SCOPE: Scope;

    // Required method
    fn build(ctx: &mut BuildContext) -> Self;
}
Expand description

组件描述符 trait,用于在编译期定义组件的元数据和构建逻辑。

该 trait 由 #[tx_comp] 宏自动生成实现,是依赖注入框架的核心接口。 每个被标记为组件的结构体都必须实现此 trait,以提供以下信息:

  • 组件的依赖关系(通过 DEP_IDS
  • 组件的作用域(通过 SCOPE
  • 组件的构建方式(通过 build 方法)

§Trait 约束

  • Any: 支持运行时类型识别,用于类型擦除和向下转型
  • Sized: 编译期已知大小,确保可以存储在栈上
  • Send + Sync: 支持跨线程安全传递和共享,因为组件会被存入 Arc
  • 'static: 生命周期为静态,确保组件可以长期存活

Required Associated Constants§

Source

const DEP_IDS: &'static [fn() -> TypeId]

组件的依赖列表,存储为返回 TypeId 的函数指针数组。

每个元素是一个返回依赖类型 TypeId 的函数,用于在运行时解析依赖关系。 该数组由宏根据结构体字段自动生成,不包含带有 #[tx_cst] 属性的字段。

§示例
// 如果 UserService 依赖 DbPool 和 AppConfig
const DEP_IDS: &'static [fn() -> TypeId] = &[
    std::any::TypeId::of::<DbPool>,
    std::any::TypeId::of::<AppConfig>,
];
Source

const SCOPE: Scope

组件的作用域,决定实例的生命周期和共享策略。

  • Scope::Singleton: 全局单例,首次注入时构建并缓存,后续注入返回相同的 Arc<T>
  • Scope::Prototype: 原型模式,每次注入都调用工厂函数创建新实例

该常量由 #[tx_comp] 宏根据属性参数自动生成,默认为 Singleton

Required Methods§

Source

fn build(ctx: &mut BuildContext) -> Self

构建组件实例。

该方法由框架在初始化阶段调用,用于创建组件实例。对于无字段的组件, 通常返回 Self {};对于有依赖的组件,会通过 ctx.inject::<T>() 注入依赖。

§参数
  • ctx - 构建上下文,用于注入依赖组件和管理组件生命周期
§返回值

返回构建完成的组件实例

§注意

该方法仅在组件首次被需要时调用(对于 Singleton),或者每次注入时调用(对于 Prototype)。 不应该手动调用此方法,应该通过 BuildContext::inject::<T>() 来获取组件实例。

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§

Source§

impl ComponentDescriptor for AppAllConfig

Source§

const DEP_IDS: &'static [fn() -> TypeId]

Source§

const SCOPE: Scope = Scope::Singleton