Skip to main content

Module di

Module di 

Source
Expand description

Lightweight type-keyed dependency injection container.

Container stores services keyed by their Rust type (TypeId). Register services at startup; resolve them anywhere that has access to the container. Share across request handlers by wrapping in Arc<Container> and using App::with_state(container.into_arc()).

§Concrete services

use rust_web_server::di::Container;

struct EmailService { host: String }

let mut c = Container::new();
c.register(EmailService { host: "smtp.example.com".into() });

let svc = c.get::<EmailService>().unwrap();
assert_eq!(svc.host, "smtp.example.com");

§Trait-object services

use std::sync::Arc;
use rust_web_server::di::Container;

pub trait Mailer: Send + Sync {
    fn send(&self, to: &str);
}
struct SmtpMailer;
impl Mailer for SmtpMailer {
    fn send(&self, _to: &str) {}
}

let mut c = Container::new();
c.provide::<dyn Mailer>(Arc::new(SmtpMailer));
let mailer = c.get::<dyn Mailer>().unwrap();
mailer.send("user@example.com");

§Named services

Register multiple instances of the same type under distinct string names:

use rust_web_server::di::Container;

let mut c = Container::new();
c.register_named("primary",  5432u16)
 .register_named("replica",  5433u16);

assert_eq!(*c.get_named::<u16>("primary").unwrap(), 5432);
assert_eq!(*c.get_named::<u16>("replica").unwrap(), 5433);

§Usage with App::with_state

use std::sync::Arc;
use rust_web_server::app::App;
use rust_web_server::di::Container;
use rust_web_server::request::Request;
use rust_web_server::router::PathParams;
use rust_web_server::server::ConnectionInfo;
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
use rust_web_server::routes;

struct Config { version: &'static str }

fn get_version(
    _req: &Request,
    _p: &PathParams,
    _c: &ConnectionInfo,
    state: &Arc<Container>,
) -> Response {
    let _cfg = state.get::<Config>().unwrap();
    Response::get_response(&STATUS_CODE_REASON_PHRASE.n200_ok, None, None)
}

let mut container = Container::new();
container.register(Config { version: "1.0" });

let app = routes! {
    App::with_state(container.into_arc()),
    GET "/version" => get_version,
};

Structs§

Container
A type-keyed service container for dependency injection.