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§
Provided Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".