dipper/dyn_mod/module/module_traits.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
use std::any::Any;
use crate::dyn_mod::ModuleBuildContext;
/// A module represents a group of services. By implementing traits such as
/// [`HasComponent`] on a module, service dependencies are checked at compile
/// time. At runtime, modules hold the components they are associated with.
///
/// Modules can also use other modules as submodules, importing specific
/// services for the root module's use. For more details, see the [`module`]
/// macro.
///
/// Modules are usually created via the [`module`] macro.
///
/// # Example
/// ```
/// use dipper::{module, Component, Interface};
///
/// trait MyComponent: Interface {}
///
/// #[derive(Component)]
/// #[dipper(interface = MyComponent)]
/// struct MyComponentImpl;
/// impl MyComponent for MyComponentImpl {}
///
/// // MyModule implements Module and HasComponent<dyn MyComponent>
/// module! {
/// MyModule {
/// components = [MyComponentImpl],
/// providers = []
/// }
/// }
/// # fn main() {}
/// ```
///
/// [`HasComponent`]: trait.HasComponent.html
/// [`module`]: macro.module.html
pub trait Module: ModuleInterface {
/// A container for this module's submodules.
type Submodules;
/// Create the module instance by resolving the components this module
/// provides.
fn build(context: ModuleBuildContext<Self>) -> Self
where
Self: Sized;
}
#[cfg(not(feature = "thread_safe"))]
trait_alias!(
/// Submodules must be `'static` in order to be stored in other modules
/// (hence the `Any` requirement).
///
/// The `thread_safe` feature is turned off, so submodules do not need to
/// implement `Send` or `Sync`.
pub ModuleInterface = Any
);
#[cfg(feature = "thread_safe")]
trait_alias!(
/// Submodules must be `'static` in order to be stored in other modules
/// (hence the `Any` requirement).
///
/// The `thread_safe` feature is turned on, which requires submodules to
/// also implement `Send` and `Sync`.
pub ModuleInterface = Any + Send + Sync
);