pub struct AppLocal<T>{ /* 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> AppLocal<T>
impl<T> AppLocal<T>
sourcepub fn read(&'static self) -> MappedRwLockReadGuard<'static, RawRwLock, T>
pub fn read(&'static self) -> MappedRwLockReadGuard<'static, RawRwLock, 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, RawRwLock, T>>
pub fn try_read( &'static self, ) -> Option<MappedRwLockReadGuard<'static, RawRwLock, 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, RawRwLock, T>
pub fn write(&'static self) -> MappedRwLockWriteGuard<'static, RawRwLock, 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, RawRwLock, T>>
pub fn try_write( &'static self, ) -> Option<MappedRwLockWriteGuard<'static, RawRwLock, 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, RawRwLock, O>
pub fn read_map<O>( &'static self, map: impl FnOnce(&T) -> &O, ) -> MappedRwLockReadGuard<'static, RawRwLock, 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, RawRwLock, O>>
pub fn try_read_map<O>( &'static self, map: impl FnOnce(&T) -> &O, ) -> Option<MappedRwLockReadGuard<'static, RawRwLock, 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, RawRwLock, O>
pub fn write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O, ) -> MappedRwLockWriteGuard<'static, RawRwLock, 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, RawRwLock, O>>
pub fn try_write_map<O>( &'static self, map: impl FnOnce(&mut T) -> &mut O, ) -> Option<MappedRwLockWriteGuard<'static, RawRwLock, 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> 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§
§impl<T> AnyEq for T
impl<T> AnyEq for T
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<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> 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