aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-20 18:06:42 +0200
committerHampusM <hampus@hampusmat.com>2022-08-21 18:17:51 +0200
commit55f5e8ca096467be2db895b459760ec34b63fce6 (patch)
treebb34d7f74d5ee7e423e57dbdfd9865d0115b0ff9 /examples
parent12acab96a7d9ba8032378f8be7b6932f4406a849 (diff)
docs: add example for displaying a unbound interface error
Diffstat (limited to 'examples')
-rw-r--r--examples/unbound/animals/cat.rs22
-rw-r--r--examples/unbound/animals/dog.rs22
-rw-r--r--examples/unbound/animals/human.rs35
-rw-r--r--examples/unbound/animals/mod.rs3
-rw-r--r--examples/unbound/bootstrap.rs23
-rw-r--r--examples/unbound/interfaces/cat.rs4
-rw-r--r--examples/unbound/interfaces/dog.rs4
-rw-r--r--examples/unbound/interfaces/human.rs4
-rw-r--r--examples/unbound/interfaces/mod.rs3
-rw-r--r--examples/unbound/main.rs26
10 files changed, 146 insertions, 0 deletions
diff --git a/examples/unbound/animals/cat.rs b/examples/unbound/animals/cat.rs
new file mode 100644
index 0000000..153ad4b
--- /dev/null
+++ b/examples/unbound/animals/cat.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::cat::ICat;
+
+pub struct Cat {}
+
+#[injectable(ICat)]
+impl Cat
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl ICat for Cat
+{
+ fn meow(&self)
+ {
+ println!("Meow!");
+ }
+}
diff --git a/examples/unbound/animals/dog.rs b/examples/unbound/animals/dog.rs
new file mode 100644
index 0000000..84959c0
--- /dev/null
+++ b/examples/unbound/animals/dog.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::dog::IDog;
+
+pub struct Dog {}
+
+#[injectable(IDog)]
+impl Dog
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IDog for Dog
+{
+ fn woof(&self)
+ {
+ println!("Woof!");
+ }
+}
diff --git a/examples/unbound/animals/human.rs b/examples/unbound/animals/human.rs
new file mode 100644
index 0000000..d9b848b
--- /dev/null
+++ b/examples/unbound/animals/human.rs
@@ -0,0 +1,35 @@
+use syrette::injectable;
+use syrette::ptr::{SingletonPtr, TransientPtr};
+
+use crate::interfaces::cat::ICat;
+use crate::interfaces::dog::IDog;
+use crate::interfaces::human::IHuman;
+
+pub struct Human
+{
+ dog: SingletonPtr<dyn IDog>,
+ cat: TransientPtr<dyn ICat>,
+}
+
+#[injectable(IHuman)]
+impl Human
+{
+ pub fn new(dog: SingletonPtr<dyn IDog>, cat: TransientPtr<dyn ICat>) -> Self
+ {
+ Self { dog, cat }
+ }
+}
+
+impl IHuman for Human
+{
+ fn make_pets_make_sounds(&self)
+ {
+ println!("Hi doggy!");
+
+ self.dog.woof();
+
+ println!("Hi kitty!");
+
+ self.cat.meow();
+ }
+}
diff --git a/examples/unbound/animals/mod.rs b/examples/unbound/animals/mod.rs
new file mode 100644
index 0000000..5444978
--- /dev/null
+++ b/examples/unbound/animals/mod.rs
@@ -0,0 +1,3 @@
+pub mod cat;
+pub mod dog;
+pub mod human;
diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs
new file mode 100644
index 0000000..7835619
--- /dev/null
+++ b/examples/unbound/bootstrap.rs
@@ -0,0 +1,23 @@
+use syrette::DIContainer;
+
+// Concrete implementations
+use crate::animals::dog::Dog;
+use crate::animals::human::Human;
+//
+// Interfaces
+use crate::interfaces::dog::IDog;
+use crate::interfaces::human::IHuman;
+
+pub fn bootstrap() -> DIContainer
+{
+ let mut di_container: DIContainer = DIContainer::new();
+
+ di_container
+ .bind::<dyn IDog>()
+ .to_singleton::<Dog>()
+ .unwrap();
+
+ di_container.bind::<dyn IHuman>().to::<Human>().unwrap();
+
+ di_container
+}
diff --git a/examples/unbound/interfaces/cat.rs b/examples/unbound/interfaces/cat.rs
new file mode 100644
index 0000000..8650802
--- /dev/null
+++ b/examples/unbound/interfaces/cat.rs
@@ -0,0 +1,4 @@
+pub trait ICat
+{
+ fn meow(&self);
+}
diff --git a/examples/unbound/interfaces/dog.rs b/examples/unbound/interfaces/dog.rs
new file mode 100644
index 0000000..4211c27
--- /dev/null
+++ b/examples/unbound/interfaces/dog.rs
@@ -0,0 +1,4 @@
+pub trait IDog
+{
+ fn woof(&self);
+}
diff --git a/examples/unbound/interfaces/human.rs b/examples/unbound/interfaces/human.rs
new file mode 100644
index 0000000..dc20194
--- /dev/null
+++ b/examples/unbound/interfaces/human.rs
@@ -0,0 +1,4 @@
+pub trait IHuman
+{
+ fn make_pets_make_sounds(&self);
+}
diff --git a/examples/unbound/interfaces/mod.rs b/examples/unbound/interfaces/mod.rs
new file mode 100644
index 0000000..5444978
--- /dev/null
+++ b/examples/unbound/interfaces/mod.rs
@@ -0,0 +1,3 @@
+pub mod cat;
+pub mod dog;
+pub mod human;
diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs
new file mode 100644
index 0000000..3a937c3
--- /dev/null
+++ b/examples/unbound/main.rs
@@ -0,0 +1,26 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![allow(clippy::module_name_repetitions)]
+
+mod animals;
+mod bootstrap;
+mod interfaces;
+
+use bootstrap::bootstrap;
+use interfaces::dog::IDog;
+use interfaces::human::IHuman;
+
+fn main()
+{
+ println!("Hello, world!");
+
+ let di_container = bootstrap();
+
+ let dog = di_container.get_singleton::<dyn IDog>().unwrap();
+
+ dog.woof();
+
+ let human = di_container.get::<dyn IHuman>().unwrap();
+
+ human.make_pets_make_sounds();
+}