summaryrefslogtreecommitdiff
path: root/src/image.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-05-29 15:45:57 +0200
committerLaurenz <laurmaedje@gmail.com>2021-05-31 22:33:40 +0200
commite023bf2ac9f5796355d9485afc16781196bf212b (patch)
tree26d4487de0c4e2d0f69182483301de867cb5fa34 /src/image.rs
parent9f77f09aacd1fb0fd6138a6d16ed2755f6bfae3f (diff)
Module loading system
Detects cyclic imports and loads each module only once per compilation.
Diffstat (limited to 'src/image.rs')
-rw-r--r--src/image.rs13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/image.rs b/src/image.rs
index bdfc19a6..3c5c8573 100644
--- a/src/image.rs
+++ b/src/image.rs
@@ -3,12 +3,13 @@
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::{self, Debug, Formatter};
use std::io::Cursor;
+use std::path::Path;
use image::io::Reader as ImageReader;
use image::{DynamicImage, GenericImageView, ImageFormat};
use serde::{Deserialize, Serialize};
-use crate::loading::Loader;
+use crate::loading::{FileHash, Loader};
/// A loaded image.
pub struct Image {
@@ -56,8 +57,8 @@ impl Debug for Image {
pub struct ImageCache {
/// Loaded images indexed by [`ImageId`].
images: Vec<Image>,
- /// Maps from paths to loaded images.
- paths: HashMap<String, ImageId>,
+ /// Maps from file hashes to ids of decoded images.
+ map: HashMap<FileHash, ImageId>,
/// Callback for loaded images.
on_load: Option<Box<dyn Fn(ImageId, &Image)>>,
}
@@ -67,14 +68,14 @@ impl ImageCache {
pub fn new() -> Self {
Self {
images: vec![],
- paths: HashMap::new(),
+ map: HashMap::new(),
on_load: None,
}
}
/// Load and decode an image file from a path.
- pub fn load(&mut self, loader: &mut dyn Loader, path: &str) -> Option<ImageId> {
- Some(match self.paths.entry(path.to_string()) {
+ pub fn load(&mut self, loader: &mut dyn Loader, path: &Path) -> Option<ImageId> {
+ Some(match self.map.entry(loader.resolve(path)?) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let buffer = loader.load_file(path)?;