diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-01 12:46:01 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-01 12:55:07 +0200 |
| commit | 7218892c722ca583297c0ebbda350bdf6f16d3ce (patch) | |
| tree | 27ebbfaf0662c1e0dd01e7c5e9e360ab288cae4d /src/loading/fs.rs | |
| parent | 9bdb0bdeffa5e4b6da9e3f6d3c1b79c506005fc5 (diff) | |
Refactor path handling
Diffstat (limited to 'src/loading/fs.rs')
| -rw-r--r-- | src/loading/fs.rs | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/src/loading/fs.rs b/src/loading/fs.rs index ac3f3706..7fa1c120 100644 --- a/src/loading/fs.rs +++ b/src/loading/fs.rs @@ -24,8 +24,7 @@ pub struct FsLoader { cache: FileCache, } -/// Maps from paths to loaded file buffers. When the buffer is `None` the file -/// does not exist or couldn't be read. +/// Maps from resolved file hashes to loaded file buffers. type FileCache = HashMap<FileHash, Buffer>; impl FsLoader { @@ -169,38 +168,29 @@ impl Loader for FsLoader { } fn load_face(&mut self, idx: usize) -> Option<Buffer> { - load(&mut self.cache, &self.files[idx]) + self.load_file(&self.files[idx].clone()) } fn load_file(&mut self, path: &Path) -> Option<Buffer> { - load(&mut self.cache, path) + let hash = self.resolve(path)?; + Some(Rc::clone(match self.cache.entry(hash) { + Entry::Occupied(entry) => entry.into_mut(), + Entry::Vacant(entry) => { + let buffer = std::fs::read(path).ok()?; + entry.insert(Rc::new(buffer)) + } + })) } fn resolve(&self, path: &Path) -> Option<FileHash> { - hash(path) - } -} - -/// Load from the file system using a cache. -fn load(cache: &mut FileCache, path: &Path) -> Option<Buffer> { - Some(match cache.entry(hash(path)?) { - Entry::Occupied(entry) => entry.get().clone(), - Entry::Vacant(entry) => { - let buffer = std::fs::read(path).ok()?; - entry.insert(Rc::new(buffer)).clone() + let file = File::open(path).ok()?; + let meta = file.metadata().ok()?; + if meta.is_file() { + let handle = Handle::from_file(file).ok()?; + Some(FileHash::from_raw(fxhash::hash64(&handle))) + } else { + None } - }) -} - -/// Create a hash that is the same for all paths pointing to the same file. -fn hash(path: &Path) -> Option<FileHash> { - let file = File::open(path).ok()?; - let meta = file.metadata().ok()?; - if meta.is_file() { - let handle = Handle::from_file(file).ok()?; - Some(FileHash(fxhash::hash64(&handle))) - } else { - None } } |
