pub struct LocalItem<T> { /* private fields */ }Expand description
A scope-local item.
Implementations§
Source§impl<T: Send + Sync + 'static> LocalItem<T>
impl<T: Send + Sync + 'static> LocalItem<T>
Sourcepub fn with<R>(&self, operation: impl for<'access> FnOnce(&'access T) -> R) -> R
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);Sourcepub fn with_pinned<'pin, R>(
&self,
pin: &CpuPin<'pin>,
operation: impl for<'access> FnOnce(&'access T) -> R,
) -> R
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.
Sourcepub fn try_with_pinned<'pin, R>(
&self,
pin: &CpuPin<'pin>,
operation: impl for<'access> FnOnce(&'access T) -> R,
) -> Option<R>
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.
Sourcepub fn clone_current(&self) -> Twhere
T: Clone,
pub fn clone_current(&self) -> Twhere
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.
Sourcepub fn scope<'scope>(&self, scope: &'scope Scope) -> ScopeItem<'scope, T>
pub fn scope<'scope>(&self, scope: &'scope Scope) -> ScopeItem<'scope, T>
Returns a reference to this item within the given scope.
Sourcepub fn scope_mut<'scope>(
&self,
scope: &'scope mut Scope,
) -> ScopeItemMut<'scope, T>
pub fn scope_mut<'scope>( &self, scope: &'scope mut Scope, ) -> ScopeItemMut<'scope, T>
Returns a mutable reference to this item within the given scope.
Sourcepub fn scope_cell<'scope>(
&self,
scope: &'scope ScopeCellReadGuard<'_>,
) -> &'scope T
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.
Sourcepub fn scope_cell_mut<'scope>(
&self,
scope: &'scope mut ScopeCellWriteGuard<'_>,
) -> ScopeItemMut<'scope, T>
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.