1 use std::any::{Any, TypeId};
2 use std::borrow::Borrow;
3 use std::cell::RefCell;
4 use std::cmp::{Ord, Ordering, PartialOrd};
5 use std::collections::HashMap;
6 use std::convert::AsRef;
9 use std::hash::{Hash, Hasher};
10 use std::marker::PhantomData;
13 use std::path::{Path, PathBuf};
16 use lazy_static::lazy_static;
18 use crate::builder::Step;
20 pub struct Interned<T>(usize, PhantomData<*const T>);
22 impl Default for Interned<String> {
23 fn default() -> Self {
24 INTERNER.intern_string(String::default())
28 impl Default for Interned<PathBuf> {
29 fn default() -> Self {
30 INTERNER.intern_path(PathBuf::default())
34 impl<T> Copy for Interned<T> {}
35 impl<T> Clone for Interned<T> {
36 fn clone(&self) -> Interned<T> {
41 impl<T> PartialEq for Interned<T> {
42 fn eq(&self, other: &Self) -> bool {
46 impl<T> Eq for Interned<T> {}
48 impl PartialEq<str> for Interned<String> {
49 fn eq(&self, other: &str) -> bool {
53 impl<'a> PartialEq<&'a str> for Interned<String> {
54 fn eq(&self, other: &&str) -> bool {
58 impl<'a, T> PartialEq<&'a Interned<T>> for Interned<T> {
59 fn eq(&self, other: &&Self) -> bool {
63 impl<'a, T> PartialEq<Interned<T>> for &'a Interned<T> {
64 fn eq(&self, other: &Interned<T>) -> bool {
69 unsafe impl<T> Send for Interned<T> {}
70 unsafe impl<T> Sync for Interned<T> {}
72 impl fmt::Display for Interned<String> {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 impl fmt::Debug for Interned<String> {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 f.write_fmt(format_args!("{:?}", s))
85 impl fmt::Debug for Interned<PathBuf> {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 let s: &Path = &*self;
88 f.write_fmt(format_args!("{:?}", s))
92 impl Hash for Interned<String> {
93 fn hash<H: Hasher>(&self, state: &mut H) {
94 let l = INTERNER.strs.lock().unwrap();
95 l.get(*self).hash(state)
99 impl Hash for Interned<PathBuf> {
100 fn hash<H: Hasher>(&self, state: &mut H) {
101 let l = INTERNER.paths.lock().unwrap();
102 l.get(*self).hash(state)
106 impl Deref for Interned<String> {
108 fn deref(&self) -> &'static str {
109 let l = INTERNER.strs.lock().unwrap();
110 unsafe { mem::transmute::<&str, &'static str>(l.get(*self)) }
114 impl Deref for Interned<PathBuf> {
116 fn deref(&self) -> &'static Path {
117 let l = INTERNER.paths.lock().unwrap();
118 unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) }
122 impl AsRef<Path> for Interned<PathBuf> {
123 fn as_ref(&self) -> &'static Path {
124 let l = INTERNER.paths.lock().unwrap();
125 unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) }
129 impl AsRef<Path> for Interned<String> {
130 fn as_ref(&self) -> &'static Path {
131 let l = INTERNER.strs.lock().unwrap();
132 unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self).as_ref()) }
136 impl AsRef<OsStr> for Interned<PathBuf> {
137 fn as_ref(&self) -> &'static OsStr {
138 let l = INTERNER.paths.lock().unwrap();
139 unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) }
143 impl AsRef<OsStr> for Interned<String> {
144 fn as_ref(&self) -> &'static OsStr {
145 let l = INTERNER.strs.lock().unwrap();
146 unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) }
150 impl PartialOrd<Interned<String>> for Interned<String> {
151 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
152 let l = INTERNER.strs.lock().unwrap();
153 l.get(*self).partial_cmp(l.get(*other))
157 impl Ord for Interned<String> {
158 fn cmp(&self, other: &Self) -> Ordering {
159 let l = INTERNER.strs.lock().unwrap();
160 l.get(*self).cmp(l.get(*other))
164 struct TyIntern<T: Clone + Eq> {
166 set: HashMap<T, Interned<T>>,
169 impl<T: Hash + Clone + Eq> Default for TyIntern<T> {
170 fn default() -> Self {
171 TyIntern { items: Vec::new(), set: Default::default() }
175 impl<T: Hash + Clone + Eq> TyIntern<T> {
176 fn intern_borrow<B>(&mut self, item: &B) -> Interned<T>
178 B: Eq + Hash + ToOwned<Owned = T> + ?Sized,
181 if let Some(i) = self.set.get(&item) {
184 let item = item.to_owned();
185 let interned = Interned(self.items.len(), PhantomData::<*const T>);
186 self.set.insert(item.clone(), interned);
187 self.items.push(item);
191 fn intern(&mut self, item: T) -> Interned<T> {
192 if let Some(i) = self.set.get(&item) {
195 let interned = Interned(self.items.len(), PhantomData::<*const T>);
196 self.set.insert(item.clone(), interned);
197 self.items.push(item);
201 fn get(&self, i: Interned<T>) -> &T {
207 pub struct Interner {
208 strs: Mutex<TyIntern<String>>,
209 paths: Mutex<TyIntern<PathBuf>>,
213 pub fn intern_str(&self, s: &str) -> Interned<String> {
214 self.strs.lock().unwrap().intern_borrow(s)
216 pub fn intern_string(&self, s: String) -> Interned<String> {
217 self.strs.lock().unwrap().intern(s)
220 pub fn intern_path(&self, s: PathBuf) -> Interned<PathBuf> {
221 self.paths.lock().unwrap().intern(s)
226 pub static ref INTERNER: Interner = Interner::default();
229 /// This is essentially a `HashMap` which allows storing any type in its input and
230 /// any type in its output. It is a write-once cache; values are never evicted,
231 /// which means that references to the value can safely be returned from the
238 Box<dyn Any>, // actually a HashMap<Step, Interned<Step::Output>>
244 pub fn new() -> Cache {
245 Cache(RefCell::new(HashMap::new()))
248 pub fn put<S: Step>(&self, step: S, value: S::Output) {
249 let mut cache = self.0.borrow_mut();
250 let type_id = TypeId::of::<S>();
251 let stepcache = cache
253 .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
254 .downcast_mut::<HashMap<S, S::Output>>()
255 .expect("invalid type mapped");
256 assert!(!stepcache.contains_key(&step), "processing {:?} a second time", step);
257 stepcache.insert(step, value);
260 pub fn get<S: Step>(&self, step: &S) -> Option<S::Output> {
261 let mut cache = self.0.borrow_mut();
262 let type_id = TypeId::of::<S>();
263 let stepcache = cache
265 .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
266 .downcast_mut::<HashMap<S, S::Output>>()
267 .expect("invalid type mapped");
268 stepcache.get(step).cloned()
274 pub fn all<S: Ord + Copy + Step>(&mut self) -> Vec<(S, S::Output)> {
275 let cache = self.0.get_mut();
276 let type_id = TypeId::of::<S>();
279 .map(|b| b.downcast::<HashMap<S, S::Output>>().expect("correct type"))
280 .map(|m| m.into_iter().collect::<Vec<_>>())
281 .unwrap_or_default();
282 v.sort_by_key(|&(a, _)| a);
286 pub fn contains<S: Step>(&self) -> bool {
287 self.0.borrow().contains_key(&TypeId::of::<S>())