Merge branch 'vfs-arc'

This commit is contained in:
Lucien Greathouse
2019-10-12 15:29:54 -07:00
9 changed files with 27 additions and 25 deletions

View File

@@ -37,7 +37,7 @@ impl SnapshotMiddleware for SnapshotCsv {
.path() .path()
.with_file_name(format!("{}.meta.json", instance_name)); .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 { let mut snapshot = InstanceSnapshot {
snapshot_id: None, snapshot_id: None,
@@ -58,7 +58,7 @@ impl SnapshotMiddleware for SnapshotCsv {
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; 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); metadata.apply_all(&mut snapshot);
} }

View File

@@ -33,7 +33,7 @@ impl SnapshotMiddleware for SnapshotJsonModel {
}; };
let instance: JsonModel = 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 let Some(json_name) = &instance.name {
if json_name != &instance_name { if json_name != &instance_name {

View File

@@ -71,7 +71,7 @@ fn snapshot_lua_file<F: VfsFetcher>(
}; };
let contents = entry.contents(vfs)?; let contents = entry.contents(vfs)?;
let contents_str = str::from_utf8(contents) let contents_str = str::from_utf8(&contents)
// TODO: Turn into error type // TODO: Turn into error type
.expect("File content was not valid UTF-8") .expect("File content was not valid UTF-8")
.to_string(); .to_string();
@@ -103,7 +103,7 @@ fn snapshot_lua_file<F: VfsFetcher>(
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; 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); metadata.apply_all(&mut snapshot);
} }

View File

@@ -43,7 +43,7 @@ impl SnapshotMiddleware for SnapshotProject {
return Ok(None); 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()))?; .map_err(|err| SnapshotError::malformed_project(err, entry.path()))?;
// Snapshotting a project should always return an instance, so this // Snapshotting a project should always return an instance, so this

View File

@@ -31,7 +31,7 @@ impl SnapshotMiddleware for SnapshotRbxlx {
let options = rbx_xml::DecodeOptions::new() let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); .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"); .expect("TODO: Handle rbx_xml errors");
let root_id = temp_tree.get_root_id(); let root_id = temp_tree.get_root_id();

View File

@@ -37,7 +37,7 @@ impl SnapshotMiddleware for SnapshotRbxm {
}); });
let root_id = temp_tree.get_root_id(); 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"); .expect("TODO: Handle rbx_binary errors");
let root_instance = temp_tree.get_instance(root_id).unwrap(); let root_instance = temp_tree.get_instance(root_id).unwrap();

View File

@@ -31,7 +31,7 @@ impl SnapshotMiddleware for SnapshotRbxmx {
let options = rbx_xml::DecodeOptions::new() let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); .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"); .expect("TODO: Handle rbx_xml errors");
let root_instance = temp_tree.get_instance(temp_tree.get_root_id()).unwrap(); let root_instance = temp_tree.get_instance(temp_tree.get_root_id()).unwrap();

View File

@@ -34,7 +34,7 @@ impl SnapshotMiddleware for SnapshotTxt {
}; };
let contents = entry.contents(vfs)?; 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()))? .map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))?
.to_string(); .to_string();
@@ -63,7 +63,7 @@ impl SnapshotMiddleware for SnapshotTxt {
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; 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); metadata.apply_all(&mut snapshot);
} }

View File

@@ -1,6 +1,7 @@
use std::{ use std::{
io, io,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc,
}; };
use crossbeam_channel::Receiver; use crossbeam_channel::Receiver;
@@ -143,7 +144,7 @@ impl<F: VfsFetcher> Vfs<F> {
}) })
} }
pub fn get_contents(&mut self, path: impl AsRef<Path>) -> FsResult<&[u8]> { pub fn get_contents(&mut self, path: impl AsRef<Path>) -> FsResult<Arc<Vec<u8>>> {
let path = path.as_ref(); let path = path.as_ref();
self.read_if_not_exists(path)?; self.read_if_not_exists(path)?;
@@ -154,11 +155,12 @@ impl<F: VfsFetcher> Vfs<F> {
file.contents = Some( file.contents = Some(
self.fetcher self.fetcher
.read_contents(path) .read_contents(path)
.map(Arc::new)
.map_err(|err| FsError::new(err, path.to_path_buf()))?, .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( VfsItem::Directory(_) => Err(FsError::new(
io::Error::new(io::ErrorKind::Other, "Can't read a directory"), io::Error::new(io::ErrorKind::Other, "Can't read a directory"),
@@ -275,7 +277,7 @@ impl<F: VfsFetcher> VfsDebug for Vfs<F> {
path.to_path_buf(), path.to_path_buf(),
VfsItem::File(VfsFile { VfsItem::File(VfsFile {
path: path.to_path_buf(), path: path.to_path_buf(),
contents: Some(file.contents), contents: Some(Arc::new(file.contents)),
}), }),
); );
} }
@@ -343,7 +345,7 @@ impl VfsEntry {
&self.path &self.path
} }
pub fn contents<'vfs>(&self, vfs: &'vfs mut Vfs<impl VfsFetcher>) -> FsResult<&'vfs [u8]> { pub fn contents<'vfs>(&self, vfs: &'vfs mut Vfs<impl VfsFetcher>) -> FsResult<Arc<Vec<u8>>> {
vfs.get_contents(&self.path) vfs.get_contents(&self.path)
} }
@@ -391,7 +393,7 @@ impl VfsItem {
pub struct VfsFile { pub struct VfsFile {
pub(super) path: PathBuf, pub(super) path: PathBuf,
pub(super) contents: Option<Vec<u8>>, pub(super) contents: Option<Arc<Vec<u8>>>,
} }
pub struct VfsDirectory { pub struct VfsDirectory {
@@ -417,8 +419,8 @@ mod test {
vfs.debug_load_snapshot("/hello.txt", file); vfs.debug_load_snapshot("/hello.txt", file);
let entry = vfs.get_contents("/hello.txt").unwrap(); let contents = vfs.get_contents("/hello.txt").unwrap();
assert_eq!(entry, b"hello, world!"); assert_eq!(contents.as_slice(), b"hello, world!");
} }
#[test] #[test]
@@ -449,11 +451,11 @@ mod test {
assert!(has_a, "/dir/a.txt was missing"); assert!(has_a, "/dir/a.txt was missing");
assert!(has_b, "/dir/b.lua was missing"); assert!(has_b, "/dir/b.lua was missing");
let a = vfs.get_contents("/dir/a.txt").unwrap(); let a_contents = vfs.get_contents("/dir/a.txt").unwrap();
assert_eq!(a, b"contents of a.txt"); assert_eq!(a_contents.as_slice(), b"contents of a.txt");
let b = vfs.get_contents("/dir/b.lua").unwrap(); let b_contents = vfs.get_contents("/dir/b.lua").unwrap();
assert_eq!(b, b"contents of b.lua"); assert_eq!(b_contents.as_slice(), b"contents of b.lua");
} }
#[test] #[test]
@@ -523,7 +525,7 @@ mod test {
let contents = a.contents(&mut vfs).expect("mock file contents error"); 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(); 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"); 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] #[test]
@@ -551,7 +553,7 @@ mod test {
.contents(&mut vfs) .contents(&mut vfs)
.expect("couldn't get hello.txt contents"); .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") vfs.raise_file_removed("/hello.txt")
.expect("error processing file removal"); .expect("error processing file removal");