[−][src]Struct picollar::tasktrack::LocalKey
A key for task-local data.
This type is generated by the task_local!
macro.
Unlike std::thread::LocalKey
, tokio::task::LocalKey
will
not lazily initialize the value on first access. Instead, the
value is first initialized when the future containing
the task-local is first polled by a futures executor, like Tokio.
Examples
tokio::task_local! { static NUMBER: u32; } NUMBER.scope(1, async move { assert_eq!(NUMBER.get(), 1); }).await; NUMBER.scope(2, async move { assert_eq!(NUMBER.get(), 2); NUMBER.scope(3, async move { assert_eq!(NUMBER.get(), 3); }).await; }).await;
Methods
impl<T> LocalKey<T> where
T: 'static,
[src]
T: 'static,
pub async fn scope<F>(&'static self, value: T, f: F) -> <F as Future>::Output where
F: Future,
[src]
F: Future,
Sets a value T
as the task-local value for the future F
.
On completion of scope
, the task-local will be dropped.
Examples
tokio::task_local! { static NUMBER: u32; } NUMBER.scope(1, async move { println!("task local value: {}", NUMBER.get()); }).await;
pub fn with<F, R>(&'static self, f: F) -> R where
F: FnOnce(&T) -> R,
[src]
F: FnOnce(&T) -> R,
Accesses the current task-local and runs the provided closure.
Panics
This function will panic if not called within the context of a future containing a task-local with the corresponding key.
pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError> where
F: FnOnce(&T) -> R,
[src]
F: FnOnce(&T) -> R,
Accesses the current task-local and runs the provided closure.
If the task-local with the accociated key is not present, this
method will return an AccessError
. For a panicking variant,
see with
.
impl<T> LocalKey<T> where
T: 'static + Copy,
[src]
T: 'static + Copy,
pub fn get(&'static self) -> T
[src]
Returns a copy of the task-local value
if the task-local value implements Copy
.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for LocalKey<T>
impl<T> Send for LocalKey<T>
impl<T> Sync for LocalKey<T>
impl<T> Unpin for LocalKey<T>
impl<T> UnwindSafe for LocalKey<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T, I> AsResult<T, I> for T where
I: Input,
[src]
I: Input,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoCollection<T> for T
[src]
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
[src]
A: Array<Item = T>,
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
A: Array<Item = U>,
F: FnMut(T) -> U,
[src]
A: Array<Item = U>,
F: FnMut(T) -> U,
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Typeable for T where
T: Any,
[src]
T: Any,