Skip to main content

Store

Struct Store 

Source
pub struct Store { /* private fields */ }
Expand description

组件存储 — 类型安全的注入入口

Implementations§

Source§

impl Store

Source

pub fn new() -> Self

创建空 Store

Source

pub fn from_dashmap(inner: DashMap<TypeId, CompRef>) -> Self

从 DashMap 创建 Store(trait_impls 为空,需后续填充)

Source

pub fn inner(&self) -> &DashMap<TypeId, CompRef>

获取内部 DashMap 的引用

Source

pub fn into_inner(self) -> DashMap<TypeId, CompRef>

获取内部 DashMap 的所有权(消耗 self)

Source

pub fn insert_cached<T: Any + Send + Sync>(&self, value: T)

注册缓存实例(Singleton)

Source

pub fn insert_arc<T: Any + Send + Sync>(&self, arc: Arc<T>)

注册已 Arc 包装的缓存实例

Source

pub fn insert_factory<T: Any + Send + Sync, F>(&self, factory: F)
where F: Fn(&Store) -> Arc<T> + Send + Sync + 'static,

注册工厂闭包(Prototype)

每次注入时调用工厂,构造新实例。 T 为组件类型,TypeId 通过 TypeId::of::<T>() 自动获取。

Source

pub fn inject<T: Component>(&self) -> Result<Arc<T>, AppError>

注入组件实例(类型安全)

  • Singleton:返回缓存的 Arc<T>
  • Prototype:调用工厂闭包,每次构造新实例
§Panics

组件未注册时 panic(编程错误,不是运行时错误)。

Source

pub fn inject_or_panic<T: Component>(&self) -> Arc<T> ⓘ

注入组件实例(类型安全)— 直接返回 Arc,失败时 panic

这是 inject() 的便捷版本,用于不需要错误处理的场景。

Source

pub fn try_inject<T: Component>(&self) -> Option<Arc<T>>

尝试注入,失败返回 None

Source

pub fn contains<T: Any + Send + Sync>(&self) -> bool

检查组件是否已注册

Source

pub fn len(&self) -> usize

已注册组件数量

Source

pub fn is_empty(&self) -> bool

是否为空

Trait Implementations§

Source§

impl Default for Store

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Store

§

impl !UnwindSafe for Store

§

impl Freeze for Store

§

impl Send for Store

§

impl Sync for Store

§

impl Unpin for Store

§

impl UnsafeUnpin for Store

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more