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};
17pub use sword_macros::injectable;
18
19/// Base trait for any component that can be constructed from the application State.
20pub trait Build: Clone + Send + Sync + 'static {
21    fn build(state: &State) -> Result<Self, DependencyInjectionError>
22    where
23        Self: Sized;
24}
25
26/// Trait for components that have dependencies on other components.
27///
28/// The `deps()` method returns a list of `TypeId`s of the dependencies
29/// required to build the component.
30pub trait HasDeps: Build {
31    fn deps() -> Vec<TypeId> {
32        Vec::new()
33    }
34}
35
36/// Pointer to dyn Any element. It retrieves dynamic capabilites
37/// to the dependency container. Basically represents Any element.
38pub type Injectable = Arc<dyn Any + Send + Sync>;