Skip to main content

LocalItem

Struct LocalItem 

Source
pub struct LocalItem<T> { /* private fields */ }
Expand description

A scope-local item.

Implementations§

Source§

impl<T: Send + Sync + 'static> LocalItem<T>

Source

pub fn with<R>(&self, operation: impl for<'access> FnOnce(&'access T) -> R) -> R

Runs operation with the value selected by the current active scope.

The higher-ranked closure prevents a reference into per-CPU-selected storage from escaping after preemption is re-enabled. The first global access initializes the global scope before entering the pinned access. Concurrent first access waits for that initialization to be published.

This entry is intended for task context. Callers that already hold an IRQ or preemption guard should use Self::with_pinned to avoid a context transition on return. operation must not block, sleep, yield, or retain another context-aware guard; clone an owned handle and perform potentially blocking work after this method returns instead.

use scope_local::scope_local;

scope_local! {
    static VALUE: usize = 1;
}

let escaped: &'static usize = VALUE.with(|value| value);
Source

pub fn with_pinned<'pin, R>( &self, pin: &CpuPin<'pin>, operation: impl for<'access> FnOnce(&'access T) -> R, ) -> R

Runs operation with the current value under an existing CPU pin.

It never enters or leaves preemption state itself. The selected global scope must already have been initialized by Self::with; explicit Scope values are initialized eagerly. The caller remains responsible for making operation valid in the context represented by pin.

Source

pub fn try_with_pinned<'pin, R>( &self, pin: &CpuPin<'pin>, operation: impl for<'access> FnOnce(&'access T) -> R, ) -> Option<R>

Runs operation under an existing CPU pin without lazy initialization.

Returns None when the global scope has not been initialized. This path performs no allocation, lock acquisition, context transition, or user callback other than operation, making it suitable for a caller holding an IRQ-derived pin when that operation is itself hard-IRQ-safe.

Source

pub fn clone_current(&self) -> T
where T: Clone,

Clones the value selected by the current active scope.

This is the preferred entry for Arc-backed lock owners: the CPU pin is released before the returned owner is locked or used by potentially blocking code.

Source

pub fn scope<'scope>(&self, scope: &'scope Scope) -> ScopeItem<'scope, T>

Returns a reference to this item within the given scope.

Source

pub fn scope_mut<'scope>( &self, scope: &'scope mut Scope, ) -> ScopeItemMut<'scope, T>

Returns a mutable reference to this item within the given scope.

Source

pub fn scope_cell<'scope>( &self, scope: &'scope ScopeCellReadGuard<'_>, ) -> &'scope T

Returns the value selected through an existing ScopeCell read capability.

This path reuses the guard’s shared count. It never recursively acquires the underlying gate, so a writer that has already published upgrade intent cannot deadlock the current reader.

Source

pub fn scope_cell_mut<'scope>( &self, scope: &'scope mut ScopeCellWriteGuard<'_>, ) -> ScopeItemMut<'scope, T>

Returns mutable access to this item under a ScopeCell writer guard.

Unlike Self::scope_mut, this path never creates &mut Scope; the guard authorizes slot-level interior mutation while other CPUs may still retain the stable active-scope identity.

Auto Trait Implementations§

§

impl<T> Freeze for LocalItem<T>
where PhantomData<T>: Freeze,

§

impl<T> RefUnwindSafe for LocalItem<T>

§

impl<T> Send for LocalItem<T>
where PhantomData<T>: Send,

§

impl<T> Sync for LocalItem<T>
where PhantomData<T>: Sync,

§

impl<T> Unpin for LocalItem<T>
where PhantomData<T>: Unpin,

§

impl<T> UnsafeUnpin for LocalItem<T>

§

impl<T> UnwindSafe for LocalItem<T>

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, 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.