pub struct Container { /* private fields */ }
Expand description
The IoC container
Implementations§
Source§impl Container
impl Container
Sourcepub fn register<T>(
&mut self,
implementation_factory: impl Fn(&Container) -> T + 'static + Send + Sync + Clone,
) -> &mut Selfwhere
T: 'static,
pub fn register<T>(
&mut self,
implementation_factory: impl Fn(&Container) -> T + 'static + Send + Sync + Clone,
) -> &mut Selfwhere
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 {}
Sourcepub fn extend(&mut self, container: Container) -> &mut Self
pub fn extend(&mut self, container: Container) -> &mut Self
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 {}
Sourcepub fn try_resolve<T>(&self) -> Option<T>where
T: 'static,
pub fn try_resolve<T>(&self) -> Option<T>where
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 {}
Sourcepub fn resolve<T>(&self) -> Twhere
T: 'static,
pub fn resolve<T>(&self) -> Twhere
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§
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