Skip to content

Instantly share code, notes, and snippets.

@inv2004
Created May 3, 2018 15:22
Show Gist options
  • Save inv2004/68ac11e42a90d64483c6980d5d23f639 to your computer and use it in GitHub Desktop.
Save inv2004/68ac11e42a90d64483c6980d5d23f639 to your computer and use it in GitHub Desktop.
#![feature(specialization)]
use std::marker::PhantomData;
pub trait PHash<T> {
fn new() -> T;
}
// pub trait PHashFun<K,V> {
// fn
// }
pub struct PHashMap<T>(T);
pub struct PHashMapDef<K,V> {
keys: Vec<K>,
vals: Vec<V>
}
pub struct PHashMapSimple<K,V> {
phantom: PhantomData<K>,
vals: Vec<Option<V>>
}
impl<K,V> PHash<PHashMap<PHashMapDef<K,V>>> for PHashMap<PHashMapDef<K,V>> {
default fn new() -> PHashMap<PHashMapDef<K,V>> {
PHashMap(PHashMapDef {
keys: Vec::new(),
vals: Vec::new()
})
}
}
impl<V: Clone> PHash<PHashMap<PHashMapSimple<u8,V>>> for PHashMap<PHashMapSimple<u8,V>> {
fn new() -> PHashMap<PHashMapSimple<u8,V>> {
PHashMap(PHashMapSimple {
phantom: PhantomData,
vals: vec![None; std::u8::MAX as usize]
})
}
}
impl<K,V> PHashMap<PHashMapDef<K,V>> {
pub fn insert(&mut self, k: K, v: V) {
println!("insert default");
}
}
impl<V> PHashMap<PHashMapSimple<u8,V>> {
pub fn insert(&mut self, k: u8, v: V) {
println!("insert u8");
self.0.vals[k as usize] = Some(v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment