aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-09-12 20:22:13 +0200
committerHampusM <hampus@hampusmat.com>2022-09-17 14:33:15 +0200
commitc1e682c25c24be3174d44ceb95b0537c38299d0c (patch)
tree6e59f37e1b98e68fad2e3e2fe4a428ac97fcf8b4 /examples
parente8e48906a3899e71c9c9d86a3d4528cb7d17e5b9 (diff)
feat!: allow factories access to DI container
BREAKING CHANGE: Factory types should now be written with the Fn trait instead of the IFactory trait and the to_factory & to_default_factory methods of BindingBuilder now expect a function returning a factory function
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/bootstrap.rs3
-rw-r--r--examples/factory/bootstrap.rs11
-rw-r--r--examples/factory/interfaces/user.rs4
-rw-r--r--examples/generics/bootstrap.rs3
-rw-r--r--examples/unbound/bootstrap.rs3
-rw-r--r--examples/with-3rd-party/bootstrap.rs5
6 files changed, 7 insertions, 22 deletions
diff --git a/examples/basic/bootstrap.rs b/examples/basic/bootstrap.rs
index 30f6df3..2c45676 100644
--- a/examples/basic/bootstrap.rs
+++ b/examples/basic/bootstrap.rs
@@ -3,12 +3,9 @@ use std::rc::Rc;
use syrette::DIContainer;
-// 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;
diff --git a/examples/factory/bootstrap.rs b/examples/factory/bootstrap.rs
index 19fad81..f8bef6e 100644
--- a/examples/factory/bootstrap.rs
+++ b/examples/factory/bootstrap.rs
@@ -4,11 +4,8 @@ use std::rc::Rc;
use syrette::ptr::TransientPtr;
use syrette::DIContainer;
-// Interfaces
use crate::interfaces::user::{IUser, IUserFactory};
use crate::interfaces::user_manager::IUserManager;
-//
-// Concrete implementations
use crate::user::User;
use crate::user_manager::UserManager;
@@ -20,14 +17,14 @@ pub fn bootstrap() -> Result<Rc<DIContainer>, Box<dyn Error>>
.bind::<dyn IUserManager>()
.to::<UserManager>()?;
- di_container.bind::<IUserFactory>().to_factory(
- &|name, date_of_birth, password| {
+ di_container.bind::<IUserFactory>().to_factory(&|_| {
+ Box::new(move |name, date_of_birth, password| {
let user: TransientPtr<dyn IUser> =
TransientPtr::new(User::new(name, date_of_birth, password));
user
- },
- )?;
+ })
+ })?;
Ok(di_container)
}
diff --git a/examples/factory/interfaces/user.rs b/examples/factory/interfaces/user.rs
index 70cd632..aafd0cb 100644
--- a/examples/factory/interfaces/user.rs
+++ b/examples/factory/interfaces/user.rs
@@ -1,5 +1,5 @@
use syrette::factory;
-use syrette::interfaces::factory::IFactory;
+use syrette::ptr::TransientPtr;
pub trait IUser
{
@@ -10,4 +10,4 @@ pub trait IUser
#[factory]
pub type IUserFactory =
- dyn IFactory<(&'static str, &'static str, &'static str), dyn IUser>;
+ dyn Fn(&'static str, &'static str, &'static str) -> TransientPtr<dyn IUser>;
diff --git a/examples/generics/bootstrap.rs b/examples/generics/bootstrap.rs
index 752a39b..98d03db 100644
--- a/examples/generics/bootstrap.rs
+++ b/examples/generics/bootstrap.rs
@@ -2,10 +2,7 @@ use std::rc::Rc;
use syrette::{di_container_bind, DIContainer};
-// Interfaces
use crate::interfaces::printer::IPrinter;
-//
-// Implementations
use crate::printer::Printer;
pub fn bootstrap() -> Rc<DIContainer>
diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs
index 8df6678..04643dc 100644
--- a/examples/unbound/bootstrap.rs
+++ b/examples/unbound/bootstrap.rs
@@ -3,12 +3,9 @@ use std::rc::Rc;
use anyhow::Result;
use syrette::DIContainer;
-// Concrete implementations
use crate::animal_store::AnimalStore;
use crate::animals::dog::Dog;
use crate::animals::human::Human;
-//
-// Interfaces
use crate::interfaces::animal_store::IAnimalStore;
use crate::interfaces::dog::IDog;
use crate::interfaces::human::IHuman;
diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs
index a4bd84a..c5512c4 100644
--- a/examples/with-3rd-party/bootstrap.rs
+++ b/examples/with-3rd-party/bootstrap.rs
@@ -5,10 +5,7 @@ use syrette::ptr::TransientPtr;
use syrette::{declare_default_factory, DIContainer};
use third_party_lib::Shuriken;
-// Interfaces
use crate::interfaces::ninja::INinja;
-//
-// Concrete implementations
use crate::ninja::Ninja;
declare_default_factory!(Shuriken);
@@ -21,7 +18,7 @@ pub fn bootstrap() -> Result<Rc<DIContainer>, Box<dyn Error>>
di_container
.bind::<Shuriken>()
- .to_default_factory(&|| TransientPtr::new(Shuriken::new()))?;
+ .to_default_factory(&|_| TransientPtr::new(Shuriken::new()))?;
Ok(di_container)
}