Struct he_di::container::Container
[−]
[src]
pub struct Container { /* fields omitted */ }
Struct containing all the components registered during the build phase, used to resolve
Components.
A Container can't be used as a builder/factory of components from the same type, resolve() will consume registration data. Use resolve_ref() or resolve_mut() if you just want to borrow a (mutable) reference of a Component.
Examples
extern crate he_di; #[macro_use] extern crate he_di_derive; trait FooValue { fn get_value(&self) -> usize; fn set_value(&mut self, _: usize); } #[derive(Component)] #[interface(FooValue)] struct FooImpl { value: usize, } impl FooValue for FooImpl { fn get_value(&self) -> usize { self.value } fn set_value(&mut self, val: usize) { self.value = val; } } fn main() { let mut builder = he_di::ContainerBuilder::new(); builder .register::<FooImpl>() .as_type::<FooValue>() .with_named_parameter("value", 17 as usize); let mut container = builder.build().unwrap(); { let foo : &FooValue = container.resolve_ref::<FooValue>().unwrap(); assert_eq!(foo.get_value(), 17); } { let foo : &mut FooValue = container.resolve_mut::<FooValue>().unwrap(); assert_eq!(foo.get_value(), 17); foo.set_value(99); } { let foo : Box<FooValue> = container.resolve::<FooValue>().unwrap(); // consume registration data, `FooValue` won't be able to be resolved from this container any longer assert_eq!(foo.get_value(), 99); } { let foo = container.resolve_ref::<FooValue>(); assert!(foo.is_err()); } { let foo = container.resolve_mut::<FooValue>(); assert!(foo.is_err()); } }
See also module documentation for more details.
Methods
impl Container
[src]
fn resolve<T: ?Sized + 'static>(&mut self) -> DIResult<Box<T>>
Create a new Component registered with the trait T
and transfer the ownership
of the Component to the callee.
Further resolve calls on the same container for the same trait T
will fail.
Errors
Returns a Error::ResolveError if we can't resolve your Component from the Container (most likely your Component wasn't properly registered)
Examples
let foo = some_container.resolve::<Foo>();
fn resolve_ref<T: ?Sized + 'static>(&mut self) -> DIResult<&T>
Get a reference of a Component registered with the trait T
.
This component can later be resolved with any other resolve method.
Errors
Returns a Error::ResolveError if we can't resolve your Component from the Container (most likely your Component wasn't properly registered)
Examples
let foo = some_container.resolve_ref::<Foo>();
fn resolve_mut<T: ?Sized + 'static>(&mut self) -> DIResult<&mut T>
Get a mutable reference of a Component registered with the trait T
.
This component can later be resolved with any other resolve method.
Errors
Returns a Error::ResolveError if we can't resolve your Component from the Container (most likely your Component wasn't properly registered)
Examples
let foo = some_container.resolve_mut::<Foo>();
fn with_named_parameter<T: ?Sized + 'static, V: Any>(
&mut self,
name: &str,
value: V
) -> &mut Self
&mut self,
name: &str,
value: V
) -> &mut Self
Add a new named parameter for an already registered Component T
.
If T
wasn't previously registered, it does nothing.
Examples
Classic use is in a chained calls like that:
let foo = some_container .with_named_parameter::<Foo, String>("param_1", "value 1".to_string()) // ... .with_named_parameter::<Foo, String>("param_N", "value N".to_string()) .resolve::<Foo>();
fn with_typed_parameter<T: ?Sized + 'static, V: Any>(
&mut self,
value: V
) -> &mut Self
&mut self,
value: V
) -> &mut Self
Add a new typed parameter for an already registered Component T
.
If T
wasn't previously registered, it does nothing.
Examples
Classic use is in a chained calls like that:
let foo = some_container .with_typed_parameter::<Foo, String>("value 1".to_string()) // ... .with_typed_parameter::<Foo, String>("value N".to_string()) .resolve::<Foo>();