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 associatedValuetype 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,
impl<S, K> Persisted<S, K>where
S: PersistedStore<K>,
K: PersistedKey,
Sourcepub fn new(key: K, default: K::Value) -> Self
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.
Sourcepub fn new_default(key: K) -> Self
pub fn new_default(key: K) -> Self
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.
Sourcepub fn get_mut(&mut self) -> PersistedRefMut<'_, S, K>
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.