aboutsummaryrefslogtreecommitdiff
path: root/example/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-09 22:02:15 +0200
committerHampusM <hampus@hampusmat.com>2022-07-09 22:02:15 +0200
commit152a7a71101b94e4ddaa221396403d7dd1e76dbe (patch)
treecff8e46dc88396165b87e3cc83227eda1e51e186 /example/src
parentdda28f312ac87e84b6daed1ace7b2bcc3e47f71d (diff)
docs: add example
Diffstat (limited to 'example/src')
-rw-r--r--example/src/main.rs112
1 files changed, 112 insertions, 0 deletions
diff --git a/example/src/main.rs b/example/src/main.rs
new file mode 100644
index 0000000..920d9f0
--- /dev/null
+++ b/example/src/main.rs
@@ -0,0 +1,112 @@
+use syrette::{injectable, DIContainer, DIContainerError};
+
+trait IDog
+{
+ fn woof(&self);
+}
+
+struct Dog {}
+
+#[injectable(IDog)]
+impl Dog
+{
+ fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IDog for Dog
+{
+ fn woof(&self)
+ {
+ println!("Woof!");
+ }
+}
+
+trait ICat
+{
+ fn meow(&self);
+}
+
+struct Cat {}
+
+#[injectable(ICat)]
+impl Cat
+{
+ fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl ICat for Cat
+{
+ fn meow(&self)
+ {
+ println!("Meow!");
+ }
+}
+
+trait ICow
+{
+ fn moo(&self);
+}
+
+trait IHuman
+{
+ fn make_pets_make_sounds(&self);
+}
+
+struct Human
+{
+ _dog: Box<dyn IDog>,
+ _cat: Box<dyn ICat>,
+}
+
+#[injectable(IHuman)]
+impl Human
+{
+ fn new(dog: Box<dyn IDog>, cat: Box<dyn ICat>) -> Self
+ {
+ Self {
+ _dog: dog,
+ _cat: cat,
+ }
+ }
+}
+
+impl IHuman for Human
+{
+ fn make_pets_make_sounds(&self)
+ {
+ println!("Hi doggy!");
+
+ self._dog.woof();
+
+ println!("Hi kitty!");
+
+ self._cat.meow();
+ }
+}
+
+fn main() -> error_stack::Result<(), DIContainerError>
+{
+ println!("Hello, world!");
+
+ let mut di_container: DIContainer = DIContainer::new();
+
+ di_container.bind::<dyn IDog>().to::<Dog>();
+ di_container.bind::<dyn ICat>().to::<Cat>();
+ di_container.bind::<dyn IHuman>().to::<Human>();
+
+ let dog = di_container.get::<dyn IDog>()?;
+
+ dog.woof();
+
+ let human = di_container.get::<dyn IHuman>()?;
+
+ human.make_pets_make_sounds();
+
+ Ok(())
+}