pub trait PropertyPersistence:
Send
+ Sync
+ 'static {
type Value: Clone + Send + Sync + 'static;
// Required methods
fn load(&self) -> Result<Self::Value, Box<dyn Error + Send + Sync>>;
fn save(
&self,
value: &Self::Value,
) -> Result<(), Box<dyn Error + Send + Sync>>;
}Expand description
Trait for implementing property value persistence
This trait allows custom persistence strategies for ObservableProperty values, enabling automatic save/load functionality to various storage backends like disk files, databases, cloud storage, etc.
§Examples
use observable_property::PropertyPersistence;
use std::fs;
struct FilePersistence {
path: String,
}
impl PropertyPersistence for FilePersistence {
type Value = String;
fn load(&self) -> Result<Self::Value, Box<dyn std::error::Error + Send + Sync>> {
fs::read_to_string(&self.path)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
fn save(&self, value: &Self::Value) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
fs::write(&self.path, value)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
}