SyncWrapContext

Trait SyncWrapContext 

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

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

Context Manager definition (sync hooks)

The defined context, is suitable for initialisation, before and after the execution that requires the execution of synchronous code. If you need to have context initialisation or before/after hooks to be asynchronous, please consider using AsyncWrapContext instead.

Implementers are then expected to be used via the wrap macro

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

#[wrap(PrintDuration)]
fn sync_foo() -> usize {
    do_something_expensive()
}

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

or via the SyncWrapContext::run_sync and SyncWrapContext::run_async associated functions.

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

let sync_run_output: &'static str = PrintDuration::run_sync(CallerContext::new("manual"), || {
    "sync"
});
let async_run_output: &'static str = PrintDuration::run_async(CallerContext::new("manual"), async {
    "async"
}).await;

Required Methods§

Source

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

Initialize the context

Provided Methods§

Source

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

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

fn run_sync(caller_context: CallerContext, block: impl FnOnce() -> T) -> T
where Self: Sized,

Execute a synchronous 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 callable to wrap and execute

Usage example:

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

let async_run_output: &'static str = PrintDuration::run_sync(CallerContext::new("manual"), || {
    "sync"
});
Source

async fn run_async( 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> SyncWrapContext<T> for PrintDuration {
  fn new() -> Self { Self }
}

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

Implementors§