[][src]Trait kamikaze_di::Inject

pub trait Inject where
    Self: Sized
{ fn resolve(container: &Container) -> Result<Self>; }

Resolves itself from a container.

Allows the type to be resolved by the container without having to register it beforehand. If you don't want to also implement Clone, which this trait requires, use InjectAsRc.

Examples

use kamikaze_di::{Result, Container, ContainerBuilder, Inject, Injector};

#[derive(Clone)]
struct Point { x: i32, y: i32 }

impl Inject for Point {
    fn resolve(container: &Container) -> Result<Self> {
        // You can use the container here.
        // As long as the compile can figure out the type you want,
        // it will do the right thing.
        Ok(Point { x: container.inject()?, y: 5 })
    }
}

let mut container_builder = ContainerBuilder::new();
container_builder.register::<i32>(42);

let container = container_builder.build();

let point: Point = container.inject()?;

assert_eq!(42, point.x);
assert_eq!( 5, point.y);

Required methods

fn resolve(container: &Container) -> Result<Self>

Resolve Self from a Container.

Loading content...

Implementors

Loading content...