Struct zng_app_context::ContextLocal
source · pub struct ContextLocal<T: Send + Sync + 'static> { /* private fields */ }
Expand description
Represents an AppLocal<T>
value that can be temporarily overridden in a context.
The context works across threads, as long as the threads are instrumented using LocalContext
.
Use the context_local!
macro to declare a static variable in the same style as thread_local!
.
Implementations§
source§impl<T: Send + Sync + 'static> ContextLocal<T>
impl<T: Send + Sync + 'static> ContextLocal<T>
sourcepub fn id(&'static self) -> AppLocalId
pub fn id(&'static self) -> AppLocalId
Gets an ID for this context local instance that is valid for the lifetime of the process.
Note that comparing two &'static CTX_LOCAL
pointers is incorrect, because in "hot_reload"
builds the statics
can be different and still represent the same app local. This ID identifies the actual inner pointer.
sourcepub fn with_context<R>(
&'static self,
value: &mut Option<Arc<T>>,
f: impl FnOnce() -> R,
) -> R
pub fn with_context<R>( &'static self, value: &mut Option<Arc<T>>, f: impl FnOnce() -> R, ) -> R
Calls f
with the value
loaded in context.
The value
is moved into context, f
is called, then the value is moved back to value
.
§Panics
Panics if value
is None
.
sourcepub fn with_context_var<R>(
&'static self,
value: &mut Option<Arc<T>>,
f: impl FnOnce() -> R,
) -> R
pub fn with_context_var<R>( &'static self, value: &mut Option<Arc<T>>, f: impl FnOnce() -> R, ) -> R
Same as with_context
, but value
represents a variable.
Values loaded with this method are captured by CaptureFilter::ContextVars
.
sourcepub fn with_default<R>(&'static self, f: impl FnOnce() -> R) -> R
pub fn with_default<R>(&'static self, f: impl FnOnce() -> R) -> R
Calls f
with no value loaded in context.
sourcepub fn is_default(&'static self) -> bool
pub fn is_default(&'static self) -> bool
Gets if no value is set in the context.
source§impl<T: Send + Sync + 'static> ContextLocal<RwLock<T>>
impl<T: Send + Sync + 'static> ContextLocal<RwLock<T>>
sourcepub fn read_only(&'static self) -> ReadOnlyRwLock<T>
pub fn read_only(&'static self) -> ReadOnlyRwLock<T>
Gets a read-only shared reference to the current context value.
sourcepub fn read(&'static self) -> RwLockReadGuardOwned<T>
pub fn read(&'static self) -> RwLockReadGuardOwned<T>
Locks this RwLock
with shared read access, blocking the current thread until it can be acquired.
See parking_lot::RwLock::read
for more details.
sourcepub fn read_recursive(&'static self) -> RwLockReadGuardOwned<T>
pub fn read_recursive(&'static self) -> RwLockReadGuardOwned<T>
Locks this RwLock
with shared read access, blocking the current thread until it can be acquired.
Unlike read
, this method is guaranteed to succeed without blocking if
another read lock is held at the time of the call.
See parking_lot::RwLock::read
for more details.
sourcepub fn write(&'static self) -> RwLockWriteGuardOwned<T>
pub fn write(&'static self) -> RwLockWriteGuardOwned<T>
Locks this RwLock
with exclusive write access, blocking the current
thread until it can be acquired.
See parking_lot::RwLock::write
for more details.
sourcepub fn try_read(&'static self) -> Option<RwLockReadGuardOwned<T>>
pub fn try_read(&'static self) -> Option<RwLockReadGuardOwned<T>>
Try lock this RwLock
with shared read access, blocking the current thread until it can be acquired.
See parking_lot::RwLock::try_read
for more details.
sourcepub fn try_read_recursive(&'static self) -> Option<RwLockReadGuardOwned<T>>
pub fn try_read_recursive(&'static self) -> Option<RwLockReadGuardOwned<T>>
Locks this RwLock
with shared read access, blocking the current thread until it can be acquired.
See parking_lot::RwLock::try_read_recursive
for more details.
sourcepub fn try_write(&'static self) -> Option<RwLockWriteGuardOwned<T>>
pub fn try_write(&'static self) -> Option<RwLockWriteGuardOwned<T>>
Locks this RwLock
with exclusive write access, blocking the current
thread until it can be acquired.
See parking_lot::RwLock::try_write
for more details.
Trait Implementations§
source§impl<T: Send + Sync + 'static> ContextLocalKeyProvider for ContextLocal<T>
impl<T: Send + Sync + 'static> ContextLocalKeyProvider for ContextLocal<T>
source§fn context_local_key(&'static self) -> AppLocalId
fn context_local_key(&'static self) -> AppLocalId
Auto Trait Implementations§
impl<T> Freeze for ContextLocal<T>
impl<T> RefUnwindSafe for ContextLocal<T>
impl<T> Send for ContextLocal<T>
impl<T> Sync for ContextLocal<T>
impl<T> Unpin for ContextLocal<T>
impl<T> UnwindSafe for ContextLocal<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more