diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-04 15:24:32 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-04 15:25:58 +0200 |
commit | 14e45fe9aa2431120cfd6a7240f8bb94e45e730d (patch) | |
tree | 72bceb0580801ec1d59fc7f79e609f0ac7c53681 | |
parent | dce6bfc2321c0041fef5a2cb368ff45ba089198b (diff) |
refactor!: remove async_closure macro from API
BREAKING CHANGE: The async_closure macro has been removed. This is because it is completely out of scope for this crate
-rw-r--r-- | examples/async-factory/main.rs | 22 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 35 | ||||
-rw-r--r-- | src/lib.rs | 53 | ||||
-rw-r--r-- | src/test_utils.rs | 17 |
4 files changed, 50 insertions, 77 deletions
diff --git a/examples/async-factory/main.rs b/examples/async-factory/main.rs index b9beded..76efb2f 100644 --- a/examples/async-factory/main.rs +++ b/examples/async-factory/main.rs @@ -7,7 +7,7 @@ use std::time::Duration; use anyhow::Result; use syrette::di_container::asynchronous::prelude::*; use syrette::ptr::TransientPtr; -use syrette::{async_closure, declare_default_factory, factory}; +use syrette::{declare_default_factory, factory}; use tokio::time::sleep; trait IFoo: Send + Sync @@ -77,10 +77,12 @@ async fn main() -> Result<()> di_container .bind::<IFooFactory>() .to_async_factory(&|_| { - async_closure!(|cnt| { - let foo_ptr = Box::new(Foo::new(cnt)); + Box::new(|cnt| { + Box::pin(async move { + let foo_ptr = Box::new(Foo::new(cnt)); - foo_ptr as Box<dyn IFoo> + foo_ptr as Box<dyn IFoo> + }) }) }) .await?; @@ -88,13 +90,15 @@ async fn main() -> Result<()> di_container .bind::<dyn IPerson>() .to_async_default_factory(&|_| { - async_closure!(|| { - // Do some time demanding thing... - sleep(Duration::from_secs(1)).await; + Box::new(|| { + Box::pin(async { + // Do some time demanding thing... + sleep(Duration::from_secs(1)).await; - let person = TransientPtr::new(Person::new("Bob".to_string())); + let person = TransientPtr::new(Person::new("Bob".to_string())); - person as TransientPtr<dyn IPerson> + person as TransientPtr<dyn IPerson> + }) }) }) .await?; diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index b2d2b55..d33b840 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -229,7 +229,7 @@ where /// # use std::error::Error; /// # use std::time::Duration; /// # - /// # use syrette::{factory, async_closure}; + /// # use syrette::{factory}; /// # use syrette::di_container::asynchronous::prelude::*; /// # use syrette::ptr::TransientPtr; /// # @@ -254,12 +254,14 @@ where /// di_container /// .bind::<FooFactory>() /// .to_async_factory(&|_| { - /// async_closure!(|num, some_str| { - /// let bar = TransientPtr::new(Bar { num, some_str }); + /// Box::new(|num, some_str| { + /// Box::pin(async move { + /// let bar = TransientPtr::new(Bar { num, some_str }); /// - /// tokio::time::sleep(Duration::from_secs(2)).await; + /// tokio::time::sleep(Duration::from_secs(2)).await; /// - /// bar as TransientPtr<dyn Foo> + /// bar as TransientPtr<dyn Foo> + /// }) /// }) /// }) /// .await?; @@ -416,7 +418,6 @@ where /// # use std::error::Error; /// # use std::time::Duration; /// # - /// # use syrette::async_closure; /// # use syrette::di_container::asynchronous::prelude::*; /// # use syrette::ptr::TransientPtr; /// # @@ -438,15 +439,17 @@ where /// di_container /// .bind::<dyn Foo>() /// .to_async_default_factory(&|_| { - /// async_closure!(|| { - /// let bar = TransientPtr::new(Bar { - /// num: 42, - /// some_str: "hello".to_string(), - /// }); + /// Box::new(|| { + /// Box::pin(async { + /// let bar = TransientPtr::new(Bar { + /// num: 42, + /// some_str: "hello".to_string(), + /// }); /// - /// tokio::time::sleep(Duration::from_secs(1)).await; + /// tokio::time::sleep(Duration::from_secs(1)).await; /// - /// bar as TransientPtr<dyn Foo> + /// bar as TransientPtr<dyn Foo> + /// }) /// }) /// }) /// .await?; @@ -600,7 +603,8 @@ mod tests async fn can_bind_to_async_factory() -> Result<(), Box<dyn Error>> { use crate::ptr::TransientPtr; - use crate::{self as syrette, async_closure, factory}; + use crate::test_utils::async_closure; + use crate::{self as syrette, factory}; #[factory(async = true)] type IUserManagerFactory = dyn Fn(String) -> dyn subjects_async::IUserManager; @@ -699,7 +703,8 @@ mod tests use syrette_macros::declare_default_factory; use crate::ptr::TransientPtr; - use crate::{self as syrette, async_closure}; + use crate::test_utils::async_closure; + use crate::{self as syrette}; declare_default_factory!(dyn subjects_async::IUserManager, async = true); @@ -160,56 +160,3 @@ macro_rules! di_container_bind { syrette::declare_interface!($implementation -> $interface); }; } - -/// Creates a async closure. -/// -/// # Examples -/// ``` -/// # use syrette::async_closure; -/// # -/// # async fn do_heavy_operation(timeout: u32, size: u32) -> String { String::new() } -/// # -/// # async fn do_other_heavy_operation(input: String) -> String { String::new() } -/// # -/// async_closure!(|timeout, size| { -/// let value = do_heavy_operation(timeout, size).await; -/// -/// let final_value = do_other_heavy_operation(value).await; -/// -/// final_value -/// }); -/// ``` -/// -/// expands to the following -/// -/// ```rust -/// # async fn do_heavy_operation(timeout: u32, size: u32) -> String { String::new() } -/// # -/// # async fn do_other_heavy_operation(input: String) -> String { String::new() } -/// # -/// Box::new(|timeout, size| { -/// Box::pin(async move { -/// let value = do_heavy_operation(timeout, size).await; -/// -/// let final_value = do_other_heavy_operation(value).await; -/// -/// final_value -/// }) -/// }); -/// ``` -#[cfg(feature = "async")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "async")))] -#[cfg(not(tarpaulin_include))] -#[macro_export] -macro_rules! async_closure { - (|$($args: ident),*| { $($inner: stmt);* }) => { - Box::new(|$($args),*| { - Box::pin(async move { $($inner)* }) - }) - }; - (|| { $($inner: stmt);* }) => { - Box::new(|| { - Box::pin(async move { $($inner)* }) - }) - }; -} diff --git a/src/test_utils.rs b/src/test_utils.rs index 6fba778..8f07fa9 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -536,3 +536,20 @@ pub mod mocks impl crate::dependency_history::private::Sealed for DependencyHistory {} } } + +#[cfg(feature = "async")] +macro_rules! async_closure { + (|$($args: ident),*| { $($inner: stmt);* }) => { + Box::new(|$($args),*| { + Box::pin(async move { $($inner)* }) + }) + }; + (|| { $($inner: stmt);* }) => { + Box::new(|| { + Box::pin(async move { $($inner)* }) + }) + }; +} + +#[cfg(feature = "async")] +pub(crate) use async_closure; |