pub trait DeviceStorageCompatible: Serialize + DeserializeOwned + Clone + PartialEq + Any + Send + Sync {
type Loader: DefaultDispatcher<Self>;
const KEY: &'static str;
// 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
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.