diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-19 13:11:19 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-19 13:11:19 +0200 |
commit | 9ab2f550a25c3e4465afa025e4ffb1294b331bdb (patch) | |
tree | af7d8b999934a27b22782b0081b94c5ef0f3a5de /example/src/main.rs | |
parent | ff71b49f014613729237178f0d3ea374f7d72cd5 (diff) |
docs: split example into multiple files
Diffstat (limited to 'example/src/main.rs')
-rw-r--r-- | example/src/main.rs | 145 |
1 files changed, 8 insertions, 137 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index d79a030..956b79e 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,151 +1,22 @@ #![deny(clippy::all)] #![deny(clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] use syrette::errors::di_container::DIContainerError; -use syrette::interfaces::factory::IFactory; -use syrette::ptr::{FactoryPtr, InterfacePtr}; -use syrette::{factory, injectable, DIContainer}; -trait IDog -{ - fn woof(&self); -} - -struct Dog {} - -#[injectable(IDog)] -impl Dog -{ - fn new() -> Self - { - Self {} - } -} - -impl IDog for Dog -{ - fn woof(&self) - { - println!("Woof!"); - } -} - -trait ICat -{ - fn meow(&self); -} - -struct Cat {} - -#[injectable(ICat)] -impl Cat -{ - fn new() -> Self - { - Self {} - } -} - -impl ICat for Cat -{ - fn meow(&self) - { - println!("Meow!"); - } -} - -trait ICow -{ - fn moo(&self); -} - -struct Cow -{ - moo_cnt: i32, -} +mod animals; +mod bootstrap; +mod interfaces; -impl Cow -{ - fn new(moo_cnt: i32) -> Self - { - Self { moo_cnt } - } -} - -impl ICow for Cow -{ - fn moo(&self) - { - for _ in 0..self.moo_cnt { - println!("Moo"); - } - } -} - -#[factory] -type CowFactory = dyn IFactory<(i32,), dyn ICow>; - -trait IHuman -{ - fn make_pets_make_sounds(&self); -} - -struct Human -{ - dog: InterfacePtr<dyn IDog>, - cat: InterfacePtr<dyn ICat>, - cow_factory: FactoryPtr<CowFactory>, -} - -#[injectable(IHuman)] -impl Human -{ - fn new( - dog: InterfacePtr<dyn IDog>, - cat: InterfacePtr<dyn ICat>, - cow_factory: FactoryPtr<CowFactory>, - ) -> Self - { - Self { - dog, - cat, - cow_factory, - } - } -} - -impl IHuman for Human -{ - fn make_pets_make_sounds(&self) - { - println!("Hi doggy!"); - - self.dog.woof(); - - println!("Hi kitty!"); - - self.cat.meow(); - - let cow: Box<dyn ICow> = (self.cow_factory)(3); - - cow.moo(); - } -} +use bootstrap::bootstrap; +use interfaces::dog::IDog; +use interfaces::human::IHuman; fn main() -> error_stack::Result<(), DIContainerError> { println!("Hello, world!"); - let mut di_container: DIContainer = DIContainer::new(); - - di_container.bind::<dyn IDog>().to::<Dog>(); - di_container.bind::<dyn ICat>().to::<Cat>(); - di_container.bind::<dyn IHuman>().to::<Human>(); - - di_container.bind::<CowFactory>().to_factory(&|moo_cnt| { - let cow: Box<dyn ICow> = Box::new(Cow::new(moo_cnt)); - cow - }); + let di_container = bootstrap(); let dog = di_container.get::<dyn IDog>()?; |