Struct zng_app_context::AppLocal
source · pub struct AppLocal<T: Send + Sync + 'static> { /* private fields */ }
Expand description
An app local storage.
This is similar to std::thread::LocalKey
, but works across all threads of the app.
Use the app_local!
macro to declare a static variable in the same style as thread_local!
.
Note that in "multi_app"
builds the app local can only be used if an app is running in the thread,
if no app is running read and write will panic.
Implementations§
source§impl<T: Send + Sync + 'static> AppLocal<T>
impl<T: Send + Sync + 'static> AppLocal<T>
sourcepub fn read(&'static self) -> MappedRwLockReadGuard<'static, T>
pub fn read(&'static self) -> MappedRwLockReadGuard<'static, T>
Read lock the value associated with the current app.
Initializes the default value for the app if this is the first value access.
§Panics
Panics if no app is running in "multi_app"
builds.
sourcepub fn try_read(&'static self) -> Option<MappedRwLockReadGuard<'static, T>>
pub fn try_read(&'static self) -> Option<MappedRwLockReadGuard<'static, T>>
Try read lock the value associated with the current app.
Initializes the default value for the app if this is the first value access.
Returns None
if can’t acquire a read lock.
§Panics
Panics if no app is running in "multi_app"
builds.
sourcepub fn write(&'static self) -> MappedRwLockWriteGuard<'static, T>
pub fn write(&'static self) -> MappedRwLockWriteGuard<'static, T>
Write lock the value associated with the current app.
Initializes the default value for the app if this is the first value access.
§Panics
Panics if no app is running in "multi_app"
builds.
sourcepub fn try_write(&'static self) -> Option<MappedRwLockWriteGuard<'static, T>>
pub fn try_write(&'static self) -> Option<MappedRwLockWriteGuard<'static, T>>
Try to write lock the value associated with the current app.
Initializes the default value for the app if this is the first value access.
Returns None
if can’t acquire a write lock.
§Panics
Panics if no app is running in "multi_app"
builds.
sourcepub fn try_get(&'static self) -> Option<T>where
T: Clone,
pub fn try_get(&'static self) -> Option<T>where
T: Clone,
Try to get a clone of the value.
Returns None
if can’t acquire a read lock.
sourcepub fn try_set(&'static self, value: T) -> Result<(), T>
pub fn try_set(&'static self, value: T) -> Result<(), T>
Try to set the value.
Returns Err(value)
if can’t acquire a write lock.
sourcepub fn read_map<O>(
&'static self,
map: impl FnOnce(&T) -> &O,
) -> MappedRwLockReadGuard<'static, O>
pub fn read_map<O>( &'static self, map: impl FnOnce(&T) -> &O, ) -> MappedRwLockReadGuard<'static, O>
Create a read lock and map
it to a sub-value.
sourcepub fn try_read_map<O>(
&'static self,
map: impl FnOnce(&T) -> &O,
) -> Option<MappedRwLockReadGuard<'static, O>>
pub fn try_read_map<O>( &'static self, map: impl FnOnce(&T) -> &O, ) -> Option<MappedRwLockReadGuard<'static, O>>
Try to create a read lock and map
it to a sub-value.
sourcepub fn write_map<O>(
&'static self,
map: impl FnOnce(&mut T) -> &mut O,
) -> MappedRwLockWriteGuard<'static, O>
pub fn write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O, ) -> MappedRwLockWriteGuard<'static, O>
Create a write lock and map
it to a sub-value.
sourcepub fn try_write_map<O>(
&'static self,
map: impl FnOnce(&mut T) -> &mut O,
) -> Option<MappedRwLockWriteGuard<'static, O>>
pub fn try_write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O, ) -> Option<MappedRwLockWriteGuard<'static, O>>
Try to create a write lock and map
it to a sub-value.
sourcepub fn id(&'static self) -> AppLocalId
pub fn id(&'static self) -> AppLocalId
Gets an ID for this local instance that is valid for the lifetime of the process.
Note that comparing two &'static 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.
Trait Implementations§
impl<T: Send + Sync + 'static> Eq for AppLocal<T>
Auto Trait Implementations§
impl<T> Freeze for AppLocal<T>
impl<T> RefUnwindSafe for AppLocal<T>
impl<T> Send for AppLocal<T>
impl<T> Sync for AppLocal<T>
impl<T> Unpin for AppLocal<T>
impl<T> UnwindSafe for AppLocal<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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§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