8 releases (5 breaking)

0.6.0 Jan 10, 2021
0.5.0 Jun 19, 2020
0.4.1 Jun 1, 2020
0.4.0 May 26, 2020
0.1.0 Feb 6, 2020

#35 in Build Utils

Download history 28/week @ 2020-10-02 18/week @ 2020-10-09 33/week @ 2020-10-16 30/week @ 2020-10-23 17/week @ 2020-10-30 29/week @ 2020-11-06 25/week @ 2020-11-13 21/week @ 2020-11-20 20/week @ 2020-11-27 31/week @ 2020-12-04 40/week @ 2020-12-11 12/week @ 2020-12-18 21/week @ 2020-12-25 57/week @ 2021-01-01 50/week @ 2021-01-08 33/week @ 2021-01-15

119 downloads per month
Used in 6 crates (5 directly)

MIT/Apache

58KB
294 lines

Current version Current documentation Build status

Shaku

Shaku is a compile time dependency injection Rust library. See the docs for more details, including a getting started guide.

Example

use shaku::{module, Component, Interface, HasComponent};
use std::sync::Arc;

trait Logger: Interface {
    fn log(&self, content: &str);
}

trait DateLogger: Interface {
    fn log_date(&self);
}

#[derive(Component)]
#[shaku(interface = Logger)]
struct LoggerImpl;

impl Logger for LoggerImpl {
    fn log(&self, content: &str) {
        println!("{}", content);
    }
}

#[derive(Component)]
#[shaku(interface = DateLogger)]
struct DateLoggerImpl {
    #[shaku(inject)]
    logger: Arc<dyn Logger>,
    today: String,
    year: usize,
}

impl DateLogger for DateLoggerImpl {
    fn log_date(&self) {
        self.logger.log(&format!("Today is {}, {}", self.today, self.year));
    }
}

module! {
    MyModule {
        components = [LoggerImpl, DateLoggerImpl],
        providers = []
    }
}

fn main() {
    let module = MyModule::builder()
        .with_component_parameters::<DateLoggerImpl>(DateLoggerImplParameters {
            today: "Jan 26".to_string(),
            year: 2020
        })
        .build();

    let date_logger: &dyn DateLogger = module.resolve_ref();
    date_logger.log_date();
}

Minimum Supported Rust Version

Shaku supports the latest stable release of Rust, plus the previous two versions at minimum (but possibly more). Changes to the minimum supported version will be noted in the changelog.

Minimum supported version: 1.38.0

Project Status

The foundation of shaku's API is in place, and now the focus is to mature the project based on user feedback. I (@Mcat12) am active in the project, but I do not have many major changes of my own planned for the future. Most of the future changes will be based on user feedback.

Acknowledgements

This library started off as "he_di" (later renamed to "shaku") under the guidance of @bgbahoue and @U007D. Their work inspired the current maintainer (@Mcat12) to continue the library from where they left off.

Dependencies

~215KB