Skip to main content

shard_local

Macro shard_local 

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

Declares shard-local statics, each a ShardLocal made with the expression given on its first use on each worker.

use std::cell::Cell;

nuclei::shard_local! {
    static REQUESTS: Cell<u64> = Cell::new(0);
}

let rt = nuclei::Runtime::builder().worker_threads(1).build().unwrap();
let count = rt.block_on(rt.spawn(async {
    REQUESTS.with(|n| n.set(n.get() + 1));
    REQUESTS.with(Cell::get)
}));
assert_eq!(count, 1);