Skip to main content

ShardLocal

Struct ShardLocal 

Source
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>

Source

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.

Source

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.

Trait Implementations§

Source§

impl<T: Send + 'static> Debug for ShardLocal<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for ShardLocal<T>

§

impl<T> RefUnwindSafe for ShardLocal<T>
where fn() -> T: RefUnwindSafe,

§

impl<T> Send for ShardLocal<T>
where fn() -> T: Send,

§

impl<T> Sync for ShardLocal<T>
where fn() -> T: Sync,

§

impl<T> Unpin for ShardLocal<T>
where fn() -> T: Unpin,

§

impl<T> UnsafeUnpin for ShardLocal<T>
where fn() -> T: UnsafeUnpin,

§

impl<T> UnwindSafe for ShardLocal<T>
where fn() -> T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.