From 3a0432e690f2ad7af4fb7ccf98ccfba6b3d49705 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 11 Nov 2022 12:42:39 +0100 Subject: docs: add a example to the crate root --- src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3bddf1b..3bb921b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,87 @@ //! Syrette //! -//! Syrette is a collection of utilities useful for performing dependency injection. +//! Syrette is a framework for utilizing inversion of control & dependency injection. +//! +//! # Example +//! ``` +//! use std::error::Error; +//! +//! use syrette::di_container::blocking::prelude::*; +//! use syrette::injectable; +//! use syrette::ptr::TransientPtr; +//! +//! trait IWeapon +//! { +//! fn deal_damage(&self, damage: i32); +//! } +//! +//! struct Sword {} +//! +//! #[injectable(IWeapon)] // Makes Sword injectable with a interface of IWeapon +//! impl Sword +//! { +//! fn new() -> Self +//! { +//! Self {} +//! } +//! } +//! +//! impl IWeapon for Sword +//! { +//! fn deal_damage(&self, damage: i32) +//! { +//! println!("Sword dealt {} damage!", damage); +//! } +//! } +//! +//! trait IWarrior +//! { +//! fn fight(&self); +//! } +//! +//! struct Warrior +//! { +//! weapon: TransientPtr, +//! } +//! +//! #[injectable(IWarrior)] // Makes Warrior injectable with a interface of IWarrior +//! impl Warrior +//! { +//! fn new(weapon: TransientPtr) -> Self +//! { +//! Self { weapon } +//! } +//! } +//! +//! impl IWarrior for Warrior +//! { +//! fn fight(&self) +//! { +//! self.weapon.deal_damage(30); +//! } +//! } +//! +//! fn main() -> Result<(), Box> +//! { +//! let mut di_container = DIContainer::new(); +//! +//! // Creates a binding of the interface IWeapon to the concrete type Sword +//! di_container.bind::().to::()?; +//! +//! // Creates a binding of the interface IWarrior to the concrete type Warrior +//! di_container.bind::().to::()?; +//! +//! // Create a transient IWarrior with all of its dependencies automatically injected +//! let warrior = di_container.get::()?.transient()?; +//! +//! warrior.fight(); +//! +//! println!("Warrior has fighted"); +//! +//! Ok(()) +//! } +//! ``` pub mod dependency_history; pub mod di_container; -- cgit v1.2.3-18-g5258