Trait async_local::AsyncLocal
source · pub trait AsyncLocal<T, Ref>where
T: 'static + AsContext<Ref>,
Ref: Sync + 'static,{
type impl_trait_with_async_0<'async_trait, F, R: 'async_trait, Fut>: Future<Output = R> + 'async_trait + Send
where
F: FnOnce(RefGuard<'async_trait, Ref>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
T: 'async_trait,
Ref: 'async_trait,
Self: 'static;
fn with_async<'async_trait, F, R: 'async_trait, Fut>(
&'static self,
f: F
) -> Self::impl_trait_with_async_0<'async_trait, F, R, Fut>
where
F: FnOnce(RefGuard<'async_trait, Ref>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait;
unsafe fn local_ref(&'static self) -> LocalRef<Ref>;
unsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, Ref>;
}Expand description
LocalKey extension for creating thread-safe pointers to thread-local Context
Required Associated Types§
type impl_trait_with_async_0<'async_trait, F, R: 'async_trait, Fut>: Future<Output = R> + 'async_trait + Send
where
F: FnOnce(RefGuard<'async_trait, Ref>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
T: 'async_trait,
Ref: 'async_trait,
Self: 'static
Required Methods§
sourcefn with_async<'async_trait, F, R: 'async_trait, Fut>(
&'static self,
f: F
) -> Self::impl_trait_with_async_0<'async_trait, F, R, Fut>where
F: FnOnce(RefGuard<'async_trait, Ref>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
fn with_async<'async_trait, F, R: 'async_trait, Fut>(
&'static self,
f: F
) -> Self::impl_trait_with_async_0<'async_trait, F, R, Fut>where
F: FnOnce(RefGuard<'async_trait, Ref>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
The async counterpart of LocalKey::with
sourceunsafe fn local_ref(&'static self) -> LocalRef<Ref>
unsafe fn local_ref(&'static self) -> LocalRef<Ref>
Create a thread-safe pointer to a thread local Context
Safety
The only safe way to use LocalRef is within the context of an async runtime
The well-known way of safely accomplishing these guarantees is to:
-
ensure that
LocalRefcan only refer to a thread local within the context of the runtime by creating and using only within an async context such as withintokio::spawn,std::future::Future::poll, an async fn or block or within the drop of a pinnedstd::future::Futurethat createdLocalRefprior while pinned and polling. -
ensure that
LocalRefcannot be dereferenced outside of the async runtime context or any thread scoped therein -
use
pin_project::pinned_dropto ensure the safety of dereferencingLocalRefon the drop impl of a pinned future that createdLocalRefwhile polling. -
ensure that a move into
std::threadcannot occur or otherwise thatLocalRefcannot be created nor derefenced outside of an async context by constraining use exclusively to within a pinnedstd::future::Futurebeing polled or dropped and otherwise usingRefGuardexplicitly over any non-'staticlifetime such as'async_traitto allow more flexible usage combined with async traits -
only use
std::thread::scopewith validly createdLocalRef
sourceunsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, Ref>
unsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, Ref>
Create a lifetime-constrained thread-safe pointer to a thread local Context
Safety
The only safe way to use RefGuard is within the context of an async runtime
The well-known way of safely accomplishing these guarantees is to:
-
ensure that
RefGuardcan only refer to a thread local within the context of the async runtime by creating within an async context such astokio::spawn,std::future::Future::poll, or an async fn or block or within the drop of a pinnedstd::future::Futurethat createdRefGuardprior while pinned and polling. -
Use borrows of any non-
'staticlifetime such as'async_traitas a way of constraining the lifetime and preventingRefGuardfrom being movable into a blocking thread.