use std::any::{Any, TypeId};
use std::borrow::Borrow;
use std::cell::RefCell;
+use std::cmp::{Ord, Ordering, PartialOrd};
use std::collections::HashMap;
use std::convert::AsRef;
use std::ffi::OsStr;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
-use std::cmp::{PartialOrd, Ord, Ordering};
use lazy_static::lazy_static;
impl PartialEq<str> for Interned<String> {
fn eq(&self, other: &str) -> bool {
- *self == other
+ *self == other
}
}
impl<'a> PartialEq<&'a str> for Interned<String> {
impl<T: Hash + Clone + Eq> Default for TyIntern<T> {
fn default() -> Self {
- TyIntern {
- items: Vec::new(),
- set: Default::default(),
- }
+ TyIntern { items: Vec::new(), set: Default::default() }
}
}
impl<T: Hash + Clone + Eq> TyIntern<T> {
fn intern_borrow<B>(&mut self, item: &B) -> Interned<T>
where
- B: Eq + Hash + ToOwned<Owned=T> + ?Sized,
+ B: Eq + Hash + ToOwned<Owned = T> + ?Sized,
T: Borrow<B>,
{
if let Some(i) = self.set.get(&item) {
return *i;
}
let item = item.to_owned();
- let interned = Interned(self.items.len(), PhantomData::<*const T>);
+ let interned = Interned(self.items.len(), PhantomData::<*const T>);
self.set.insert(item.clone(), interned);
self.items.push(item);
interned
if let Some(i) = self.set.get(&item) {
return *i;
}
- let interned = Interned(self.items.len(), PhantomData::<*const T>);
+ let interned = Interned(self.items.len(), PhantomData::<*const T>);
self.set.insert(item.clone(), interned);
self.items.push(item);
interned
/// `get()` method.
#[derive(Debug)]
pub struct Cache(
- RefCell<HashMap<
- TypeId,
- Box<dyn Any>, // actually a HashMap<Step, Interned<Step::Output>>
- >>
+ RefCell<
+ HashMap<
+ TypeId,
+ Box<dyn Any>, // actually a HashMap<Step, Interned<Step::Output>>
+ >,
+ >,
);
impl Cache {
pub fn put<S: Step>(&self, step: S, value: S::Output) {
let mut cache = self.0.borrow_mut();
let type_id = TypeId::of::<S>();
- let stepcache = cache.entry(type_id)
- .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
- .downcast_mut::<HashMap<S, S::Output>>()
- .expect("invalid type mapped");
+ let stepcache = cache
+ .entry(type_id)
+ .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
+ .downcast_mut::<HashMap<S, S::Output>>()
+ .expect("invalid type mapped");
assert!(!stepcache.contains_key(&step), "processing {:?} a second time", step);
stepcache.insert(step, value);
}
pub fn get<S: Step>(&self, step: &S) -> Option<S::Output> {
let mut cache = self.0.borrow_mut();
let type_id = TypeId::of::<S>();
- let stepcache = cache.entry(type_id)
- .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
- .downcast_mut::<HashMap<S, S::Output>>()
- .expect("invalid type mapped");
+ let stepcache = cache
+ .entry(type_id)
+ .or_insert_with(|| Box::new(HashMap::<S, S::Output>::new()))
+ .downcast_mut::<HashMap<S, S::Output>>()
+ .expect("invalid type mapped");
stepcache.get(step).cloned()
}
}
pub fn all<S: Ord + Copy + Step>(&mut self) -> Vec<(S, S::Output)> {
let cache = self.0.get_mut();
let type_id = TypeId::of::<S>();
- let mut v = cache.remove(&type_id)
+ let mut v = cache
+ .remove(&type_id)
.map(|b| b.downcast::<HashMap<S, S::Output>>().expect("correct type"))
.map(|m| m.into_iter().collect::<Vec<_>>())
.unwrap_or_default();