aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/asynchronous/mod.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-09-19 20:37:28 +0200
committerHampusM <hampus@hampusmat.com>2023-09-19 21:32:32 +0200
commit862453174dc15e5184a4f86bb14f203ccef94de6 (patch)
tree900cb5e7f93735b5f81b3cbc0c4934688a4307ce /src/di_container/asynchronous/mod.rs
parent4fd0d6b4951b08a20d5378bca75561109dc6d036 (diff)
docs: add examples to DI container & related functions
Diffstat (limited to 'src/di_container/asynchronous/mod.rs')
-rw-r--r--src/di_container/asynchronous/mod.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs
index 4be232d..929e2c0 100644
--- a/src/di_container/asynchronous/mod.rs
+++ b/src/di_container/asynchronous/mod.rs
@@ -93,6 +93,31 @@ impl AsyncDIContainer
impl AsyncDIContainer
{
/// Returns a new [`AsyncBindingBuilder`] for the given interface.
+ ///
+ /// # Examples
+ /// ```
+ /// # use syrette::{AsyncDIContainer, injectable};
+ /// #
+ /// # struct DiskWriter {}
+ /// #
+ /// # #[injectable(async = true)]
+ /// # impl DiskWriter
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self {}
+ /// # }
+ /// # }
+ /// #
+ /// # #[tokio::main]
+ /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = AsyncDIContainer::new();
+ ///
+ /// di_container.bind::<DiskWriter>().to::<DiskWriter>().await?;
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
#[allow(clippy::missing_panics_doc)]
pub fn bind<Interface>(&mut self) -> AsyncBindingBuilder<'_, Interface>
where
@@ -112,6 +137,36 @@ impl AsyncDIContainer
/// - No binding for `Interface` exists
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
+ ///
+ /// # Examples
+ /// ```
+ /// # use syrette::{AsyncDIContainer, injectable};
+ /// #
+ /// # struct DeviceManager {}
+ /// #
+ /// # #[injectable(async = true)]
+ /// # impl DeviceManager
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self {}
+ /// # }
+ /// # }
+ /// #
+ /// # #[tokio::main]
+ /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = AsyncDIContainer::new();
+ ///
+ /// di_container
+ /// .bind::<DeviceManager>()
+ /// .to::<DeviceManager>()
+ /// .await?;
+ ///
+ /// let device_manager = di_container.get::<DeviceManager>().await?.transient();
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
pub async fn get<Interface>(
&self,
) -> Result<SomePtr<Interface>, AsyncDIContainerError>
@@ -129,6 +184,43 @@ impl AsyncDIContainer
/// - No binding for `Interface` with name `name` exists
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
+ ///
+ /// # Examples
+ /// ```
+ /// # use syrette::{AsyncDIContainer, injectable};
+ /// #
+ /// # struct DeviceManager {}
+ /// #
+ /// # #[injectable(async = true)]
+ /// # impl DeviceManager
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self {}
+ /// # }
+ /// # }
+ /// #
+ /// # #[tokio::main]
+ /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = AsyncDIContainer::new();
+ ///
+ /// di_container
+ /// .bind::<DeviceManager>()
+ /// .to::<DeviceManager>()
+ /// .await?
+ /// .in_transient_scope()
+ /// .await
+ /// .when_named("usb")
+ /// .await;
+ ///
+ /// let device_manager = di_container
+ /// .get_named::<DeviceManager>("usb")
+ /// .await?
+ /// .transient();
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
pub async fn get_named<Interface>(
&self,
name: &'static str,