pub struct ConfiguredModule<M, O> { /* private fields */ }Expand description
The configured form of module M: what M::for_root(options) and
M::from_config() return, so a module does not repeat the wrapper that
carries its id, says it is configured and hands its options to register.
register is the module’s own function. It gets Some(options) from
for_root, and None from
from_config, which means “read the
section of the app’s configuration” (a plain M registers with None
too).
use navree_kernel::{ConfiguredModule, Module, ModuleBuilder, Navree};
#[derive(Clone)]
struct GreetingOptions(&'static str);
struct GreetingModule;
impl GreetingModule {
pub fn for_root(options: GreetingOptions) -> impl Module {
ConfiguredModule::<GreetingModule, _>::for_root(options, register)
}
}
impl Module for GreetingModule {
fn register(&self, m: &mut ModuleBuilder) {
register(m, None);
}
}
fn register(m: &mut ModuleBuilder, options: Option<GreetingOptions>) {
let greeting = options.map_or("hello", |options| options.0);
m.value(greeting.to_owned()).exports::<String>();
}
// The configured instance has the plain module's id.
let configured = GreetingModule::for_root(GreetingOptions("namaste"));
assert_eq!(configured.id(), GreetingModule.id());
assert!(configured.is_configured());
let report = Navree::app().module(configured).validate().expect("valid");
// The app's root module and GreetingModule.
assert_eq!(report.modules.len(), 2);Implementations§
Source§impl<M, O> ConfiguredModule<M, O>
impl<M, O> ConfiguredModule<M, O>
Sourcepub fn for_root(options: O, register: fn(&mut ModuleBuilder, Option<O>)) -> Self
pub fn for_root(options: O, register: fn(&mut ModuleBuilder, Option<O>)) -> Self
M configured with options (configure a module once, in the app
root).
Sourcepub fn from_config(register: fn(&mut ModuleBuilder, Option<O>)) -> Self
pub fn from_config(register: fn(&mut ModuleBuilder, Option<O>)) -> Self
M configured from the app’s configuration: register gets None.
Trait Implementations§
Source§impl<M: 'static, O: Clone + Send + Sync + 'static> Module for ConfiguredModule<M, O>
impl<M: 'static, O: Clone + Send + Sync + 'static> Module for ConfiguredModule<M, O>
Source§fn id(&self) -> ModuleId
fn id(&self) -> ModuleId
The module identity.
for_root wrappers override this to return the id
of the module type they configure.Source§fn is_configured(&self) -> bool
fn is_configured(&self) -> bool
for_root(...) values return true; at most one configured instance per id.Source§fn register(&self, m: &mut ModuleBuilder)
fn register(&self, m: &mut ModuleBuilder)
Registers everything the module contributes.
Auto Trait Implementations§
impl<M, O> Forget for ConfiguredModule<M, O>
impl<M, O> Freeze for ConfiguredModule<M, O>
impl<M, O> Move for ConfiguredModule<M, O>
impl<M, O> RefUnwindSafe for ConfiguredModule<M, O>where
Option<O>: RefUnwindSafe,
fn(&mut ModuleBuilder, Option<O>): RefUnwindSafe,
PhantomData<fn() -> M>: RefUnwindSafe,
impl<M, O> Send for ConfiguredModule<M, O>
impl<M, O> Sync for ConfiguredModule<M, O>
impl<M, O> Unpin for ConfiguredModule<M, O>
impl<M, O> UnsafeUnpin for ConfiguredModule<M, O>where
Option<O>: UnsafeUnpin,
fn(&mut ModuleBuilder, Option<O>): UnsafeUnpin,
PhantomData<fn() -> M>: UnsafeUnpin,
impl<M, O> UnwindSafe for ConfiguredModule<M, O>where
Option<O>: UnwindSafe,
fn(&mut ModuleBuilder, Option<O>): UnwindSafe,
PhantomData<fn() -> M>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more