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 compat
feature flag is enabled, Context
will downgrade to internally using std::sync::Arc
to ensure the validity of T
§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, Ordering};
use async_local::{AsyncLocal, Context};
use generativity::make_guard;
thread_local! {
static COUNTER: Context<AtomicUsize> = Context::new(AtomicUsize::new(0));
}
#[async_local::main(flavor = "current_thread")]
async fn main() {
make_guard!(guard);
let counter = COUNTER.local_ref(guard);
let _count = counter
.with_blocking(|counter| counter.fetch_add(1, Ordering::Relaxed))
.await
.unwrap();
}
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