Skip to main content

SingletonStore

Trait SingletonStore 

Source
pub trait SingletonStore:
    Send
    + Sync
    + 'static {
    // Required method
    fn len(&self) -> usize;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn validate(&self) -> Result<(), String> { ... }
}
Expand description

Trait for generated typed singleton stores.

The #[derive(Injectable)] macro contributes to a single generated store struct that has one OnceCell<Arc<T>> per singleton-scoped injectable type.

§Design Rationale

Traditional DI containers use HashMap<TypeId, Box<dyn Any>> which requires:

  • Runtime type lookup
  • Dynamic downcasting with Any::downcast_ref
  • Loss of compiler guarantees

Our generated store uses named fields with concrete types:

pub struct AppSingletonStore {
    database: OnceCell<Arc<Database>>,
    cache: OnceCell<Arc<Cache>>,
}

impl AppSingletonStore {
    pub async fn database(&self, ctx: &ResolveContext) -> Arc<Database> { ... }
    pub async fn cache(&self, ctx: &ResolveContext) -> Arc<Cache> { ... }
}

This is completely typed, zero-cost, and requires no Any or TypeId.

Required Methods§

Source

fn len(&self) -> usize

Returns the number of singleton entries in the store.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the store contains no entries.

Source

fn validate(&self) -> Result<(), String>

Validate all singleton entries at startup.

This performs basic sanity checks (e.g., no unresolved references) and is called during container build.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§