[][src]Macro async_std::task_local

macro_rules! task_local {
    () => { ... };
    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => { ... };
    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => { ... };
}

Declares task-local values.

The macro wraps any number of static declarations and makes them task-local. Attributes and visibility modifiers are allowed.

Each declared value is of the accessor type LocalKey.

Examples

use std::cell::Cell;

use async_std::task;
use async_std::prelude::*;

task_local! {
    static VAL: Cell<u32> = Cell::new(5);
}

task::block_on(async {
    let v = VAL.with(|c| c.get());
    assert_eq!(v, 5);
});