Skip to main content

tx_di_core/
lib.rs

1//! # tx-di-core
2//!
3//! 类型驱动的 Rust 依赖注入框架。
4//!
5//! ## 核心概念
6//!
7//! - **Component trait** — 每个被 DI 管理的类型实现此 trait,用 associated type 声明依赖
8//! - **ComponentMeta** — 瘦注册条目,linkme 编译期收集,运行期拓扑排序
9//! - **Store** — 类型擦除的组件存储(DashMap<TypeId, CompRef>),运行期解析依赖
10//! - **AOP** — Interceptor trait + proc_macro 代理,零运行时开销
11//!
12//! ## 设计原则
13//!
14//! 1. 类型驱动:依赖在 `type Deps` 中声明,编译期可知
15//! 2. 编译期收集:linkme 零开销注册
16//! 3. 运行期解析:拓扑排序 + DashMap 存储
17//! 4. 可扩展:ComponentMeta 只存核心字段,生命周期钩子在 trait 默认方法中
18
19pub mod aop;
20pub mod component;
21pub mod config;
22pub mod error;
23pub mod lifecycle;
24pub mod registry;
25pub mod scope;
26pub mod store;
27pub mod topology;
28
29// ── 第三方 re-export ──────────────────────────────────────────────────────
30pub use dashmap;
31pub use dashmap::DashMap;
32pub use linkme;
33pub use toml;
34pub use toml::Value;
35pub use toml::map;
36
37// ── 内部模块 re-export ────────────────────────────────────────────────────
38// 注意:derive 宏 `Component` 和 trait `Component` 同名但不同命名空间,可以共存
39// `tx_cst` 和 `component` 是 derive 辅助属性,不需要单独 re-export
40pub use tx_di_macros::Component;   // derive 宏(宏命名空间)
41pub use tx_di_macros::intercept;    // AOP 方法拦截属性宏(#[intercept])
42pub use tx_error::{AppErrCode, AppError, AppResult, CodeMsg};
43pub use crate::error::DiErr;
44pub use tx_common::{ApiR, ApiRes, FormattedDateTime, RCode};
45
46/// RIE<T> = AppResult<T>
47pub type RIE<T> = AppResult<T>;
48
49pub use tokio_util::sync::CancellationToken;
50
51// ── 核心 re-export ────────────────────────────────────────────────────────
52pub use component::{BoxFuture, Component, DepsTuple};
53pub use config::AppAllConfig;
54// 内部错误模块:直接复用 tx_error 提供的统一错误类型
55// 详见 src/error.rs
56pub use lifecycle::{App, BuildContext, InnerContext, get_sys_config, set_sys_config, CONFIG_PATH};
57pub use registry::{ComponentMeta, COMPONENT_REGISTRY};
58pub use scope::Scope;
59pub use store::{Store, CompRef, TraitImplEntry, TraitImplMap, inject_from_store, inject_trait_from_store, inject_all_traits_from_store};
60pub use topology::topo_sort;
61pub use aop::{CallContext, CallResult, Interceptor, InterceptorChain};
62
63