Trait async_local::AsyncLocal
source · pub trait AsyncLocal<T>where
T: 'static + AsContext,{
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, T::Target>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
T: '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, T::Target>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait;
fn with_blocking<F, R>(&'static self, f: F) -> JoinHandle<R>
where
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> R + Send + 'static,
R: Send + 'static;
unsafe fn local_ref(&'static self) -> LocalRef<T::Target>;
unsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, T::Target>;
}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, T::Target>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
T: '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, T::Target>) -> 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, T::Target>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
The async counterpart of LocalKey::with
sourcefn with_blocking<F, R>(&'static self, f: F) -> JoinHandle<R>where
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> R + Send + 'static,
R: Send + 'static,
fn with_blocking<F, R>(&'static self, f: F) -> JoinHandle<R>where
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> R + Send + 'static,
R: Send + 'static,
tokio-runtime or async-std-runtime only.A wrapper around spawn_blocking that appropriately constrains the lifetime of RefGuard
Use the tokio-runtime feature flag for tokio::task::spawn_blocking or async-std-runtime for async_std::task::spawn_blocking
sourceunsafe fn local_ref(&'static self) -> LocalRef<T::Target>
unsafe fn local_ref(&'static self) -> LocalRef<T::Target>
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:
-
ensure that
LocalRefrefers only to thread locals owned by runtime worker threads by creating within an async context such astokio::spawn,std::future::Future::poll, an async fn/block or within theDropof a pinnedstd::future::Futurethat createdLocalRefprior while pinned and polling. -
ensure that moves into
std::threadcannot occur unless managed bytokio::task::spawn_blockingorasync_std::task::spawn_blocking
sourceunsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, T::Target>
unsafe fn guarded_ref<'a>(&'static self) -> RefGuard<'a, T::Target>
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:
-
ensure that
RefGuardcan only refer to thread locals owned by runtime worker threads by runtime worker threads by creating within an async context such astokio::spawn,std::future::Future::poll, or an async fn/block or within theDropof a pinnedstd::future::Futurethat createdRefGuardprior while pinned and polling. -
Use borrows of any non-
'staticlifetime such as'async_traitas a way of contraining the lifetime as to preventRefGuardfrom being freely movable into blocking threads. Runtime managed threads spawned bytokio::task::spawn_blockingorasync_std::task::spawn_blockingcan be safely used by re-assigning lifetimes withstd::mem::transmute
Implementations on Foreign Types§
source§impl<T> AsyncLocal<T> for LocalKey<T>where
T: AsContext + 'static,
impl<T> AsyncLocal<T> for LocalKey<T>where
T: AsContext + '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, T::Target>) -> Fut + Send + 'async_trait,
Fut: Future<Output = R> + Send + 'async_trait,
source§fn with_blocking<F, R>(&'static self, f: F) -> JoinHandle<R>where
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> R + Send + 'static,
R: Send + 'static,
fn with_blocking<F, R>(&'static self, f: F) -> JoinHandle<R>where
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> R + Send + 'static,
R: Send + 'static,
tokio-runtime or async-std-runtime only.