From e0f90a8e384615c79d7d51c66d19294d75e79391 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 27 Aug 2022 23:41:41 +0200 Subject: feat: implement named bindings --- examples/named/bootstrap.rs | 29 ++++++++++++++++++++++++ examples/named/interfaces/mod.rs | 2 ++ examples/named/interfaces/ninja.rs | 4 ++++ examples/named/interfaces/weapon.rs | 4 ++++ examples/named/katana.rs | 22 ++++++++++++++++++ examples/named/main.rs | 25 +++++++++++++++++++++ examples/named/ninja.rs | 45 +++++++++++++++++++++++++++++++++++++ examples/named/shuriken.rs | 22 ++++++++++++++++++ 8 files changed, 153 insertions(+) create mode 100644 examples/named/bootstrap.rs create mode 100644 examples/named/interfaces/mod.rs create mode 100644 examples/named/interfaces/ninja.rs create mode 100644 examples/named/interfaces/weapon.rs create mode 100644 examples/named/katana.rs create mode 100644 examples/named/main.rs create mode 100644 examples/named/ninja.rs create mode 100644 examples/named/shuriken.rs (limited to 'examples') 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 +{ + let mut di_container: DIContainer = DIContainer::new(); + + di_container + .bind::() + .to::()? + .in_transient_scope() + .when_named("strong")?; + + di_container + .bind::() + .to::()? + .in_transient_scope() + .when_named("weak")?; + + di_container.bind::().to::()?; + + 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::()?.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, + weak_weapon: TransientPtr, +} + +#[injectable(INinja)] +impl Ninja +{ + pub fn new( + #[rustfmt::skip] // Prevent rustfmt from turning this into a single line + #[syrette::named("strong")] + strong_weapon: TransientPtr, + + #[rustfmt::skip] // Prevent rustfmt from turning this into a single line + #[named("weak")] + weak_weapon: TransientPtr, + ) -> 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!"); + } +} -- cgit v1.2.3-18-g5258