pub trait DeviceStorageConvertible: Sized {
type Storable: DeviceStorageCompatible + Into<Self>;
// Required method
fn get_storable(&self) -> Cow<'_, Self::Storable>;
}
Expand description
Types that can be converted into a storable type.
This trait represents types that can be converted into a storable type. It’s also important that the type it is transformed into can also be converted back into this type. This reverse conversion is used to populate the fields of the original type with the stored values plus defaulting the other fields that, e.g. might later be populated from hardware APIs.
§Example
// Struct used in controllers.
struct SomeSettingInfo {
storable_field: u8,
hardware_backed_field: String,
}
// Struct only used for storage.
#[derive(Serialize, Deserialize, PartialEq, Clone)]
struct StorableSomeSettingInfo {
storable_field: u8,
}
// Impl compatible for the storable type.
impl DeviceStorageCompatible for StorableSomeSettingInfo {
const KEY: &'static str = "some_setting_info";
fn default_value() -> Self {
Self { storable_field: 1, }
}
}
// Impl convertible for controller type.
impl DeviceStorageConvertible for SomeSettingInfo {
type Storable = StorableSomeSettingInfo;
fn get_storable(&self) -> Cow<'_, Self::Storable> {
Cow::Owned(Self {
storable_field: self.storable_field,
hardware_backed_field: String::new()
})
}
}
// This impl helps us convert from the storable version to the
// controller version of the struct. Hardware fields should be backed
// by default or usable values.
impl Into<SomeSettingInfo> for StorableSomeSettingInfo {
fn into(self) -> SomeSettingInfo {
SomeSettingInfo {
storable_field: self.storable_field,
hardware_backed_field: String::new(),
}
}
}
Required Associated Types§
Sourcetype Storable: DeviceStorageCompatible + Into<Self>
type Storable: DeviceStorageCompatible + Into<Self>
The type that will be used for storing the data.
Required Methods§
Sourcefn get_storable(&self) -> Cow<'_, Self::Storable>
fn get_storable(&self) -> Cow<'_, Self::Storable>
Convert self
into its storable version.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.