AsyncWrapContext

Trait AsyncWrapContext 

Source
pub trait AsyncWrapContext<T> {
    // Required method
    async fn new() -> Self
       where Self: Sized;

    // Provided methods
    async fn before(&self, caller_context: &CallerContext) { ... }
    async fn after(self, caller_context: &CallerContext, result: &T)
       where Self: Sized { ... }
    async fn run(
        caller_context: CallerContext,
        block: impl Future<Output = T>,
    ) -> T
       where Self: Sized { ... }
}
Expand description

Context Manager definition (async hooks)

The defined context, is suitable for initialisation, before and after the execution that requires the execution of asynchronous code. If you have context context initialisation or before/after hooks all being syncrhonous, then please consider using SyncWrapContext instead.

IMPORTANT: AsyncWrapContext does not support running synchronous blocks. This is intentional in order to avoid possibly stalling the async-runtime in use. Please consider wrapping yourself the synchronous code in an async block, or using SyncWrapContext whether possible.

Implementers are then expected to be used via the wrap macro

struct AsyncPrintDuration;
impl<T> AsyncWrapContext<T> for AsyncPrintDuration {
   async fn new() -> Self { Self }
}

#[async_wrap(AsyncPrintDuration)]
async fn async_foo() -> usize {
    do_something_expensive().await
}

or via the AsyncWrapContext::run associated function.

struct AsyncPrintDuration;
impl<T> AsyncWrapContext<T> for AsyncPrintDuration {
  async fn new() -> Self { Self }
}

let async_run_output: &'static str = AsyncPrintDuration::run(CallerContext::new("manual"), async {
    "async"
}).await;

Required Methods§

Source

async fn new() -> Self
where Self: Sized,

Initialize the context

Provided Methods§

Source

async fn before(&self, caller_context: &CallerContext)

Execute the code before the execution of the wrapped body

Parameters:

  • caller_context: Context of the caller (including the name of the function that is being wrapped)
Source

async fn after(self, caller_context: &CallerContext, result: &T)
where Self: Sized,

Execute the code after the execution of the wrapped body, it provides also the result of the wrapped body

Parameters:

  • caller_context: Context of the caller (including the name of the function that is being wrapped)
  • result: The result of the wrapped body
Source

async fn run(caller_context: CallerContext, block: impl Future<Output = T>) -> T
where Self: Sized,

Execute a asynchronous block of code wrapped by the context

This will lead to context initialisation and execution of before/after hooks

Parameters:

  • caller_context: Context of the caller (including the name of the function that is being wrapped)
  • block: the future to wrap and execute

Usage example:

struct PrintDuration;
impl<T> AsyncWrapContext<T> for PrintDuration {
  async fn new() -> Self { Self }
}

let async_run_output: &'static str = PrintDuration::run(CallerContext::new("manual"), async {
    "async"
}).await;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§