diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/unbound/animal_store.rs | 34 | ||||
| -rw-r--r-- | examples/unbound/animals/human.rs | 20 | ||||
| -rw-r--r-- | examples/unbound/bootstrap.rs | 7 | ||||
| -rw-r--r-- | examples/unbound/interfaces/animal_store.rs | 11 | ||||
| -rw-r--r-- | examples/unbound/interfaces/mod.rs | 1 | ||||
| -rw-r--r-- | examples/unbound/main.rs | 9 | ||||
| -rw-r--r-- | examples/with-3rd-party/bootstrap.rs | 5 | ||||
| -rw-r--r-- | examples/with-3rd-party/main.rs | 25 | 
8 files changed, 78 insertions, 34 deletions
diff --git a/examples/unbound/animal_store.rs b/examples/unbound/animal_store.rs new file mode 100644 index 0000000..b2127e4 --- /dev/null +++ b/examples/unbound/animal_store.rs @@ -0,0 +1,34 @@ +use syrette::injectable; +use syrette::ptr::{SingletonPtr, TransientPtr}; + +use crate::interfaces::animal_store::IAnimalStore; +use crate::interfaces::cat::ICat; +use crate::interfaces::dog::IDog; + +pub struct AnimalStore +{ +    dog: SingletonPtr<dyn IDog>, +    cat: TransientPtr<dyn ICat>, +} + +#[injectable] +impl AnimalStore +{ +    fn new(dog: SingletonPtr<dyn IDog>, cat: TransientPtr<dyn ICat>) -> Self +    { +        Self { dog, cat } +    } +} + +impl IAnimalStore for AnimalStore +{ +    fn get_dog(&self) -> SingletonPtr<dyn IDog> +    { +        self.dog.clone() +    } + +    fn get_cat(&self) -> &TransientPtr<dyn ICat> +    { +        &self.cat +    } +} diff --git a/examples/unbound/animals/human.rs b/examples/unbound/animals/human.rs index d9b848b..56764d3 100644 --- a/examples/unbound/animals/human.rs +++ b/examples/unbound/animals/human.rs @@ -1,22 +1,20 @@  use syrette::injectable; -use syrette::ptr::{SingletonPtr, TransientPtr}; +use syrette::ptr::TransientPtr; -use crate::interfaces::cat::ICat; -use crate::interfaces::dog::IDog; +use crate::interfaces::animal_store::IAnimalStore;  use crate::interfaces::human::IHuman;  pub struct Human  { -    dog: SingletonPtr<dyn IDog>, -    cat: TransientPtr<dyn ICat>, +    animal_store: TransientPtr<dyn IAnimalStore>,  }  #[injectable(IHuman)]  impl Human  { -    pub fn new(dog: SingletonPtr<dyn IDog>, cat: TransientPtr<dyn ICat>) -> Self +    pub fn new(animal_store: TransientPtr<dyn IAnimalStore>) -> Self      { -        Self { dog, cat } +        Self { animal_store }      }  } @@ -24,12 +22,16 @@ impl IHuman for Human  {      fn make_pets_make_sounds(&self)      { +        let dog = self.animal_store.get_dog(); +          println!("Hi doggy!"); -        self.dog.woof(); +        dog.woof(); + +        let cat = self.animal_store.get_cat();          println!("Hi kitty!"); -        self.cat.meow(); +        cat.meow();      }  } diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs index 7835619..dc8468c 100644 --- a/examples/unbound/bootstrap.rs +++ b/examples/unbound/bootstrap.rs @@ -1,10 +1,12 @@  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; @@ -20,4 +22,9 @@ pub fn bootstrap() -> DIContainer      di_container.bind::<dyn IHuman>().to::<Human>().unwrap();      di_container +        .bind::<dyn IAnimalStore>() +        .to::<AnimalStore>() +        .unwrap(); + +    di_container  } diff --git a/examples/unbound/interfaces/animal_store.rs b/examples/unbound/interfaces/animal_store.rs new file mode 100644 index 0000000..dc8a4c3 --- /dev/null +++ b/examples/unbound/interfaces/animal_store.rs @@ -0,0 +1,11 @@ +use syrette::ptr::{SingletonPtr, TransientPtr}; + +use crate::interfaces::cat::ICat; +use crate::interfaces::dog::IDog; + +pub trait IAnimalStore +{ +    fn get_dog(&self) -> SingletonPtr<dyn IDog>; + +    fn get_cat(&self) -> &TransientPtr<dyn ICat>; +} diff --git a/examples/unbound/interfaces/mod.rs b/examples/unbound/interfaces/mod.rs index 5444978..bd9f848 100644 --- a/examples/unbound/interfaces/mod.rs +++ b/examples/unbound/interfaces/mod.rs @@ -1,3 +1,4 @@ +pub mod animal_store;  pub mod cat;  pub mod dog;  pub mod human; diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs index 3a937c3..47629e4 100644 --- a/examples/unbound/main.rs +++ b/examples/unbound/main.rs @@ -2,6 +2,9 @@  #![deny(clippy::pedantic)]  #![allow(clippy::module_name_repetitions)] +use std::error::Error; + +mod animal_store;  mod animals;  mod bootstrap;  mod interfaces; @@ -10,7 +13,7 @@ use bootstrap::bootstrap;  use interfaces::dog::IDog;  use interfaces::human::IHuman; -fn main() +fn main() -> Result<(), Box<dyn Error>>  {      println!("Hello, world!"); @@ -20,7 +23,9 @@ fn main()      dog.woof(); -    let human = di_container.get::<dyn IHuman>().unwrap(); +    let human = di_container.get::<dyn IHuman>()?;      human.make_pets_make_sounds(); + +    Ok(())  } diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs index 1ab3192..49de7fa 100644 --- a/examples/with-3rd-party/bootstrap.rs +++ b/examples/with-3rd-party/bootstrap.rs @@ -1,4 +1,5 @@ -use syrette::errors::di_container::BindingBuilderError; +use std::error::Error; +  use syrette::ptr::TransientPtr;  use syrette::{declare_default_factory, DIContainer};  use third_party_lib::Shuriken; @@ -11,7 +12,7 @@ use crate::ninja::Ninja;  declare_default_factory!(Shuriken); -pub fn bootstrap() -> error_stack::Result<DIContainer, BindingBuilderError> +pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>  {      let mut di_container: DIContainer = DIContainer::new(); diff --git a/examples/with-3rd-party/main.rs b/examples/with-3rd-party/main.rs index f615ff5..dd4c21f 100644 --- a/examples/with-3rd-party/main.rs +++ b/examples/with-3rd-party/main.rs @@ -2,39 +2,22 @@  #![deny(clippy::pedantic)]  #![allow(clippy::module_name_repetitions)] -use std::fmt::Display; +use std::error::Error;  mod bootstrap;  mod interfaces;  mod ninja; -use error_stack::{Context, ResultExt}; -  use crate::bootstrap::bootstrap;  use crate::interfaces::ninja::INinja; -#[derive(Debug)] -struct ApplicationError; - -impl Display for ApplicationError -{ -    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result -    { -        f.write_str("An application error has occurred") -    } -} - -impl Context for ApplicationError {} - -fn main() -> error_stack::Result<(), ApplicationError> +fn main() -> Result<(), Box<dyn Error>>  {      println!("Hello, world!"); -    let di_container = bootstrap().change_context(ApplicationError)?; +    let di_container = bootstrap()?; -    let ninja = di_container -        .get::<dyn INinja>() -        .change_context(ApplicationError)?; +    let ninja = di_container.get::<dyn INinja>()?;      ninja.throw_shuriken();  | 
