diff options
Diffstat (limited to 'src')
| -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 | 
3 files changed, 37 insertions, 68 deletions
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;  | 
