Trait task_local::TaskLocal
source · [−]pub trait TaskLocal: Sized + 'static {
fn key() -> &'static LocalKey<Storage<Self>>;
fn try_local<F, R>(f: F) -> Result<R, StorageError>
where
F: FnOnce(&Self) -> R,
{ ... }
fn try_local_mut<F, R>(f: F) -> Result<R, StorageError>
where
F: FnOnce(&mut Self) -> R,
{ ... }
fn local<F, R>(f: F) -> R
where
F: FnOnce(&Self) -> R,
{ ... }
fn local_mut<F, R>(f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{ ... }
fn try_local_chain<F, R>(f: F) -> Result<R, StorageError>
where
F: FnOnce(StorageIter<'_, Self>) -> R,
{ ... }
fn try_local_chain_mut<F, R>(f: F) -> Result<R, StorageError>
where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
{ ... }
fn local_chain<F, R>(f: F) -> R
where
F: FnOnce(StorageIter<'_, Self>) -> R,
{ ... }
fn local_chain_mut<F, R>(f: F) -> R
where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
{ ... }
}
Expand description
Implementing this trait allows the type to be used with WithLocalExt::with_local
on
asynchronous tasks and be accessible via the other associated methods while
executing those tasks.
All of the access methods work with function parameters instead of returning
references because it can’t return a 'static
lifetime, there is no object
available to bind the lifetime to, and any references can’t be held across
.await
s.
You should ensure care that *_mut
are not called while within the call of
another method. It will panic if that happens. This is to ensure Rust’s
normal referential guarantees. Attempting to execute a nested task annotated
with the current local type while in one of these calls will also panic.
Implementations of TaskLocal
on different types do not interfere with
each other.
Required Methods
This returns a reference to the thread-local key that will be used to store task-local values of this type. This is auto-implemented by the derive macro, but if implemented manually should look something like this:
use std::thread::LocalKey;
use task_local::Storage;
fn key() -> &'static LocalKey<Storage<Context>> {
thread_local!(static STORAGE: Storage<Context> = Storage::default());
&STORAGE
}
Provided Methods
This method attempts to call the function with a reference to the currently stored value or return an error if it cannot be accessed.
fn try_local_mut<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(&mut Self) -> R,
fn try_local_mut<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(&mut Self) -> R,
This method attempts to call the function with a mutable reference to the currently stored value or return an error if it cannot be accessed.
This method will call the function with a reference to the currently stored value. It will panic if outside the execution of an annotated task.
This method will call the function with a mutable reference to the currently stored value. It will panic if outside the execution of an annotated task.
fn try_local_chain<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(StorageIter<'_, Self>) -> R,
fn try_local_chain<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(StorageIter<'_, Self>) -> R,
This method attempts to call the function with an iterator yielding
references to all the currently stored values, in most-recently-annotated
order or will return an error if it cannot be accessed. This will not
return StorageError::NoValue
, the iterator will simply be empty.
fn try_local_chain_mut<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
fn try_local_chain_mut<F, R>(f: F) -> Result<R, StorageError> where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
This method attempts to call the function with an iterator yielding
mutable references to all the currently stored values, in
most-recently-annotated order or will return an error if it cannot be
accessed. This will not return StorageError::NoValue
, the iterator
will simply be empty.
fn local_chain<F, R>(f: F) -> R where
F: FnOnce(StorageIter<'_, Self>) -> R,
fn local_chain<F, R>(f: F) -> R where
F: FnOnce(StorageIter<'_, Self>) -> R,
This method will call the function with an iterator yielding references to all the currently stored values, in most-recently-annotated order.
fn local_chain_mut<F, R>(f: F) -> R where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
fn local_chain_mut<F, R>(f: F) -> R where
F: FnOnce(StorageIterMut<'_, Self>) -> R,
This method will call the function with an iterator yielding mutable references to all the currently stored values, in most-recently-annotated order.