use std::future::Future;
use std::pin::Pin;
pub fn box_future<F, O>(future: F) -> Pin<Box<dyn Future<Output = O> + Send>>
where
F: Future<Output = O> + Send + 'static,
{
Box::pin(future)
}
#[macro_export]
macro_rules! boxed_async {
(async $block:block) => {
Box::pin(async $block)
};
(async move $block:block) => {{
Box::pin(async move $block)
}};
}
#[macro_export]
macro_rules! impl_transaction_handler {
($type:ty, $db:ty, $item:ty, $error:ty) => {
#[async_trait::async_trait]
impl $crate::handlers::TransactionHandler<$db> for $type {
type Item = $item;
type Error = $error;
async fn execute(
self,
ctx: &mut $crate::TestContext<$db>,
) -> Result<Self::Item, Self::Error> {
self.execute_impl(ctx).await
}
}
};
}
pub fn boxed_future<T, F, Fut, E>(
f: F,
) -> impl FnOnce(T) -> Pin<Box<dyn Future<Output = Result<(), E>> + Send>>
where
F: FnOnce(T) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), E>> + Send + 'static,
T: Send + 'static,
E: Send + 'static,
{
move |t| {
let future = f(t);
Box::pin(future) as Pin<Box<dyn Future<Output = Result<(), E>> + Send>>
}
}
#[macro_export]
macro_rules! db_test {
($backend:expr) => {
$crate::with_boxed_database($backend)
};
($backend:expr, $config:expr) => {
$crate::with_boxed_database_config($backend, $config)
};
}
#[macro_export]
#[rustfmt::skip]
macro_rules! setup {
($backend:expr, |$conn:ident| $body:expr) => {
$crate::with_boxed_database($backend)
.setup(|$conn| $crate::boxed_async!($body))
};
}
#[macro_export]
#[rustfmt::skip]
macro_rules! transaction {
($backend:expr, |$conn:ident| $body:expr) => {
$crate::with_boxed_database($backend)
.with_transaction(|$conn| $crate::boxed_async!($body))
};
}
#[macro_export]
#[rustfmt::skip]
macro_rules! setup_and_transaction {
($backend:expr,
setup: |$setup_conn:ident| $setup_body:expr,
transaction: |$tx_conn:ident| $tx_body:expr) => {
$crate::with_boxed_database($backend)
.setup(|$setup_conn| $crate::boxed_async!($setup_body))
.with_transaction(|$tx_conn| $crate::boxed_async!($tx_body))
};
}