[][src]Struct wonderbox::Container

pub struct Container { /* fields omitted */ }

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]

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]

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]

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 Clone for Container[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Default for Container[src]

impl Debug for Container[src]

Auto Trait Implementations

impl Send for Container

impl Sync for Container

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]