pub struct ShardLocal<T: Send + 'static> { /* private fields */ }Expand description
A value per worker shard, declared with
shard_local!.
Each worker of each runtime has its own value, made by the declaration’s
expression the first time code running on that worker uses it, and
dropped with the worker’s shard. Only code running on a worker (its
tasks, and block_on called from them) reaches a value: a thread’s own
shard in block_on, the blocking pool and any other thread have none.
A task spawned on a worker stays on that worker’s shard, and a handoff
moves the shard with its values, so the task always sees one value. A
task kept on a thread by spawn_local whose worker’s shard was handed
on runs on that thread’s own shard, off the worker: it gets
ShardAccessError there.
Implementations§
Source§impl<T: Send + 'static> ShardLocal<T>
impl<T: Send + 'static> ShardLocal<T>
Sourcepub fn with<R>(&'static self, f: impl FnOnce(&T) -> R) -> R
pub fn with<R>(&'static self, f: impl FnOnce(&T) -> R) -> R
Runs f on this worker’s value, made now if this is its first use
here.
While f runs the shard stays on this thread, even across a
block_on inside f: keep f short, as a blocking f holds the
whole worker up.
§Panics
Off a worker, or when the value’s own initializer uses it;
try_with answers the error instead.
Sourcepub fn try_with<R>(
&'static self,
f: impl FnOnce(&T) -> R,
) -> Result<R, ShardAccessError>
pub fn try_with<R>( &'static self, f: impl FnOnce(&T) -> R, ) -> Result<R, ShardAccessError>
Runs f on this worker’s value, as with does, or
answers a ShardAccessError off a worker or while the value’s own
initializer runs.