diff --git a/Cargo.lock b/Cargo.lock index 2b14e63ad5..7b23b0acf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2594,6 +2594,8 @@ dependencies = [ "nix 0.28.0", "pkg-config", "remain", + "serde", + "serde_json", "thiserror", "winapi", "zerocopy", diff --git a/rutabaga_gfx/Cargo.toml b/rutabaga_gfx/Cargo.toml index 78ae2bcb0c..944dfd361c 100644 --- a/rutabaga_gfx/Cargo.toml +++ b/rutabaga_gfx/Cargo.toml @@ -20,6 +20,8 @@ x = [] cfg-if = "1.0.0" libc = "0.2.116" remain = "0.2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" thiserror = "1.0.23" zerocopy = { version = "0.7", features = ["derive"] } diff --git a/rutabaga_gfx/src/rutabaga_core.rs b/rutabaga_gfx/src/rutabaga_core.rs index 2cf2b1f5da..ebe3aa84e7 100644 --- a/rutabaga_gfx/src/rutabaga_core.rs +++ b/rutabaga_gfx/src/rutabaga_core.rs @@ -416,7 +416,7 @@ impl Rutabaga { .collect::>()?, }; - return snapshot.serialize_to(w).map_err(RutabagaError::IoError); + serde_json::to_writer(w, &snapshot).map_err(|e| RutabagaError::IoError(e.into())) } else { Err(RutabagaError::Unsupported) } @@ -452,7 +452,8 @@ impl Rutabaga { component.restore(directory) } else if self.default_component == RutabagaComponentType::Rutabaga2D { - let snapshot = RutabagaSnapshot::deserialize_from(r).map_err(RutabagaError::IoError)?; + let snapshot: RutabagaSnapshot = + serde_json::from_reader(r).map_err(|e| RutabagaError::IoError(e.into()))?; self.resources = snapshot .resources diff --git a/rutabaga_gfx/src/rutabaga_snapshot.rs b/rutabaga_gfx/src/rutabaga_snapshot.rs index 355ff44c6d..45f35d2636 100644 --- a/rutabaga_gfx/src/rutabaga_snapshot.rs +++ b/rutabaga_gfx/src/rutabaga_snapshot.rs @@ -3,71 +3,18 @@ // found in the LICENSE file. use std::collections::BTreeMap; -use std::io::Read; -use std::io::Write; -use zerocopy::AsBytes; -use zerocopy::FromBytes; +use serde::Deserialize; +use serde::Serialize; +#[derive(Serialize, Deserialize)] pub struct RutabagaSnapshot { pub resources: BTreeMap, } +#[derive(Serialize, Deserialize)] pub struct RutabagaResourceSnapshot { pub resource_id: u32, pub width: u32, pub height: u32, } - -impl RutabagaSnapshot { - // To avoid adding a build dependency, we use a custom serialization format. It is an internal - // detail, doesn't need to support host migration (e.g. we don't need to care about endianess - // or integer sizes), and isn't expected to be stable across releases. - pub fn serialize_to(&self, w: &mut impl Write) -> std::io::Result<()> { - fn write(w: &mut impl Write, v: impl AsBytes) -> std::io::Result<()> { - w.write_all(v.as_bytes()) - } - - write(w, self.resources.len())?; - for (id, resource) in self.resources.iter() { - assert_eq!(*id, resource.resource_id); - write(w, resource.resource_id)?; - write(w, resource.width)?; - write(w, resource.height)?; - } - - Ok(()) - } - - pub fn deserialize_from(r: &mut impl Read) -> std::io::Result { - fn read(r: &mut impl Read) -> std::io::Result { - let mut v: T = Default::default(); - r.read_exact(v.as_bytes_mut())?; - Ok(v) - } - - let num_resources: usize = read::(r)?; - let mut resources = BTreeMap::new(); - for _ in 0..num_resources { - let resource_id = read(r)?; - let width = read(r)?; - let height = read(r)?; - resources.insert( - resource_id, - RutabagaResourceSnapshot { - resource_id, - width, - height, - }, - ); - } - - // Verify we have consumed the all the input by checking for EOF. - let mut buf = [0u8]; - if r.read(&mut buf)? != 0 { - return Err(std::io::ErrorKind::InvalidData.into()); - } - - Ok(RutabagaSnapshot { resources }) - } -}