Struct zng::app::ContextLocal
source · pub struct ContextLocal<T>{ /* 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> ContextLocal<T>
impl<T> 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> ContextLocal<RwLock<RawRwLock, T>>
impl<T> ContextLocal<RwLock<RawRwLock, 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> ContextLocalKeyProvider for ContextLocal<T>
impl<T> 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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for 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> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§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