Expand description
This lib provide an trait Context
and a Timer
to control async function.
§Example
Here is a simple example. We use Timer
to control an async function
to finish in 5 seconds.
use std::time;
use context_async::{Context, Timer};
async fn a_heavy_async_function(a: u8) -> bool {
return true;
}
let timer = Timer::with_timeout(time::Duration::from_secs(5));
let _ = timer.handle(a_heavy_async_function(0)).await;
// or:
use context_async::With;
let _ = a_heavy_async_function(0).with(&timer).await;
Timer
implements Context
, you can pass a Context
to your async function:
use std::time;
use context_async::{Context, Error, Timer, With};
async fn a_heavy_async_function(a: u8) -> bool {
return true;
}
async fn my_function<Ctx: Context>(ctx: Ctx) -> Result<bool, Error> {
a_heavy_async_function(0)
.with(ctx)
.await
}
let timer = Timer::with_timeout(time::Duration::from_secs(5));
let _ = my_function(&timer).await;
§Error
Context
returns Error
, one of Error::ContextCancelled
or Error::ContextTimeout
.
§Features
Structs§
- Name
name
Enums§
Traits§
- The
Context
trait defines the required methods forContext
. It can define a duration, be cancellable, and immediately cancel async functions if the processing time exceeds the allowed duration without being cancelled. Additionally, theContext
can spawn child contexts.