Autowired
Rust dependency injection project
Usage
Just derive your struct with the marco Component, you can use the singleton component everywhere.
Define custom component initialization logic
Rust dependency injection project
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() {
// 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
#[derive(Default)]
struct Foo {
value: String,
}
impl Component for Foo {
type Error = ();
fn new_instance() -> Result<Arc<Self>, Self::Error> {
Ok(Arc::new(Foo {
value: TEST_STRING.to_string(),
}))
}
}
fn main() {
// create `foo` via new_instance
let foo = Autowired::<Foo>::new();
assert_eq!("TEST_STRING", foo.value);
}