aboutsummaryrefslogtreecommitdiff
path: root/examples/named
diff options
context:
space:
mode:
Diffstat (limited to 'examples/named')
-rw-r--r--examples/named/bootstrap.rs29
-rw-r--r--examples/named/interfaces/mod.rs2
-rw-r--r--examples/named/interfaces/ninja.rs4
-rw-r--r--examples/named/interfaces/weapon.rs4
-rw-r--r--examples/named/katana.rs22
-rw-r--r--examples/named/main.rs25
-rw-r--r--examples/named/ninja.rs45
-rw-r--r--examples/named/shuriken.rs22
8 files changed, 153 insertions, 0 deletions
diff --git a/examples/named/bootstrap.rs b/examples/named/bootstrap.rs
new file mode 100644
index 0000000..b5fa39d
--- /dev/null
+++ b/examples/named/bootstrap.rs
@@ -0,0 +1,29 @@
+use anyhow::Result;
+use syrette::DIContainer;
+
+use crate::interfaces::ninja::INinja;
+use crate::interfaces::weapon::IWeapon;
+use crate::katana::Katana;
+use crate::ninja::Ninja;
+use crate::shuriken::Shuriken;
+
+pub fn bootstrap() -> Result<DIContainer>
+{
+ let mut di_container: DIContainer = DIContainer::new();
+
+ di_container
+ .bind::<dyn IWeapon>()
+ .to::<Katana>()?
+ .in_transient_scope()
+ .when_named("strong")?;
+
+ di_container
+ .bind::<dyn IWeapon>()
+ .to::<Shuriken>()?
+ .in_transient_scope()
+ .when_named("weak")?;
+
+ di_container.bind::<dyn INinja>().to::<Ninja>()?;
+
+ Ok(di_container)
+}
diff --git a/examples/named/interfaces/mod.rs b/examples/named/interfaces/mod.rs
new file mode 100644
index 0000000..6a0108d
--- /dev/null
+++ b/examples/named/interfaces/mod.rs
@@ -0,0 +1,2 @@
+pub mod ninja;
+pub mod weapon;
diff --git a/examples/named/interfaces/ninja.rs b/examples/named/interfaces/ninja.rs
new file mode 100644
index 0000000..2d378c8
--- /dev/null
+++ b/examples/named/interfaces/ninja.rs
@@ -0,0 +1,4 @@
+pub trait INinja
+{
+ fn use_weapons(&self);
+}
diff --git a/examples/named/interfaces/weapon.rs b/examples/named/interfaces/weapon.rs
new file mode 100644
index 0000000..7848a0f
--- /dev/null
+++ b/examples/named/interfaces/weapon.rs
@@ -0,0 +1,4 @@
+pub trait IWeapon
+{
+ fn use_it(&self);
+}
diff --git a/examples/named/katana.rs b/examples/named/katana.rs
new file mode 100644
index 0000000..a03af6d
--- /dev/null
+++ b/examples/named/katana.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::weapon::IWeapon;
+
+pub struct Katana {}
+
+#[injectable(IWeapon)]
+impl Katana
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IWeapon for Katana
+{
+ fn use_it(&self)
+ {
+ println!("Used katana!");
+ }
+}
diff --git a/examples/named/main.rs b/examples/named/main.rs
new file mode 100644
index 0000000..5411a12
--- /dev/null
+++ b/examples/named/main.rs
@@ -0,0 +1,25 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![allow(clippy::module_name_repetitions)]
+
+mod bootstrap;
+mod interfaces;
+mod katana;
+mod ninja;
+mod shuriken;
+
+use anyhow::Result;
+
+use crate::bootstrap::bootstrap;
+use crate::interfaces::ninja::INinja;
+
+fn main() -> Result<()>
+{
+ let di_container = bootstrap()?;
+
+ let ninja = di_container.get::<dyn INinja>()?.transient()?;
+
+ ninja.use_weapons();
+
+ Ok(())
+}
diff --git a/examples/named/ninja.rs b/examples/named/ninja.rs
new file mode 100644
index 0000000..2069f14
--- /dev/null
+++ b/examples/named/ninja.rs
@@ -0,0 +1,45 @@
+use syrette::injectable;
+use syrette::ptr::TransientPtr;
+
+use crate::interfaces::ninja::INinja;
+use crate::interfaces::weapon::IWeapon;
+
+pub struct Ninja
+{
+ strong_weapon: TransientPtr<dyn IWeapon>,
+ weak_weapon: TransientPtr<dyn IWeapon>,
+}
+
+#[injectable(INinja)]
+impl Ninja
+{
+ pub fn new(
+ #[rustfmt::skip] // Prevent rustfmt from turning this into a single line
+ #[syrette::named("strong")]
+ strong_weapon: TransientPtr<dyn IWeapon>,
+
+ #[rustfmt::skip] // Prevent rustfmt from turning this into a single line
+ #[named("weak")]
+ weak_weapon: TransientPtr<dyn IWeapon>,
+ ) -> Self
+ {
+ Self {
+ strong_weapon,
+ weak_weapon,
+ }
+ }
+}
+
+impl INinja for Ninja
+{
+ fn use_weapons(&self)
+ {
+ println!("Ninja is using his strong weapon!");
+
+ self.strong_weapon.use_it();
+
+ println!("Ninja is using his weak weapon!");
+
+ self.weak_weapon.use_it();
+ }
+}
diff --git a/examples/named/shuriken.rs b/examples/named/shuriken.rs
new file mode 100644
index 0000000..c50aeac
--- /dev/null
+++ b/examples/named/shuriken.rs
@@ -0,0 +1,22 @@
+use syrette::injectable;
+
+use crate::interfaces::weapon::IWeapon;
+
+pub struct Shuriken {}
+
+#[injectable(IWeapon)]
+impl Shuriken
+{
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+}
+
+impl IWeapon for Shuriken
+{
+ fn use_it(&self)
+ {
+ println!("Used shuriken!");
+ }
+}