#injection

autowired

Rust dependency injection

10 releases

0.1.8 Jun 21, 2021
0.1.7 Jun 20, 2021
0.1.6 Apr 11, 2021
0.1.3 Mar 21, 2021
0.0.0 Mar 13, 2021

#508 in Rust patterns

Download history 88/week @ 2022-06-04 6/week @ 2022-06-11 11/week @ 2022-06-18 21/week @ 2022-06-25 29/week @ 2022-07-02 23/week @ 2022-07-09 19/week @ 2022-07-16 27/week @ 2022-07-23 7/week @ 2022-07-30 29/week @ 2022-08-06 15/week @ 2022-08-13 11/week @ 2022-08-20 14/week @ 2022-08-27 13/week @ 2022-09-03 51/week @ 2022-09-10 12/week @ 2022-09-17

93 downloads per month

Apache-2.0

11KB
159 lines

Autowired

crates.io docs.rs

Rust dependency injection project, inspired by Spring IOC.

Add Dependency

[dependencies]
autowired="0.1"

Usage

Just derive your struct with the marco Component, you can use the singleton component everywhere.

#[derive(Default, Component)]
struct Bar {
    name: String,
    age: u32,
}

fn main() {
    // central registration in the beginning of the program
    setup_submitted_beans();

    // create `bar` via Default::default
    let bar: Autowired<Bar> = Autowired::new();

    assert_eq!(String::default(), bar.name);
    assert_eq!(u32::default(), bar.age);
}

Define custom component initialization logic

struct Goo { pub list: Vec<String> }

#[autowired::bean]
fn build_goo() -> Goo {
    Goo { list: vec!["hello".to_string()] }
}

fn main() {
    // central registration in the beginning of the program
    setup_submitted_beans();

    let goo = Autowired::<Goo>::new();
    assert_eq!("hello", goo.list[0])
}

Lazy components

By default, components are registered with setup_submitted_beans. If you need to register components lazily, you can refer to this example:

use std::sync::Arc;
use autowired::{ LazyComponent, setup_submitted_beans, bean, Autowired};

#[allow(dead_code)]
#[derive(Default, LazyComponent)]
struct Bar {
    name: Arc<String>,
    age: u32,
}

#[allow(dead_code)]
struct Goo { pub list: Vec<String> }

#[bean(lazy)]
fn build_goo() -> Goo {
    Goo { list: vec!["hello".to_string()] }
}

#[test]
fn lazy() {
    setup_submitted_beans();

    assert!(!autowired::exist_component::<Bar>());
    assert!(!autowired::exist_component::<Goo>());

    let bar = Autowired::<Bar>::new();
    assert!( bar.name.is_empty());

    let goo = Autowired::<Goo>::new();
    assert_eq!("hello", goo.list[0]);

    assert!(autowired::exist_component::<Bar>());
    assert!(autowired::exist_component::<Goo>());
}

Option components

Functional bean constructor can return Option with attribute #[bean(option)].

if return value is None, this bean will not be submitted.

if you like, this feature can work with lazy components, #[bean(option, lazy)].

#[allow(dead_code)]
struct Bar {
    name: String,
}

/// return `None`, this bean will not be submitted
#[bean(option)]
fn build_bar_none() -> Option<Bar> {
    None
}

#[allow(dead_code)]
struct Goo {
    pub list: Vec<String>,
}

#[bean(option)]
fn build_goo_some() -> Option<Goo> {
    Some(Goo { list: vec!["hello".to_string()] })
}

#[test]
fn option() {
    setup_submitted_beans();

    assert!(!autowired::exist_component::<Bar>());
    assert!(autowired::exist_component::<Goo>());

    let goo = Autowired::<Goo>::new();
    assert_eq!("hello", goo.list[0]);
}

Dependencies

~0.5–1MB
~21K SLoC