Skip to content

Instantly share code, notes, and snippets.

@stevenpack
Created March 19, 2016 05:00
Show Gist options
  • Select an option

  • Save stevenpack/94361c40e421460b8b32 to your computer and use it in GitHub Desktop.

Select an option

Save stevenpack/94361c40e421460b8b32 to your computer and use it in GitHub Desktop.
Lifetime Problem
use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::cmp::Ordering;
pub struct Cache<'a, K: 'a,V: 'a> where K : Eq + Hash, V: Ord {
map: HashMap<K,V>,
keys: BTreeMap<&'a V, &'a K>
}
impl<'a, K,V> Cache<'a, K,V> where K : Eq + Hash, V: Ord {
pub fn new() -> Cache<'a, K,V> {
Cache {
map: HashMap::new(),
keys: BTreeMap::new()
}
}
pub fn upsert(&mut self, key: K, val: V) {
self.keys.insert(&val, &key);
self.map.insert(key, val);
}
}
#[derive(Eq)]
#[derive(PartialEq)]
#[derive(Hash)]
struct ResolverCacheKey {
qname: String,
qtype: u32,
qclass: u32
}
#[derive(PartialOrd)]
#[derive(PartialEq)]
#[derive(Eq)]
struct ResolverCacheEntry {
answers: Vec<String>,
ttl: u32
}
impl Ord for ResolverCacheEntry {
fn cmp(&self, other: &Self) -> Ordering {
self.ttl.cmp(&other.ttl)
}
}
pub struct ResolverCache<'a> {
base: Cache<'a,ResolverCacheKey,ResolverCacheEntry>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment