aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/async/animals/cat.rs22
-rw-r--r--examples/async/animals/dog.rs22
-rw-r--r--examples/async/animals/human.rs36
-rw-r--r--examples/async/animals/mod.rs3
-rw-r--r--examples/async/bootstrap.rs28
-rw-r--r--examples/async/interfaces/cat.rs4
-rw-r--r--examples/async/interfaces/dog.rs4
-rw-r--r--examples/async/interfaces/human.rs4
-rw-r--r--examples/async/interfaces/mod.rs3
-rw-r--r--examples/async/main.rs52
10 files changed, 178 insertions, 0 deletions
diff --git a/examples/async/animals/cat.rs b/examples/async/animals/cat.rs
new file mode 100644
index 0000000..b1e6f27
--- /dev/null
+++ b/examples/async/animals/cat.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::cat::ICat;
+
+pub struct Cat {}
+
+#[injectable(ICat, { async = true })]
+impl Cat
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl ICat for Cat
+{
+ fn meow(&self)
+ {
+ println!("Meow!");
+ }
+}
diff --git a/examples/async/animals/dog.rs b/examples/async/animals/dog.rs
new file mode 100644
index 0000000..d1b33f9
--- /dev/null
+++ b/examples/async/animals/dog.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::dog::IDog;
+
+pub struct Dog {}
+
+#[injectable(IDog, { async = true })]
+impl Dog
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IDog for Dog
+{
+ fn woof(&self)
+ {
+ println!("Woof!");
+ }
+}
diff --git a/examples/async/animals/human.rs b/examples/async/animals/human.rs
new file mode 100644
index 0000000..140f27c
--- /dev/null
+++ b/examples/async/animals/human.rs
@@ -0,0 +1,36 @@
+use syrette::injectable;
+use syrette::ptr::{ThreadsafeSingletonPtr, TransientPtr};
+
+use crate::interfaces::cat::ICat;
+use crate::interfaces::dog::IDog;
+use crate::interfaces::human::IHuman;
+
+pub struct Human
+{
+ dog: ThreadsafeSingletonPtr<dyn IDog>,
+ cat: TransientPtr<dyn ICat>,
+}
+
+#[injectable(IHuman, { async = true })]
+impl Human
+{
+ pub fn new(dog: ThreadsafeSingletonPtr<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/async/animals/mod.rs b/examples/async/animals/mod.rs
new file mode 100644
index 0000000..5444978
--- /dev/null
+++ b/examples/async/animals/mod.rs
@@ -0,0 +1,3 @@
+pub mod cat;
+pub mod dog;
+pub mod human;
diff --git a/examples/async/bootstrap.rs b/examples/async/bootstrap.rs
new file mode 100644
index 0000000..b640712
--- /dev/null
+++ b/examples/async/bootstrap.rs
@@ -0,0 +1,28 @@
+use anyhow::Result;
+use syrette::async_di_container::AsyncDIContainer;
+
+// Concrete implementations
+use crate::animals::cat::Cat;
+use crate::animals::dog::Dog;
+use crate::animals::human::Human;
+//
+// Interfaces
+use crate::interfaces::cat::ICat;
+use crate::interfaces::dog::IDog;
+use crate::interfaces::human::IHuman;
+
+pub async fn bootstrap() -> Result<AsyncDIContainer>
+{
+ let mut di_container = AsyncDIContainer::new();
+
+ di_container
+ .bind::<dyn IDog>()
+ .to::<Dog>()?
+ .in_singleton_scope()
+ .await?;
+
+ di_container.bind::<dyn ICat>().to::<Cat>()?;
+ di_container.bind::<dyn IHuman>().to::<Human>()?;
+
+ Ok(di_container)
+}
diff --git a/examples/async/interfaces/cat.rs b/examples/async/interfaces/cat.rs
new file mode 100644
index 0000000..478f7e0
--- /dev/null
+++ b/examples/async/interfaces/cat.rs
@@ -0,0 +1,4 @@
+pub trait ICat: Send + Sync
+{
+ fn meow(&self);
+}
diff --git a/examples/async/interfaces/dog.rs b/examples/async/interfaces/dog.rs
new file mode 100644
index 0000000..a6ed111
--- /dev/null
+++ b/examples/async/interfaces/dog.rs
@@ -0,0 +1,4 @@
+pub trait IDog: Send + Sync
+{
+ fn woof(&self);
+}
diff --git a/examples/async/interfaces/human.rs b/examples/async/interfaces/human.rs
new file mode 100644
index 0000000..18f9d63
--- /dev/null
+++ b/examples/async/interfaces/human.rs
@@ -0,0 +1,4 @@
+pub trait IHuman: Send + Sync
+{
+ fn make_pets_make_sounds(&self);
+}
diff --git a/examples/async/interfaces/mod.rs b/examples/async/interfaces/mod.rs
new file mode 100644
index 0000000..5444978
--- /dev/null
+++ b/examples/async/interfaces/mod.rs
@@ -0,0 +1,3 @@
+pub mod cat;
+pub mod dog;
+pub mod human;
diff --git a/examples/async/main.rs b/examples/async/main.rs
new file mode 100644
index 0000000..f72ff39
--- /dev/null
+++ b/examples/async/main.rs
@@ -0,0 +1,52 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![allow(clippy::module_name_repetitions)]
+
+use std::sync::Arc;
+
+use anyhow::Result;
+use tokio::spawn;
+use tokio::sync::Mutex;
+
+mod animals;
+mod bootstrap;
+mod interfaces;
+
+use bootstrap::bootstrap;
+use interfaces::dog::IDog;
+use interfaces::human::IHuman;
+
+#[tokio::main]
+async fn main() -> Result<()>
+{
+ println!("Hello, world!");
+
+ let di_container = Arc::new(Mutex::new(bootstrap().await?));
+
+ {
+ let dog = di_container
+ .lock()
+ .await
+ .get::<dyn IDog>()
+ .await?
+ .threadsafe_singleton()?;
+
+ dog.woof();
+ }
+
+ spawn(async move {
+ let human = di_container
+ .lock()
+ .await
+ .get::<dyn IHuman>()
+ .await?
+ .transient()?;
+
+ human.make_pets_make_sounds();
+
+ Ok::<_, anyhow::Error>(())
+ })
+ .await??;
+
+ Ok(())
+}