[][src]Trait kamikaze_di::InjectAsRc

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

Resolves itself from a container as a Rc.

Allows the type to be resolved by the container without having to register it beforehand. Use this if you don't want your type to implement Clone.

Examples

use std::rc::Rc;
use kamikaze_di::{Result, Container, ContainerBuilder, InjectAsRc, Injector};

struct Point { x: i32, y: i32 }

impl InjectAsRc 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: Rc<Point> = container.inject()?;

assert_eq!(42, point.x);
assert_eq!( 5, point.y);
assert_eq!(2, Rc::strong_count(&point));

Required methods

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

Resolve Self from a Container.

The object will be Rc-ed inside the container.

Loading content...

Implementors

Loading content...