pub struct Container { /* private fields */ }
Expand description
Contains providers for resolvable types.
The macro container!
simplifies the construction of Container
s calling
container.install(..)
and
container.install_ref(..)
(if ref
keyword is supplied)
on the macro arguments provided.
To resolve a dependecy by using the installed providers, methods
container.get()
and
container.get_ref()
are called for values and references,
respectively.
§Example
use ::inject::*;
let reference_provider = Box::new(5usize);
let container = container![
|container: &Container| Ok(2i32),
ref reference_provider,
];
assert_eq!(5usize, *container.get_ref().unwrap());
assert_eq!(2i32, container.get().unwrap());
Implementations§
Source§impl Container
impl Container
Sourcepub fn new() -> Container
pub fn new() -> Container
Create a new Container
, used to store implementors of
Provider
s and RefProvider
s.
Sourcepub fn install<T: Inject, P: 'static + Provider<ProvidedType = T>>(
&mut self,
provider: P,
)
pub fn install<T: Inject, P: 'static + Provider<ProvidedType = T>>( &mut self, provider: P, )
Install a Provider
into this Container
Sourcepub fn install_ref<T: Inject, P: 'static + RefProvider<ProvidedRef = T>>(
&mut self,
provider: P,
)
pub fn install_ref<T: Inject, P: 'static + RefProvider<ProvidedRef = T>>( &mut self, provider: P, )
Install a RefProvider
into this Container
Sourcepub fn get<T: Inject>(&self) -> Result<T, InjectError>
pub fn get<T: Inject>(&self) -> Result<T, InjectError>
Resolve a value-type from the installed Provider
s.
Sourcepub fn get_ref<T: 'static>(&self) -> Result<&T, InjectError>
pub fn get_ref<T: 'static>(&self) -> Result<&T, InjectError>
Resolve a reference-type from the installed RefProvider
s.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Container
impl !RefUnwindSafe for Container
impl !Send for Container
impl !Sync for Container
impl Unpin for Container
impl !UnwindSafe for Container
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