diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-19 20:37:28 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-19 21:32:32 +0200 |
commit | 862453174dc15e5184a4f86bb14f203ccef94de6 (patch) | |
tree | 900cb5e7f93735b5f81b3cbc0c4934688a4307ce /src/di_container/blocking/mod.rs | |
parent | 4fd0d6b4951b08a20d5378bca75561109dc6d036 (diff) |
docs: add examples to DI container & related functions
Diffstat (limited to 'src/di_container/blocking/mod.rs')
-rw-r--r-- | src/di_container/blocking/mod.rs | 80 |
1 files changed, 80 insertions, 0 deletions
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<dyn std::error::Error>> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container.bind::<DiskWriter>().to::<DiskWriter>()?; + /// # + /// # Ok(()) + /// # } + /// ``` #[allow(clippy::missing_panics_doc)] pub fn bind<Interface>(&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<dyn std::error::Error>> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container.bind::<DeviceManager>().to::<DeviceManager>()?; + /// + /// let device_manager = di_container.get::<DeviceManager>()?.transient(); + /// # + /// # Ok(()) + /// # } + /// ``` pub fn get<Interface>(&self) -> Result<SomePtr<Interface>, 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<dyn std::error::Error>> { + /// let mut di_container = DIContainer::new(); + /// + /// di_container + /// .bind::<DeviceManager>() + /// .to::<DeviceManager>()? + /// .in_transient_scope() + /// .when_named("usb")?; + /// + /// let device_manager = di_container.get_named::<DeviceManager>("usb")?.transient(); + /// # + /// # Ok(()) + /// # } + /// ``` pub fn get_named<Interface>( &self, name: &'static str, |