Struct async_local::Context
source · pub struct Context<T: Sync>(_);
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
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
Safety
Types that use Context
must not impl std::ops::Drop
because doing so results in the thread_local
macro registering destructor functions that cannot be deferred by blocking with std::sync::Condvar
Example
use std::sync::atomic::AtomicUsize;
use async_local::Context;
thread_local! {
static COUNTER: Context<AtomicUsize> = Context::new(AtomicUsize::new(0));
}