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