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>

source

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.

source

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.

source

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.

source

pub fn with_default<R>(&'static self, f: impl FnOnce() -> R) -> R

Calls f with no value loaded in context.

source

pub fn is_default(&'static self) -> bool

Gets if no value is set in the context.

source

pub fn get(&'static self) -> Arc<T>

Clone a reference to the current value in the context or the default value.

source

pub fn get_clone(&'static self) -> T
where T: Clone,

Clone the current value in the context or the default value.

source§

impl<T: Send + Sync + 'static> ContextLocal<RwLock<T>>

source

pub fn read_only(&'static self) -> ReadOnlyRwLock<T>

Gets a read-only shared reference to the current context value.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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>

source§

fn context_local_key(&'static self) -> AppLocalId

Gets the key.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more