Skip to main content

injectable_rs_runtime/
injectable.rs

1//! The `Injectable` trait — the primary marker for injectable types.
2//!
3/// Every type that wants to participate in the DI system must derive or
4/// implement `Injectable`. The derive macro generates:
5///
6/// - An associated `Provider` type implementing [`Provider<T>`]
7/// - Static dependency metadata for graph validation
8/// - Lifecycle hook orchestration
9use crate::Provider;
10
11/// The primary trait for types that can be dependency-injected.
12///
13/// # Derivable
14///
15/// This trait is normally derived via `#[derive(Injectable)]`:
16///
17/// ```rust,ignore
18/// #[derive(Injectable)]
19/// pub struct Database {
20///     pool_size: usize,
21/// }
22/// ```
23///
24/// The derive macro generates:
25/// - A `<Type>Provider` struct implementing `Provider<Type>`
26/// - All necessary `Extract` calls for constructor parameters
27/// - Lifecycle hook invocation (`post_construct`, `pre_destruct`)
28///
29/// # Associated Types
30///
31/// - `Provider`: The compile-time generated provider that knows how to
32///   construct this type, including all its transitive dependencies.
33///
34/// # Bounds
35///
36/// All injectable types must be `Send + Sync + 'static` to support
37/// async construction and shared access across threads.
38pub trait Injectable: Send + Sync + Sized + 'static {
39    /// The provider type that knows how to construct `Self`.
40    ///
41    /// Generated by the `#[derive(Injectable)]` macro. Each provider
42    /// statically encodes its dependency tree through `Extract` calls.
43    type Provider: Provider<Self>;
44
45    /// Whether this type is singleton-scoped (constructed once and cached).
46    ///
47    /// Defaults to `true`. Transient-scoped types generated by the macro
48    /// override this to `false` so the cache is bypassed on every resolution.
49    const IS_SINGLETON: bool = true;
50}