From 9488b1b850152eb564dbfefc898c962bdac73eb4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 20 Jul 2021 20:21:56 +0200 Subject: Main context struct --- src/loading/fs.rs | 23 ++++++++++++----------- src/loading/mod.rs | 8 ++++---- 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'src/loading') diff --git a/src/loading/fs.rs b/src/loading/fs.rs index ea33016c..8785499e 100644 --- a/src/loading/fs.rs +++ b/src/loading/fs.rs @@ -1,3 +1,4 @@ +use std::cell::RefCell; use std::collections::HashMap; use std::fs::{self, File}; use std::io; @@ -18,23 +19,23 @@ use crate::util::PathExt; #[derive(Default, Debug, Clone)] pub struct FsLoader { faces: Vec, - paths: HashMap, + paths: RefCell>, } impl FsLoader { /// Create a new loader without any fonts. pub fn new() -> Self { - Self { faces: vec![], paths: HashMap::new() } + Self { faces: vec![], paths: RefCell::default() } } /// Resolve a file id for a path. - pub fn resolve_path(&mut self, path: &Path) -> io::Result { + pub fn resolve_path(&self, path: &Path) -> io::Result { let file = File::open(path)?; let meta = file.metadata()?; if meta.is_file() { let handle = Handle::from_file(file)?; let id = FileId(fxhash::hash64(&handle)); - self.paths.insert(id, path.normalize()); + self.paths.borrow_mut().insert(id, path.normalize()); Ok(id) } else { Err(io::Error::new(io::ErrorKind::Other, "not a file")) @@ -165,14 +166,13 @@ impl Loader for FsLoader { &self.faces } - fn resolve_from(&mut self, base: FileId, path: &Path) -> Option { - let dir = self.paths[&base].parent()?; - let full = dir.join(path); - self.resolve_path(&full).ok() + fn resolve_from(&self, base: FileId, path: &Path) -> Option { + let full = self.paths.borrow()[&base].parent()?.join(path); + self.resolve(&full).ok() } - fn load_file(&mut self, id: FileId) -> Option> { - fs::read(&self.paths[&id]).ok() + fn load_file(&self, id: FileId) -> Option> { + fs::read(&self.paths.borrow()[&id]).ok() } } @@ -185,7 +185,8 @@ mod tests { let mut loader = FsLoader::new(); loader.search_path("fonts"); - let mut paths: Vec<_> = loader.paths.values().collect(); + let map = loader.paths.borrow(); + let mut paths: Vec<_> = map.values().collect(); paths.sort(); assert_eq!(paths, [ diff --git a/src/loading/mod.rs b/src/loading/mod.rs index c2f7ca39..3be74428 100644 --- a/src/loading/mod.rs +++ b/src/loading/mod.rs @@ -21,10 +21,10 @@ pub trait Loader { /// /// This should return the same id for all paths pointing to the same file /// and `None` if the file does not exist. - fn resolve_from(&mut self, base: FileId, path: &Path) -> Option; + fn resolve_from(&self, base: FileId, path: &Path) -> Option; /// Load a file by id. - fn load_file(&mut self, id: FileId) -> Option>; + fn load_file(&self, id: FileId) -> Option>; } /// A file id that can be [resolved](Loader::resolve_from) from a path. @@ -53,11 +53,11 @@ impl Loader for BlankLoader { &[] } - fn resolve_from(&mut self, _: FileId, _: &Path) -> Option { + fn resolve_from(&self, _: FileId, _: &Path) -> Option { None } - fn load_file(&mut self, _: FileId) -> Option> { + fn load_file(&self, _: FileId) -> Option> { None } } -- cgit v1.2.3