diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-18 20:20:53 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-18 20:20:53 +0200 |
commit | 6813690e893bc461bd2be60509850a5a80454c6a (patch) | |
tree | f84e5321a0f608beda6b3abbf82de5c9496efc3e /src/lib.rs | |
parent | 0b914f415cb04c45d8655cae3828af264887d203 (diff) |
feat: add binding async factories to async DI container
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -16,6 +16,9 @@ pub mod ptr; pub mod async_di_container; #[cfg(feature = "async")] +pub mod future; + +#[cfg(feature = "async")] pub use async_di_container::AsyncDIContainer; pub use di_container::DIContainer; pub use syrette_macros::*; @@ -75,3 +78,51 @@ macro_rules! di_container_bind { syrette::declare_interface!($implementation -> $interface); }; } + +/// Creates a async closure. +/// +/// *This macro is only available if Syrette is built with the "async" feature.* +/// +/// # 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")] +#[macro_export] +macro_rules! async_closure { + (|$($args: ident),*| { $($inner: stmt);* }) => { + Box::new(|$($args),*| { + Box::pin(async move { $($inner)* }) + }) + }; +} |