Skip to main content

sword_core/injectables/
mod.rs

1mod components;
2mod container;
3mod error;
4mod providers;
5
6use crate::State;
7
8use std::{
9    any::{Any, TypeId},
10    sync::Arc,
11};
12
13pub use components::{Component, ComponentRegistry};
14pub use container::DependencyContainer;
15pub use error::DependencyInjectionError;
16pub use providers::{Provider, ProviderRegistry};
17
18/// Base trait for any component that can be constructed from the application State.
19pub trait Build: Clone + Send + Sync + 'static {
20    fn build(state: &State) -> Result<Self, DependencyInjectionError>
21    where
22        Self: Sized;
23}
24
25/// Trait for components that have dependencies on other components.
26///
27/// The `deps()` method returns a list of `TypeId`s of the dependencies
28/// required to build the component.
29pub trait HasDeps: Build {
30    fn deps() -> Vec<TypeId> {
31        Vec::new()
32    }
33}
34
35/// Pointer to dyn Any element. It retrieves dynamic capabilites
36/// to the dependency container. Basically represents Any element.
37pub(crate) type Injectable = Arc<dyn Any + Send + Sync>;