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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use any_cache::CacheKey;
use std::hash::{Hash, Hasher};
use std::fmt::{self, Display};
use std::marker::PhantomData;
use std::path::{Component, Path, PathBuf};
use crate::res::Res;
pub trait Key: 'static + Clone + Eq + Hash {
fn prepare_key(self, root: &Path) -> Self;
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum SimpleKey {
Path(PathBuf),
Logical(String),
}
impl SimpleKey {
pub fn from_path<P>(path: P) -> Self where P: AsRef<Path> {
SimpleKey::Path(path.as_ref().to_owned())
}
}
impl<'a> From<&'a Path> for SimpleKey {
fn from(path: &Path) -> Self {
SimpleKey::from_path(path)
}
}
impl From<PathBuf> for SimpleKey {
fn from(path: PathBuf) -> Self {
SimpleKey::Path(path)
}
}
impl Into<Option<PathBuf>> for SimpleKey {
fn into(self) -> Option<PathBuf> {
match self {
SimpleKey::Path(path) => Some(path),
_ => None
}
}
}
impl<'a> From<&'a str> for SimpleKey {
fn from(s: &str) -> Self {
SimpleKey::Logical(s.to_owned())
}
}
impl From<String> for SimpleKey {
fn from(s: String) -> Self {
SimpleKey::Logical(s)
}
}
impl Display for SimpleKey {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
SimpleKey::Path(ref path) => write!(f, "{}", path.display()),
SimpleKey::Logical(ref name) => write!(f, "{}", name)
}
}
}
impl Key for SimpleKey {
fn prepare_key(self, root: &Path) -> Self {
match self {
SimpleKey::Path(path) => SimpleKey::Path(vfs_substitute_path(&path, root)),
SimpleKey::Logical(x) => SimpleKey::Logical(x),
}
}
}
fn vfs_substitute_path(path: &Path, root: &Path) -> PathBuf {
let mut components = path.components().peekable();
let root_components = root.components();
match components.peek() {
Some(&Component::RootDir) => {
root_components.chain(components.skip(1)).collect()
}
_ => root_components.chain(components).collect(),
}
}
pub(crate) struct PrivateKey<K, T>(pub(crate) K, PhantomData<T>);
impl<K, T> PrivateKey<K, T> {
pub(crate) fn new(key: K) -> Self {
PrivateKey(key, PhantomData)
}
}
impl<K, T> Hash for PrivateKey<K, T> where K: Hash {
fn hash<H>(&self, state: &mut H) where H: Hasher {
self.0.hash(state)
}
}
impl<K, T> CacheKey for PrivateKey<K, T> where T: 'static, K: Key {
type Target = Res<T>;
}