Trait Inject

Source
pub trait Inject {
    // Required method
    fn inject(container: &Container<'_>) -> Self;
}
Expand description

A trait for constructing a type getting the dependencies from a Container.

§Example

use std::sync::Mutex;
use dilib::{Singleton, Inject, Container};

struct Greeter {
   message: String,
   total_greets: Singleton<Mutex<usize>>
}

impl Greeter {
    fn greet(&self) {
        println!("{}", self.message);
        *self.total_greets.lock().unwrap() += 1;
    }
}

impl Inject for Greeter {
    fn inject(container: &Container) -> Self{
        let message = container.get_scoped_with_name::<String>("es_msg").unwrap();
        let total_greets = container.get_singleton_with_name::<Mutex<usize>>("count").unwrap();
        Greeter { message, total_greets }
    }
}

Required Methods§

Source

fn inject(container: &Container<'_>) -> Self

Constructs this type using the Container.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Default> Inject for T