aboutsummaryrefslogtreecommitdiff
path: root/examples/unbound
diff options
context:
space:
mode:
Diffstat (limited to 'examples/unbound')
-rw-r--r--examples/unbound/animal_store.rs34
-rw-r--r--examples/unbound/animals/human.rs20
-rw-r--r--examples/unbound/bootstrap.rs7
-rw-r--r--examples/unbound/interfaces/animal_store.rs11
-rw-r--r--examples/unbound/interfaces/mod.rs1
-rw-r--r--examples/unbound/main.rs9
6 files changed, 71 insertions, 11 deletions
diff --git a/examples/unbound/animal_store.rs b/examples/unbound/animal_store.rs
new file mode 100644
index 0000000..b2127e4
--- /dev/null
+++ b/examples/unbound/animal_store.rs
@@ -0,0 +1,34 @@
+use syrette::injectable;
+use syrette::ptr::{SingletonPtr, TransientPtr};
+
+use crate::interfaces::animal_store::IAnimalStore;
+use crate::interfaces::cat::ICat;
+use crate::interfaces::dog::IDog;
+
+pub struct AnimalStore
+{
+ dog: SingletonPtr<dyn IDog>,
+ cat: TransientPtr<dyn ICat>,
+}
+
+#[injectable]
+impl AnimalStore
+{
+ fn new(dog: SingletonPtr<dyn IDog>, cat: TransientPtr<dyn ICat>) -> Self
+ {
+ Self { dog, cat }
+ }
+}
+
+impl IAnimalStore for AnimalStore
+{
+ fn get_dog(&self) -> SingletonPtr<dyn IDog>
+ {
+ self.dog.clone()
+ }
+
+ fn get_cat(&self) -> &TransientPtr<dyn ICat>
+ {
+ &self.cat
+ }
+}
diff --git a/examples/unbound/animals/human.rs b/examples/unbound/animals/human.rs
index d9b848b..56764d3 100644
--- a/examples/unbound/animals/human.rs
+++ b/examples/unbound/animals/human.rs
@@ -1,22 +1,20 @@
use syrette::injectable;
-use syrette::ptr::{SingletonPtr, TransientPtr};
+use syrette::ptr::TransientPtr;
-use crate::interfaces::cat::ICat;
-use crate::interfaces::dog::IDog;
+use crate::interfaces::animal_store::IAnimalStore;
use crate::interfaces::human::IHuman;
pub struct Human
{
- dog: SingletonPtr<dyn IDog>,
- cat: TransientPtr<dyn ICat>,
+ animal_store: TransientPtr<dyn IAnimalStore>,
}
#[injectable(IHuman)]
impl Human
{
- pub fn new(dog: SingletonPtr<dyn IDog>, cat: TransientPtr<dyn ICat>) -> Self
+ pub fn new(animal_store: TransientPtr<dyn IAnimalStore>) -> Self
{
- Self { dog, cat }
+ Self { animal_store }
}
}
@@ -24,12 +22,16 @@ impl IHuman for Human
{
fn make_pets_make_sounds(&self)
{
+ let dog = self.animal_store.get_dog();
+
println!("Hi doggy!");
- self.dog.woof();
+ dog.woof();
+
+ let cat = self.animal_store.get_cat();
println!("Hi kitty!");
- self.cat.meow();
+ cat.meow();
}
}
diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs
index 7835619..dc8468c 100644
--- a/examples/unbound/bootstrap.rs
+++ b/examples/unbound/bootstrap.rs
@@ -1,10 +1,12 @@
use syrette::DIContainer;
// Concrete implementations
+use crate::animal_store::AnimalStore;
use crate::animals::dog::Dog;
use crate::animals::human::Human;
//
// Interfaces
+use crate::interfaces::animal_store::IAnimalStore;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
@@ -20,4 +22,9 @@ pub fn bootstrap() -> DIContainer
di_container.bind::<dyn IHuman>().to::<Human>().unwrap();
di_container
+ .bind::<dyn IAnimalStore>()
+ .to::<AnimalStore>()
+ .unwrap();
+
+ di_container
}
diff --git a/examples/unbound/interfaces/animal_store.rs b/examples/unbound/interfaces/animal_store.rs
new file mode 100644
index 0000000..dc8a4c3
--- /dev/null
+++ b/examples/unbound/interfaces/animal_store.rs
@@ -0,0 +1,11 @@
+use syrette::ptr::{SingletonPtr, TransientPtr};
+
+use crate::interfaces::cat::ICat;
+use crate::interfaces::dog::IDog;
+
+pub trait IAnimalStore
+{
+ fn get_dog(&self) -> SingletonPtr<dyn IDog>;
+
+ fn get_cat(&self) -> &TransientPtr<dyn ICat>;
+}
diff --git a/examples/unbound/interfaces/mod.rs b/examples/unbound/interfaces/mod.rs
index 5444978..bd9f848 100644
--- a/examples/unbound/interfaces/mod.rs
+++ b/examples/unbound/interfaces/mod.rs
@@ -1,3 +1,4 @@
+pub mod animal_store;
pub mod cat;
pub mod dog;
pub mod human;
diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs
index 3a937c3..47629e4 100644
--- a/examples/unbound/main.rs
+++ b/examples/unbound/main.rs
@@ -2,6 +2,9 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
+use std::error::Error;
+
+mod animal_store;
mod animals;
mod bootstrap;
mod interfaces;
@@ -10,7 +13,7 @@ use bootstrap::bootstrap;
use interfaces::dog::IDog;
use interfaces::human::IHuman;
-fn main()
+fn main() -> Result<(), Box<dyn Error>>
{
println!("Hello, world!");
@@ -20,7 +23,9 @@ fn main()
dog.woof();
- let human = di_container.get::<dyn IHuman>().unwrap();
+ let human = di_container.get::<dyn IHuman>()?;
human.make_pets_make_sounds();
+
+ Ok(())
}