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<'_, T>
pub fn read(&'static self) -> MappedRwLockReadGuard<'_, 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<'_, T>>
pub fn try_read(&'static self) -> Option<MappedRwLockReadGuard<'_, 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<'_, T>
pub fn write(&'static self) -> MappedRwLockWriteGuard<'_, 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<'_, T>>
pub fn try_write(&'static self) -> Option<MappedRwLockWriteGuard<'_, 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<'_, O>
pub fn read_map<O>( &'static self, map: impl FnOnce(&T) -> &O ) -> MappedRwLockReadGuard<'_, O>
Create a read lock and map
it to a sub-value.
sourcepub fn try_wread_map<O>(
&'static self,
map: impl FnOnce(&T) -> &O
) -> Option<MappedRwLockReadGuard<'_, O>>
pub fn try_wread_map<O>( &'static self, map: impl FnOnce(&T) -> &O ) -> Option<MappedRwLockReadGuard<'_, 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<'_, O>
pub fn write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O ) -> MappedRwLockWriteGuard<'_, 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<'_, O>>
pub fn try_write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O ) -> Option<MappedRwLockWriteGuard<'_, O>>
Try to create a write lock and map
it to a sub-value.