From f75a7d58135825c4f9094c1e10f36de4a952f455 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 27 Jul 2022 19:10:30 +0200 Subject: feat: add injecting singletons into constructors --- examples/basic/animals/human.rs | 6 +++--- examples/basic/bootstrap.rs | 5 ++++- examples/basic/main.rs | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/basic/animals/human.rs b/examples/basic/animals/human.rs index 8e52b5b..d9b848b 100644 --- a/examples/basic/animals/human.rs +++ b/examples/basic/animals/human.rs @@ -1,5 +1,5 @@ use syrette::injectable; -use syrette::ptr::TransientPtr; +use syrette::ptr::{SingletonPtr, TransientPtr}; use crate::interfaces::cat::ICat; use crate::interfaces::dog::IDog; @@ -7,14 +7,14 @@ use crate::interfaces::human::IHuman; pub struct Human { - dog: TransientPtr, + dog: SingletonPtr, cat: TransientPtr, } #[injectable(IHuman)] impl Human { - pub fn new(dog: TransientPtr, cat: TransientPtr) -> Self + pub fn new(dog: SingletonPtr, cat: TransientPtr) -> Self { Self { dog, cat } } diff --git a/examples/basic/bootstrap.rs b/examples/basic/bootstrap.rs index 71ef713..10a7041 100644 --- a/examples/basic/bootstrap.rs +++ b/examples/basic/bootstrap.rs @@ -14,7 +14,10 @@ pub fn bootstrap() -> DIContainer { let mut di_container: DIContainer = DIContainer::new(); - di_container.bind::().to::(); + di_container + .bind::() + .to_singleton::() + .unwrap(); di_container.bind::().to::(); di_container.bind::().to::(); diff --git a/examples/basic/main.rs b/examples/basic/main.rs index f96d963..3a937c3 100644 --- a/examples/basic/main.rs +++ b/examples/basic/main.rs @@ -16,7 +16,7 @@ fn main() let di_container = bootstrap(); - let dog = di_container.get::().unwrap(); + let dog = di_container.get_singleton::().unwrap(); dog.woof(); -- cgit v1.2.3-18-g5258