From 344dff10f8ac2e176239e10190bdb627bf58156b Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 1 Oct 2022 21:03:17 +0200 Subject: docs: add async binding builder examples --- src/async_di_container.rs | 197 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/src/async_di_container.rs b/src/async_di_container.rs index 0a092d6..f8ce92d 100644 --- a/src/async_di_container.rs +++ b/src/async_di_container.rs @@ -227,6 +227,37 @@ where /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::{AsyncDIContainer, injectable}; + /// # + /// # trait Foo: Send + Sync {} + /// # + /// # struct Bar {} + /// # + /// # #[injectable(Foo, async = true)] + /// # impl Bar { + /// # fn new() -> Self + /// # { + /// # Self {} + /// # } + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # #[tokio::main] + /// # async fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = AsyncDIContainer::new(); + /// # + /// di_container.bind::().to::().await?; + /// # + /// # Ok(()) + /// # } + /// ``` pub async fn to( &self, ) -> Result< @@ -263,6 +294,46 @@ where /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::{AsyncDIContainer, factory}; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait Foo: Send + Sync {} + /// # + /// # struct Bar + /// # { + /// # num: i32, + /// # some_str: String + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # #[factory(threadsafe = true)] + /// # type FooFactory = dyn Fn(i32, String) -> dyn Foo; + /// # + /// # #[tokio::main] + /// # async fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = AsyncDIContainer::new(); + /// # + /// di_container + /// .bind::() + /// .to_factory(&|_| { + /// Box::new(|num, some_str| { + /// let bar = TransientPtr::new(Bar { num, some_str }); + /// + /// bar as TransientPtr + /// }) + /// }) + /// .await?; + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(feature = "factory")] pub async fn to_factory( &self, @@ -307,6 +378,49 @@ where /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # use std::time::Duration; + /// # + /// # use syrette::{AsyncDIContainer, factory, async_closure}; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait Foo: Send + Sync {} + /// # + /// # struct Bar + /// # { + /// # num: i32, + /// # some_str: String + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # #[factory(async = true)] + /// # type FooFactory = dyn Fn(i32, String) -> dyn Foo; + /// # + /// # #[tokio::main] + /// # async fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = AsyncDIContainer::new(); + /// # + /// di_container + /// .bind::() + /// .to_async_factory(&|_| { + /// async_closure!(|num, some_str| { + /// let bar = TransientPtr::new(Bar { num, some_str }); + /// + /// tokio::time::sleep(Duration::from_secs(2)).await; + /// + /// bar as TransientPtr + /// }) + /// }) + /// .await?; + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(all(feature = "factory", feature = "async"))] pub async fn to_async_factory( &self, @@ -355,6 +469,46 @@ where /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::AsyncDIContainer; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait Foo: Send + Sync {} + /// # + /// # struct Bar + /// # { + /// # num: i32, + /// # some_str: String + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # #[tokio::main] + /// # async fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = AsyncDIContainer::new(); + /// # + /// di_container + /// .bind::() + /// .to_default_factory(&|_| { + /// Box::new(|| { + /// let bar = TransientPtr::new(Bar { + /// num: 42, + /// some_str: "hello".to_string(), + /// }); + /// + /// bar as TransientPtr + /// }) + /// }) + /// .await?; + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(feature = "factory")] pub async fn to_default_factory( &self, @@ -400,6 +554,49 @@ where /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # use std::time::Duration; + /// # + /// # use syrette::{AsyncDIContainer, async_closure}; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait Foo: Send + Sync {} + /// # + /// # struct Bar + /// # { + /// # num: i32, + /// # some_str: String + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # #[tokio::main] + /// # async fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = AsyncDIContainer::new(); + /// # + /// di_container + /// .bind::() + /// .to_async_default_factory(&|_| { + /// async_closure!(|| { + /// let bar = TransientPtr::new(Bar { + /// num: 42, + /// some_str: "hello".to_string(), + /// }); + /// + /// tokio::time::sleep(Duration::from_secs(1)).await; + /// + /// bar as TransientPtr + /// }) + /// }) + /// .await?; + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(all(feature = "factory", feature = "async"))] pub async fn to_async_default_factory( &self, -- cgit v1.2.3-18-g5258