arcium-primitives 0.4.2

Arcium primitives
Documentation
/// Macro that joins futures and ensures all have completed before any values are dropped.
///
/// Uses a barrier synchronization pattern to make sure all futures are completed before returning.
/// This is useful when the futures are expected to have side effects that need to be synchronized.
#[macro_export]
macro_rules! sync_join {
    ($($fut:expr),+ $(,)?) => {{
        use tokio::sync::Barrier;
        use std::sync::Arc;

        // Count the number of futures
        let future_count = [$($fut),+].len();

        // Create a shared barrier for all futures
        let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(future_count));

        // Create synchronized futures
        tokio::join!(
            $({
                let barrier = barrier.clone();
                async move {
                    // Execute the original future
                    let result = $fut.await?;
                    // Wait for all futures to reach this point
                    barrier.wait().await;
                    Ok(result)
                }
            }),+
        )
    }};
}

/// Macro that try_joins futures and ensures all have completed before any values are dropped.
///
/// Uses a barrier synchronization pattern to make sure all futures are completed before returning.
/// This is useful when the futures are expected to have side effects that need to be synchronized.
#[macro_export]
macro_rules! sync_try_join {
    ($($fut:expr),+ $(,)?) => {{
        // Count the number of futures
        let future_count = [$($fut),+].len();

        // Create a shared barrier for all futures
        let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(future_count));

        // Create synchronized futures
        tokio::try_join!(
            $({
                let barrier = barrier.clone();
                async move {
                    // Execute the original future
                    let result = $fut.await?;
                    // Wait for all futures to reach this point
                    barrier.wait().await;
                    Ok(result)
                }
            }),+
        )
    }};
}