Function with_async_context

Source
pub fn with_async_context<C, T, F>(ctx: C, future: F) -> AsyncContext<C, T, F> 
where C: 'static + ToString, F: Future<Output = T>,
Expand description

Creates a new async context and executes the provided future within it

§Arguments

  • ctx - The context data to make available during future execution
  • future - The future to execute within the context

§Panics

Panics if attempting to create a nested context when one already exists

§Examples

use with_async_context::with_async_context;

struct MyContext;

impl MyContext {
    fn new() -> Self {
        MyContext
    }
}

impl ToString for MyContext {
    fn to_string(&self) -> String {
        "MyContext".into()
    }
}

async fn async_function() {}

let result = with_async_context(
    MyContext::new(),
    async_function()
).await;