Skip to main content

PersistedValue

Struct PersistedValue 

Source
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>

Source

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.

Source

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.

Source

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)
Source

pub async fn set(&self, value: K::Value) -> Result<(), PersistedValueError>

Update (or create) this persisted value with a new value.

Source

pub async fn remove(&self) -> Result<(), PersistedValueError>

Delete this persisted value from storage.

Trait Implementations§

Source§

impl<K: PersistedValueKey> Clone for PersistedValue<K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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>
where Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: Send, PhantomData<K>: Send,

§

impl<K> Sync for PersistedValue<K>
where Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: Sync, PhantomData<K>: Sync,

§

impl<K> Unpin for PersistedValue<K>
where Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: Unpin, PhantomData<K>: Unpin,

§

impl<K> UnsafeUnpin for PersistedValue<K>
where Arc<dyn PersistedValueTrait<<K as PersistedValueKey>::Value>>: UnsafeUnpin, PhantomData<K>: UnsafeUnpin,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T> ⓘ
where T: Sized,

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T> ⓘ

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T> ⓘ

Applies the [Compat] adapter by mutable reference. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, UT> HandleAlloc<UT> for T
where T: Send + Sync,

§

fn new_handle(value: Arc<T>) -> Handle

Create a new handle for an Arc value Read more
§

unsafe fn clone_handle(handle: Handle) -> Handle

Clone a handle Read more
§

unsafe fn consume_handle(handle: Handle) -> Arc<T> ⓘ

Consume a handle, getting back the initial Arc<> Read more
§

unsafe fn get_arc(handle: Handle) -> Arc<Self> ⓘ

Get a clone of the Arc<> using a “borrowed” handle. Read more
§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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>,

Source§

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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

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