pub trait DeviceStorageCompatible: Serialize + DeserializeOwned + Clone + PartialEq + Any + Send + Sync {
    const KEY: &'static str;

    // Required method
    fn default_value() -> Self;

    // Provided methods
    fn try_deserialize_from(value: &str) -> Result<Self, Error> { ... }
    fn extract(value: &str) -> Result<Self, Error> { ... }
    fn serialize_to(&self) -> String { ... }
}
Expand description

Structs that can be stored in device storage should derive the Serialize, Deserialize, and Clone traits, as well as provide constants. KEY should be unique the struct, usually the name of the struct itself. DEFAULT_VALUE will be the value returned when nothing has yet been stored.

Anything that implements this should not introduce breaking changes with the same key. Clients that want to make a breaking change should create a new structure with a new key and implement conversion/cleanup logic. Adding optional fields to a struct is not breaking, but removing fields, renaming fields, or adding non-optional fields are.

The Storage trait has Send and Sync requirements, so they have to be carried here as well. This was not necessary before because rust could determine the additional trait requirements at compile-time just for when the Storage trait was used. We don’t get that benefit anymore once we hide the type.

Required Associated Constants§

source

const KEY: &'static str

Required Methods§

source

fn default_value() -> Self

Provided Methods§

source

fn try_deserialize_from(value: &str) -> Result<Self, Error>

source

fn extract(value: &str) -> Result<Self, Error>

source

fn serialize_to(&self) -> String

Object Safety§

This trait is not object safe.

Implementors§