Struct Persisted

Source
pub struct Persisted<S, K>
where S: PersistedStore<K>, K: PersistedKey,
{ /* private fields */ }
Expand description

A wrapper that will automatically persist its contained value to the store. The value will be loaded from the store on creation, and saved on mutation.

§Generic Params

  • S: The backend type used to persist data. While we don’t need access to an instance of the backend, we do need to know its type so we can access its static functions on setup/mutation.
  • K: The type of the persistence key. The associated Value type will be the type of the contained value.

§Accessing

The inner value can be accessed immutably via Deref. To get mutable access, use Persisted::get_mut. This wrapper method returns a guard that implements DerefMut (similar to RefMut or MutexGuard from std, without the internal mutability). When your mutable access is complete, this wrapper will be dropped and the value, which presumably was changed, will be persisted to the store.

§Cloning

This type intentionally does not implement Clone. Cloning would result in two values with the same key. When the values are mutated, their persisted values would overwrite each other. It’s unlikely this is the desired behavior, and therefore is not provided.

Implementations§

Source§

impl<S, K> Persisted<S, K>
where S: PersistedStore<K>, K: PersistedKey,

Source

pub fn new(key: K, default: K::Value) -> Self

Initialize a new persisted value. The latest persisted value will be loaded from the store. If missing, use the given default instead.

Source

pub fn new_default(key: K) -> Self
where K::Value: Default,

Initialize a new persisted value. The latest persisted value will be loaded from the store. If missing, use the value type’s Default implementation instead.

Source

pub fn get_mut(&mut self) -> PersistedRefMut<'_, S, K>

Get a mutable reference to the value. This is wrapped by a guard, so that after mutation when the guard is dropped, the value can be saved.

Trait Implementations§

Source§

impl<S, K> Debug for Persisted<S, K>
where S: PersistedStore<K>, K: PersistedKey + Debug, K::Value: Debug,

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, K> Default for Persisted<S, K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S, K> Deref for Persisted<S, K>
where S: PersistedStore<K>, K: PersistedKey,

Source§

type Target = <K as PersistedKey>::Value

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S, K> Display for Persisted<S, K>

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, K> Freeze for Persisted<S, K>
where K: Freeze, <K as PersistedKey>::Value: Freeze,

§

impl<S, K> RefUnwindSafe for Persisted<S, K>

§

impl<S, K> Send for Persisted<S, K>
where K: Send, <K as PersistedKey>::Value: Send, S: Send,

§

impl<S, K> Sync for Persisted<S, K>
where K: Sync, <K as PersistedKey>::Value: Sync, S: Sync,

§

impl<S, K> Unpin for Persisted<S, K>
where K: Unpin, <K as PersistedKey>::Value: Unpin, S: Unpin,

§

impl<S, K> UnwindSafe for Persisted<S, K>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = Infallible

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.