vm-migration: Introduce new constructor for Snapshot

This simplifies the Snapshot creation as we expect a SnapshotData to be
provided most of the time.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2022-12-02 16:03:50 +01:00
parent 4ae6b595d7
commit 3931b99d4e
5 changed files with 20 additions and 30 deletions

View File

@@ -148,15 +148,19 @@ pub struct Snapshot {
}
impl Snapshot {
pub fn from_data(data: SnapshotData) -> Self {
Snapshot {
snapshot_data: Some(data),
..Default::default()
}
}
/// Create from state that can be serialized
pub fn new_from_state<T>(state: &T) -> Result<Self, MigratableError>
where
T: Serialize,
{
let mut snapshot_data = Snapshot::default();
snapshot_data.add_data(SnapshotData::new_from_state(state)?);
Ok(snapshot_data)
Ok(Snapshot::from_data(SnapshotData::new_from_state(state)?))
}
/// Create from versioned state
@@ -164,10 +168,9 @@ impl Snapshot {
where
T: Versionize + VersionMapped,
{
let mut snapshot_data = Snapshot::default();
snapshot_data.add_data(SnapshotData::new_from_versioned_state(state)?);
Ok(snapshot_data)
Ok(Snapshot::from_data(SnapshotData::new_from_versioned_state(
state,
)?))
}
/// Add a sub-component's Snapshot to the Snapshot.
@@ -175,11 +178,6 @@ impl Snapshot {
self.snapshots.insert(id, snapshot);
}
/// Add a SnapshotData to the component snapshot data.
pub fn add_data(&mut self, section: SnapshotData) {
self.snapshot_data = Some(section);
}
/// Generate the state data from the snapshot
pub fn to_state<'a, T>(&'a self) -> Result<T, MigratableError>
where