From 1967f738a8643a3b1977feea0057aa1ba7c246dc Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sat, 12 Oct 2019 14:17:52 -0700 Subject: [PATCH] Make all file contents be Arc> instead of &[u8] --- src/snapshot_middleware/csv.rs | 4 ++-- src/snapshot_middleware/json_model.rs | 2 +- src/snapshot_middleware/lua.rs | 4 ++-- src/snapshot_middleware/project.rs | 2 +- src/snapshot_middleware/rbxlx.rs | 2 +- src/snapshot_middleware/rbxm.rs | 2 +- src/snapshot_middleware/rbxmx.rs | 2 +- src/snapshot_middleware/txt.rs | 4 ++-- src/vfs/vfs.rs | 30 ++++++++++++++------------- 9 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/snapshot_middleware/csv.rs b/src/snapshot_middleware/csv.rs index ca2abbc4..08c2b755 100644 --- a/src/snapshot_middleware/csv.rs +++ b/src/snapshot_middleware/csv.rs @@ -37,7 +37,7 @@ impl SnapshotMiddleware for SnapshotCsv { .path() .with_file_name(format!("{}.meta.json", instance_name)); - let table_contents = convert_localization_csv(entry.contents(vfs)?); + let table_contents = convert_localization_csv(&entry.contents(vfs)?); let mut snapshot = InstanceSnapshot { snapshot_id: None, @@ -58,7 +58,7 @@ impl SnapshotMiddleware for SnapshotCsv { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; - let mut metadata = AdjacentMetadata::from_slice(meta_contents); + let mut metadata = AdjacentMetadata::from_slice(&meta_contents); metadata.apply_all(&mut snapshot); } diff --git a/src/snapshot_middleware/json_model.rs b/src/snapshot_middleware/json_model.rs index bab2c0c7..3ccde84c 100644 --- a/src/snapshot_middleware/json_model.rs +++ b/src/snapshot_middleware/json_model.rs @@ -33,7 +33,7 @@ impl SnapshotMiddleware for SnapshotJsonModel { }; let instance: JsonModel = - serde_json::from_slice(entry.contents(vfs)?).expect("TODO: Handle serde_json errors"); + serde_json::from_slice(&entry.contents(vfs)?).expect("TODO: Handle serde_json errors"); if let Some(json_name) = &instance.name { if json_name != &instance_name { diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index c058895e..8f3266d5 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -71,7 +71,7 @@ fn snapshot_lua_file( }; let contents = entry.contents(vfs)?; - let contents_str = str::from_utf8(contents) + let contents_str = str::from_utf8(&contents) // TODO: Turn into error type .expect("File content was not valid UTF-8") .to_string(); @@ -103,7 +103,7 @@ fn snapshot_lua_file( if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; - let mut metadata = AdjacentMetadata::from_slice(meta_contents); + let mut metadata = AdjacentMetadata::from_slice(&meta_contents); metadata.apply_all(&mut snapshot); } diff --git a/src/snapshot_middleware/project.rs b/src/snapshot_middleware/project.rs index 211ca611..a3050ff0 100644 --- a/src/snapshot_middleware/project.rs +++ b/src/snapshot_middleware/project.rs @@ -43,7 +43,7 @@ impl SnapshotMiddleware for SnapshotProject { return Ok(None); } - let project = Project::load_from_slice(entry.contents(vfs)?, entry.path()) + let project = Project::load_from_slice(&entry.contents(vfs)?, entry.path()) .map_err(|err| SnapshotError::malformed_project(err, entry.path()))?; // Snapshotting a project should always return an instance, so this diff --git a/src/snapshot_middleware/rbxlx.rs b/src/snapshot_middleware/rbxlx.rs index c758dd34..631afe22 100644 --- a/src/snapshot_middleware/rbxlx.rs +++ b/src/snapshot_middleware/rbxlx.rs @@ -31,7 +31,7 @@ impl SnapshotMiddleware for SnapshotRbxlx { let options = rbx_xml::DecodeOptions::new() .property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); - let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?, options) + let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?.as_slice(), options) .expect("TODO: Handle rbx_xml errors"); let root_id = temp_tree.get_root_id(); diff --git a/src/snapshot_middleware/rbxm.rs b/src/snapshot_middleware/rbxm.rs index 03bbc28d..8af33296 100644 --- a/src/snapshot_middleware/rbxm.rs +++ b/src/snapshot_middleware/rbxm.rs @@ -37,7 +37,7 @@ impl SnapshotMiddleware for SnapshotRbxm { }); let root_id = temp_tree.get_root_id(); - rbx_binary::decode(&mut temp_tree, root_id, entry.contents(vfs)?) + rbx_binary::decode(&mut temp_tree, root_id, entry.contents(vfs)?.as_slice()) .expect("TODO: Handle rbx_binary errors"); let root_instance = temp_tree.get_instance(root_id).unwrap(); diff --git a/src/snapshot_middleware/rbxmx.rs b/src/snapshot_middleware/rbxmx.rs index 8c3b142f..bd54160d 100644 --- a/src/snapshot_middleware/rbxmx.rs +++ b/src/snapshot_middleware/rbxmx.rs @@ -31,7 +31,7 @@ impl SnapshotMiddleware for SnapshotRbxmx { let options = rbx_xml::DecodeOptions::new() .property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); - let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?, options) + let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?.as_slice(), options) .expect("TODO: Handle rbx_xml errors"); let root_instance = temp_tree.get_instance(temp_tree.get_root_id()).unwrap(); diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index 6d34d3d1..0b79eaac 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -34,7 +34,7 @@ impl SnapshotMiddleware for SnapshotTxt { }; let contents = entry.contents(vfs)?; - let contents_str = str::from_utf8(contents) + let contents_str = str::from_utf8(&contents) .map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))? .to_string(); @@ -63,7 +63,7 @@ impl SnapshotMiddleware for SnapshotTxt { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; - let mut metadata = AdjacentMetadata::from_slice(meta_contents); + let mut metadata = AdjacentMetadata::from_slice(&meta_contents); metadata.apply_all(&mut snapshot); } diff --git a/src/vfs/vfs.rs b/src/vfs/vfs.rs index 355ce592..16131720 100644 --- a/src/vfs/vfs.rs +++ b/src/vfs/vfs.rs @@ -1,6 +1,7 @@ use std::{ io, path::{Path, PathBuf}, + sync::Arc, }; use crossbeam_channel::Receiver; @@ -143,7 +144,7 @@ impl Vfs { }) } - pub fn get_contents(&mut self, path: impl AsRef) -> FsResult<&[u8]> { + pub fn get_contents(&mut self, path: impl AsRef) -> FsResult>> { let path = path.as_ref(); self.read_if_not_exists(path)?; @@ -154,11 +155,12 @@ impl Vfs { file.contents = Some( self.fetcher .read_contents(path) + .map(Arc::new) .map_err(|err| FsError::new(err, path.to_path_buf()))?, ); } - Ok(file.contents.as_ref().unwrap()) + Ok(file.contents.clone().unwrap()) } VfsItem::Directory(_) => Err(FsError::new( io::Error::new(io::ErrorKind::Other, "Can't read a directory"), @@ -275,7 +277,7 @@ impl VfsDebug for Vfs { path.to_path_buf(), VfsItem::File(VfsFile { path: path.to_path_buf(), - contents: Some(file.contents), + contents: Some(Arc::new(file.contents)), }), ); } @@ -343,7 +345,7 @@ impl VfsEntry { &self.path } - pub fn contents<'vfs>(&self, vfs: &'vfs mut Vfs) -> FsResult<&'vfs [u8]> { + pub fn contents<'vfs>(&self, vfs: &'vfs mut Vfs) -> FsResult>> { vfs.get_contents(&self.path) } @@ -391,7 +393,7 @@ impl VfsItem { pub struct VfsFile { pub(super) path: PathBuf, - pub(super) contents: Option>, + pub(super) contents: Option>>, } pub struct VfsDirectory { @@ -417,8 +419,8 @@ mod test { vfs.debug_load_snapshot("/hello.txt", file); - let entry = vfs.get_contents("/hello.txt").unwrap(); - assert_eq!(entry, b"hello, world!"); + let contents = vfs.get_contents("/hello.txt").unwrap(); + assert_eq!(contents.as_slice(), b"hello, world!"); } #[test] @@ -449,11 +451,11 @@ mod test { assert!(has_a, "/dir/a.txt was missing"); assert!(has_b, "/dir/b.lua was missing"); - let a = vfs.get_contents("/dir/a.txt").unwrap(); - assert_eq!(a, b"contents of a.txt"); + let a_contents = vfs.get_contents("/dir/a.txt").unwrap(); + assert_eq!(a_contents.as_slice(), b"contents of a.txt"); - let b = vfs.get_contents("/dir/b.lua").unwrap(); - assert_eq!(b, b"contents of b.lua"); + let b_contents = vfs.get_contents("/dir/b.lua").unwrap(); + assert_eq!(b_contents.as_slice(), b"contents of b.lua"); } #[test] @@ -523,7 +525,7 @@ mod test { let contents = a.contents(&mut vfs).expect("mock file contents error"); - assert_eq!(contents, b"Initial contents"); + assert_eq!(contents.as_slice(), b"Initial contents"); { let mut mock_state = mock_state.borrow_mut(); @@ -535,7 +537,7 @@ mod test { let contents = a.contents(&mut vfs).expect("mock file contents error"); - assert_eq!(contents, b"Changed contents"); + assert_eq!(contents.as_slice(), b"Changed contents"); } #[test] @@ -551,7 +553,7 @@ mod test { .contents(&mut vfs) .expect("couldn't get hello.txt contents"); - assert_eq!(contents, b"hello, world!"); + assert_eq!(contents.as_slice(), b"hello, world!"); vfs.raise_file_removed("/hello.txt") .expect("error processing file removal");