From 862453174dc15e5184a4f86bb14f203ccef94de6 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 19 Sep 2023 20:37:28 +0200 Subject: docs: add examples to DI container & related functions --- src/di_container/blocking/mod.rs | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'src/di_container/blocking/mod.rs') diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 69efe9a..df6e68c 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -95,6 +95,30 @@ impl DIContainer impl DIContainer { /// Returns a new [`BindingBuilder`] for the given interface. + /// + /// # Examples + /// ``` + /// # use syrette::{DIContainer, injectable}; + /// # + /// # struct DiskWriter {} + /// # + /// # #[injectable] + /// # impl DiskWriter + /// # { + /// # fn new() -> Self + /// # { + /// # Self {} + /// # } + /// # } + /// # + /// # fn main() -> Result<(), Box> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container.bind::().to::()?; + /// # + /// # Ok(()) + /// # } + /// ``` #[allow(clippy::missing_panics_doc)] pub fn bind(&mut self) -> BindingBuilder<'_, Interface> where @@ -114,6 +138,32 @@ impl DIContainer /// - No binding for `Interface` exists /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails + /// + /// # Examples + /// ``` + /// # use syrette::{DIContainer, injectable}; + /// # + /// # struct DeviceManager {} + /// # + /// # #[injectable] + /// # impl DeviceManager + /// # { + /// # fn new() -> Self + /// # { + /// # Self {} + /// # } + /// # } + /// # + /// # fn main() -> Result<(), Box> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container.bind::().to::()?; + /// + /// let device_manager = di_container.get::()?.transient(); + /// # + /// # Ok(()) + /// # } + /// ``` pub fn get(&self) -> Result, DIContainerError> where Interface: 'static + ?Sized, @@ -128,6 +178,36 @@ impl DIContainer /// - No binding for `Interface` with name `name` exists /// - Resolving the binding for `Interface` fails /// - Casting the binding for `Interface` fails + /// + /// # Examples + /// ``` + /// # use syrette::{DIContainer, injectable}; + /// # + /// # struct DeviceManager {} + /// # + /// # #[injectable] + /// # impl DeviceManager + /// # { + /// # fn new() -> Self + /// # { + /// # Self {} + /// # } + /// # } + /// # + /// # fn main() -> Result<(), Box> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container + /// .bind::() + /// .to::()? + /// .in_transient_scope() + /// .when_named("usb")?; + /// + /// let device_manager = di_container.get_named::("usb")?.transient(); + /// # + /// # Ok(()) + /// # } + /// ``` pub fn get_named( &self, name: &'static str, -- cgit v1.2.3-18-g5258