Struct async_local::Context
source · pub struct Context<T: Sync + 'static>(/* private fields */);
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 soundness 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));
}
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Context<T>where
T: Freeze,
impl<T> RefUnwindSafe for Context<T>where
T: RefUnwindSafe,
impl<T> Send for Context<T>where
T: Send,
impl<T> Sync for Context<T>
impl<T> Unpin for Context<T>where
T: Unpin,
impl<T> UnwindSafe for Context<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more