Trait async_local::AsyncLocal
source · pub trait AsyncLocal<T>where
T: AsContext,{
// Required methods
fn with_async<F, R>(&'static self, f: F) -> WithLocal<T, F, R> ⓘ
where F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> Pin<Box<dyn Future<Output = R> + Send + 'a>>;
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 Methods§
sourcefn with_async<F, R>(&'static self, f: F) -> WithLocal<T, F, R> ⓘwhere
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> Pin<Box<dyn Future<Output = R> + Send + 'a>>,
fn with_async<F, R>(&'static self, f: F) -> WithLocal<T, F, R> ⓘwhere F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> Pin<Box<dyn Future<Output = R> + Send + 'a>>,
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 barrier-protected-runtime only.A wrapper around tokio::task::spawn_blocking that safely extends and constrains the lifetime of RefGuard
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 a barrier-protected Tokio 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_blockingand constrained therein
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 a barrier-protected Tokio 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. -
constrain to a non-
'staticlifetime as to preventRefGuardfrom being freely movable into blocking threads. Runtime managed threads spawned bytokio::task::spawn_blockingcan be safely used by re-assigning lifetimes withstd::mem::transmute -
closures can only adequately constrain the lifetime by using Higher-Rank Trait Bounds (see HRTBs); futures returned must be boxed, pinned and generic over an arbitrary lifetime
F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> Pin<Box<dyn Future<Output = R> + Send + 'a>>- for async trait fns using
async_torasync_trait, the lifetime'async_traitis suitable to constrainRefGuardfrom escaping to'static
Implementations on Foreign Types§
source§impl<T> AsyncLocal<T> for LocalKey<T>where
T: AsContext,
impl<T> AsyncLocal<T> for LocalKey<T>where T: AsContext,
fn with_async<F, R>(&'static self, f: F) -> WithLocal<T, F, R> ⓘwhere F: for<'a> FnOnce(RefGuard<'a, T::Target>) -> Pin<Box<dyn Future<Output = R> + Send + 'a>>,
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 barrier-protected-runtime only.