//! Phase memory module. use std::collections::HashMap; use std::hash::Hash; use gfx; /// Result of memory lookups. If it's a batch constructing error, /// we still need to memorize it in order to avoid repeating the /// error next time. pub type MemResult = Result; /// A generic phase memory type. pub trait Memory { /// Try looking up in the memory. fn lookup(&self, &T) -> Option>; /// Store the result into memory. fn store(&mut self, T, MemResult); /// Clear all the stored objects fn clear(&mut self); } impl Memory for () { fn lookup(&self, _: &T) -> Option> { None } fn store(&mut self, _: T, _: MemResult) {} fn clear(&mut self) {} } impl Memory for HashMap> { fn lookup(&self, input: &T) -> Option> { self.get(input).map(|r| r.clone()) } fn store(&mut self, input: T, out: MemResult) { self.insert(input, out); } fn clear(&mut self) { HashMap::clear(self); } }