summaryrefslogtreecommitdiff
path: root/src/loading
diff options
context:
space:
mode:
Diffstat (limited to 'src/loading')
-rw-r--r--src/loading/fs.rs23
-rw-r--r--src/loading/mod.rs8
2 files changed, 16 insertions, 15 deletions
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<FaceInfo>,
- paths: HashMap<FileId, PathBuf>,
+ paths: RefCell<HashMap<FileId, PathBuf>>,
}
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<FileId> {
+ pub fn resolve_path(&self, path: &Path) -> io::Result<FileId> {
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<FileId> {
- 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<FileId> {
+ let full = self.paths.borrow()[&base].parent()?.join(path);
+ self.resolve(&full).ok()
}
- fn load_file(&mut self, id: FileId) -> Option<Vec<u8>> {
- fs::read(&self.paths[&id]).ok()
+ fn load_file(&self, id: FileId) -> Option<Vec<u8>> {
+ 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<FileId>;
+ fn resolve_from(&self, base: FileId, path: &Path) -> Option<FileId>;
/// Load a file by id.
- fn load_file(&mut self, id: FileId) -> Option<Vec<u8>>;
+ fn load_file(&self, id: FileId) -> Option<Vec<u8>>;
}
/// 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<FileId> {
+ fn resolve_from(&self, _: FileId, _: &Path) -> Option<FileId> {
None
}
- fn load_file(&mut self, _: FileId) -> Option<Vec<u8>> {
+ fn load_file(&self, _: FileId) -> Option<Vec<u8>> {
None
}
}