[−][src]Struct wonderbox::Container
The IoC container
Methods
impl Container
[src]
pub fn new() -> Self
[src]
Create a new empty Container
.
pub fn register<T>(
&mut self,
implementation_factory: impl Fn(&Container) -> T + 'static + Send + Sync + Clone
) -> &mut Self where
T: 'static,
[src]
&mut self,
implementation_factory: impl Fn(&Container) -> T + 'static + Send + Sync + Clone
) -> &mut Self where
T: 'static,
Register a function that returns the implementation of a type. Can be used to try_resolve dependencies.
Examples
Registering a simple factory:
use wonderbox::Container; let mut container = Container::new(); container.register(|_| String::new());
Registering a factory for a trait object with dependencies:
use std::rc::Rc; use wonderbox::Container; let mut container = Container::new(); container.register(|_| "I'm a dependency".to_string()); container.register(|container| { let dependency = container.try_resolve::<String>().unwrap(); let registered_type = FooImpl { stored_string: dependency, }; Box::new(registered_type) as Box<dyn Foo> }); trait Foo {} struct FooImpl { stored_string: String, } impl Foo for FooImpl {}
pub fn extend(&mut self, container: Container) -> &mut Self
[src]
Register all the element from another container into this container.
Examples
use wonderbox::Container; let mut first_container = Container::new(); first_container.register(|_| "foo".to_string()); let mut second_container = Container::new(); second_container.register(|container| { let dependency = container.try_resolve::<String>().unwrap(); let foo = FooImpl { stored_string: dependency, }; Box::new(foo) as Box<dyn Foo> }); first_container.extend(second_container); trait Foo {} struct FooImpl { stored_string: String, } impl Foo for FooImpl {}
pub fn try_resolve<T>(&self) -> Option<T> where
T: 'static,
[src]
T: 'static,
Retrieves the registered implementation of the specified type.
Errors
Returns None
if the type was not registered
Examples
Resolve a simple registered type
use wonderbox::Container; let mut container = Container::new(); container.register(|_| "some dependency".to_string()); let resolved = container.try_resolve::<String>(); assert!(resolved.is_some())
Resolve a trait object
use wonderbox::Container; let mut container = Container::new(); container.register(|_| "foo".to_string()); container.register(|container| { let dependency = container.try_resolve::<String>().unwrap(); let foo = FooImpl { stored_string: dependency, }; Box::new(foo) as Box<dyn Foo> }); let resolved = container.try_resolve::<Box<dyn Foo>>(); assert!(resolved.is_some()); trait Foo {} struct FooImpl { stored_string: String, } impl Foo for FooImpl {}
pub fn resolve<T>(&self) -> T where
T: 'static,
[src]
T: 'static,
Retrieves the registered implementation of the specified type.
Errors
Panics with a nice error message if the type was not registered
Examples
Resolve a simple registered type
use wonderbox::Container; let mut container = Container::new(); container.register(|_| "some dependency".to_string()); let resolved = container.try_resolve::<String>(); assert!(resolved.is_some())
Resolve a trait object
use wonderbox::Container; let mut container = Container::new(); container.register(|_| "foo".to_string()); container.register(|container| { let dependency = container.try_resolve::<String>().unwrap(); let foo = FooImpl { stored_string: dependency, }; Box::new(foo) as Box<dyn Foo> }); let resolved = container.resolve::<Box<dyn Foo>>(); trait Foo {} struct FooImpl { stored_string: String, } impl Foo for FooImpl {}
Trait Implementations
impl Default for Container
[src]
impl Clone for Container
[src]
fn clone(&self) -> Container
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for Container
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,