1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;
use std::rc::Rc;
use std::result;
use super::Val;
pub type Result<T> = result::Result<T, io::Error>;
pub trait Cache {
fn has_path(&self, path: &PathBuf) -> Result<bool>;
fn get(&self, path: &PathBuf) -> Result<Option<Rc<Val>>>;
fn stash(&mut self, path: PathBuf, asset: Rc<Val>) -> Result<()>;
}
pub struct MemoryCache {
map: HashMap<PathBuf, Rc<Val>>,
}
impl MemoryCache {
pub fn new() -> Self {
MemoryCache {
map: HashMap::new(),
}
}
}
impl Cache for MemoryCache {
fn has_path(&self, path: &PathBuf) -> Result<bool> {
let new_path = try!(path.canonicalize());
Ok(self.map.contains_key(&new_path))
}
fn get(&self, path: &PathBuf) -> Result<Option<Rc<Val>>> {
let new_path = try!(path.canonicalize());
Ok(self.map.get(&new_path).map(|v| v.clone()))
}
fn stash(&mut self, path: PathBuf, asset: Rc<Val>) -> Result<()> {
let new_path = try!(path.canonicalize());
self.map.insert(new_path, asset);
Ok(())
}
}