Struct persisted::SingletonKey

source ·
pub struct SingletonKey<V> { /* private fields */ }
Expand description

A persisted key for a value type that appears only once in a program. The name of the value type is the only information available as the key, hence why the value type must only be used once.

§Example

use persisted::{Persisted, PersistedKey, PersistedStore, SingletonKey};

enum Foo {
    Bar,
    Baz,
}

#[derive(PersistedKey)]
#[persisted(Foo)]
struct FooKey;

// These two values are equivalent
let value1: Persisted<Store, _> =
    Persisted::new(SingletonKey::default(), Foo::Bar);
let value2: Persisted<Store, _> = Persisted::new(FooKey, Foo::Bar);


struct Store;

impl<K: PersistedKey> PersistedStore<K> for Store {
    fn load_persisted(key: &K) -> Option<K::Value> {
        None
    }
    fn store_persisted(key: &K, value: &K::Value) {}
}

Trait Implementations§

source§

impl<V: Clone> Clone for SingletonKey<V>

source§

fn clone(&self) -> SingletonKey<V>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<V> Debug for SingletonKey<V>

source§

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

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

impl<V> Default for SingletonKey<V>

source§

fn default() -> Self

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

impl<V> PersistedKey for SingletonKey<V>

§

type Value = V

The type of the persisted value associated with this key
source§

fn type_name() -> &'static str

Get a unique name for this key type. This should be globally unique within the scope of your program. This is important to use while persisting because most serialization formats don’t include the name of the type, just the content. This unique name allows the store to disambiguate between different key types of the same structure, which is particular important for unit keys. For example, if your store persists data as JSON, a serialized key may be just an ID, e.g. 3. This alone is not a useful key because it’s ambiguous in the scope of your entire program. This method allows you to include the key type, so you could serialize the same key as ["Person", 3] or {"type": "Person", "key": 3}. It’s up to the PersistedStore implementation to decide how to actually employ this function, it’s provided merely as a utility. Read more
source§

impl<V: Copy> Copy for SingletonKey<V>

Auto Trait Implementations§

§

impl<V> Freeze for SingletonKey<V>

§

impl<V> RefUnwindSafe for SingletonKey<V>
where V: RefUnwindSafe,

§

impl<V> Send for SingletonKey<V>
where V: Send,

§

impl<V> Sync for SingletonKey<V>
where V: Sync,

§

impl<V> Unpin for SingletonKey<V>
where V: Unpin,

§

impl<V> UnwindSafe for SingletonKey<V>
where V: UnwindSafe,

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.