pub struct PersistedValue<K: PersistedValueKey> {
backend: Arc<dyn PersistedValueTrait<K::Value>>,
_marker: PhantomData<K>,
}Expand description
A handle to a single persisted value in storage.
This type provides async methods to get, update, and delete the persisted value.
Obtained via StateClient::value(), or injected with #[derive(FromClient)].
Every failed operation is logged with tracing::error!, so callers that discard an error
don’t need to log it themselves. try_get finding no value is not a failure.
§Example
use bitwarden_state::register_persisted_value;
register_persisted_value!(THEME: String = "theme");
let value = client.platform().state().value::<THEME>();
// Get the current value
let current: Option<String> = value.try_get().await?;
// Update the value
value.set("dark".to_string()).await?;
// Delete the value
value.remove().await?;Fields§
§backend: Arc<dyn PersistedValueTrait<K::Value>>§_marker: PhantomData<K>Implementations§
Source§impl<K: PersistedValueKey> PersistedValue<K>
impl<K: PersistedValueKey> PersistedValue<K>
Sourcepub fn new(backend: Arc<dyn PersistedValueTrait<K::Value>>) -> Self
pub fn new(backend: Arc<dyn PersistedValueTrait<K::Value>>) -> Self
Create a new persisted value handle from a backend.
The backend is already bound to a single key, so it decides where the value is stored.
Sourcepub async fn get(&self) -> Result<K::Value, PersistedValueError>
pub async fn get(&self) -> Result<K::Value, PersistedValueError>
Get the current value, failing with PersistedValueError::ValueNotFound if it doesn’t
exist in storage. Use Self::try_get where absence is an expected outcome.
Sourcepub async fn try_get(&self) -> Result<Option<K::Value>, PersistedValueError>
pub async fn try_get(&self) -> Result<Option<K::Value>, PersistedValueError>
Get the current value.
Returns None if the value doesn’t exist in storage.
§Errors
Returns an error if deserialization fails, which may indicate:
- Schema evolution problems (type definition changed)
- Data corruption
- Type mismatch (two keys sharing a key name)
Sourcepub async fn set(&self, value: K::Value) -> Result<(), PersistedValueError>
pub async fn set(&self, value: K::Value) -> Result<(), PersistedValueError>
Update (or create) this persisted value with a new value.
Sourcepub async fn remove(&self) -> Result<(), PersistedValueError>
pub async fn remove(&self) -> Result<(), PersistedValueError>
Delete this persisted value from storage.
Trait Implementations§
Source§impl<K: PersistedValueKey> Clone for PersistedValue<K>
impl<K: PersistedValueKey> Clone for PersistedValue<K>
Auto Trait Implementations§
impl<K> !RefUnwindSafe for PersistedValue<K>
impl<K> !UnwindSafe for PersistedValue<K>
impl<K> Freeze for PersistedValue<K>where
Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: Freeze,
PhantomData<K>: Freeze,
impl<K> Send for PersistedValue<K>
impl<K> Sync for PersistedValue<K>
impl<K> Unpin for PersistedValue<K>
impl<K> UnsafeUnpin for PersistedValue<K>where
Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: UnsafeUnpin,
PhantomData<K>: UnsafeUnpin,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
§fn new_handle(value: Arc<T>) -> Handle
fn new_handle(value: Arc<T>) -> Handle
§unsafe fn clone_handle(handle: Handle) -> Handle
unsafe fn clone_handle(handle: Handle) -> Handle
§unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ
Arc<> Read more