Originator backup: '1'
Originator backup: '2'
Restored to state: 2
Restored to state: 1
Serde serialization framework
A common way to make a structure serializable is to derive Serialize and Deserialize traits from the serde serialization framework. Then an object of serializable type can be converted to many different formats, e.g. JSON with serde_json crate.
use serde::{Deserialize, Serialize};
/// An object to be stored. It derives a default
/// `Serialize` and `Deserialize` trait implementation, which
/// allows to convert it into many different formats (e.g. JSON).
#[derive(Serialize, Deserialize)]
struct Originator {
state: u32,
}
impl Originator {
/// Serializes an originator into a string of JSON format.
pub fn save(&self) -> String {
serde_json::to_string(self).unwrap()
}
/// Deserializes an originator into a string of JSON format.
pub fn restore(json: &str) -> Self {
serde_json::from_str(json).unwrap()
}
}
fn main() {
// A stack of mementos.
let mut history = Vec::<String>::new();
let mut originator = Originator { state: 0 };
originator.state = 1;
history.push(originator.save());
originator.state = 2;
history.push(originator.save());
for moment in history.iter() {
println!("{}", moment);
}
let originator = Originator::restore(&history.pop().unwrap());
println!("Restored to state: {}", originator.state);
let originator = Originator::restore(&history.pop().unwrap());
println!("Restored to state: {}", originator.state);
}
Output
{"state":1}
{"state":2}
Restored to state: 2
Restored to state: 1