Struct async_local::Context
source · pub struct Context<T: Sync + 'static>(_);
Expand description
A wrapper type used for creating pointers to thread-locals
Implementations§
source§impl<T> Context<T>where
T: Sync,
impl<T> Context<T>where T: Sync,
sourcepub fn new(inner: T) -> Context<T>
pub fn new(inner: T) -> Context<T>
Create a new thread-local context
If the barrier-protected-runtime
feature flag isn’t enabled, Context
will use Box::leak
to avoid T
ever being deallocated instead of relying on the provided barrier-protected Tokio runtime to ensure tasks never outlive thread local data owned by worker threads. This provides compatibility at the cost of performance for whenever it’s not well-known that the async runtime used is the Tokio runtime
configured by this crate.
Usage
Either wrap a type with Context
and assign to a thread-local, or use as an unwrapped field in a struct that derives AsContext
Example
use std::sync::atomic::AtomicUsize;
use async_local::Context;
thread_local! {
static COUNTER: Context<AtomicUsize> = Context::new(AtomicUsize::new(0));
}