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 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 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 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 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 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 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 RwLock::try_write
for more details.