From 9ab2f550a25c3e4465afa025e4ffb1294b331bdb Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 19 Jul 2022 13:11:19 +0200 Subject: docs: split example into multiple files --- example/src/main.rs | 145 +++------------------------------------------------- 1 file changed, 8 insertions(+), 137 deletions(-) (limited to 'example/src/main.rs') 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, - cat: InterfacePtr, - cow_factory: FactoryPtr, -} - -#[injectable(IHuman)] -impl Human -{ - fn new( - dog: InterfacePtr, - cat: InterfacePtr, - cow_factory: FactoryPtr, - ) -> 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 = (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::().to::(); - di_container.bind::().to::(); - di_container.bind::().to::(); - - di_container.bind::().to_factory(&|moo_cnt| { - let cow: Box = Box::new(Cow::new(moo_cnt)); - cow - }); + let di_container = bootstrap(); let dog = di_container.get::()?; -- cgit v1.2.3-18-g5258