Last active
August 29, 2015 14:05
-
-
Save shadowmint/804b2004802b49c80867 to your computer and use it in GitHub Desktop.
hashmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
struct Tmp { | |
v:String | |
} | |
impl Tmp { | |
fn new(value:&str) -> Tmp { | |
return Tmp { | |
v:String::from_str(value) | |
}; | |
} | |
fn borrow(&self) -> &String { | |
return &self.v; | |
} | |
} | |
#[test] | |
fn test_hash_with_keys() { | |
let mut map = HashMap::<&String, &Tmp>::new(); | |
let value = Tmp::new("Hello"); | |
let value2 = Tmp::new("World"); | |
map.insert(value.borrow(), &value); | |
map.insert(value2.borrow(), &value2); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::hash::sip::SipState; | |
use std::hash::Hash; | |
struct Tmp { | |
v:String | |
} | |
#[deriving(Eq)] | |
struct TmpKey { | |
key:*const String | |
} | |
impl Tmp { | |
fn new(value:&str) -> Tmp { | |
return Tmp { | |
v:String::from_str(value) | |
}; | |
} | |
fn borrow(&self) -> TmpKey { | |
return TmpKey { key: &self.v as *const String }; | |
} | |
} | |
impl Hash for TmpKey { | |
fn hash(&self, state: &mut SipState) { | |
let tmp1:&String; | |
unsafe { | |
tmp1 = &(*self.key); | |
} | |
tmp1.hash(state); | |
} | |
} | |
impl PartialEq for TmpKey { | |
fn eq(&self, other: &TmpKey) -> bool { | |
let tmp1:&String; | |
let tmp2:&String; | |
unsafe { | |
tmp1 = &(*self.key); | |
tmp2 = &(*other.key); | |
} | |
return *tmp1 == *tmp2; | |
} | |
} | |
#[test] | |
fn test_hash_with_keys() { | |
let mut map = HashMap::<TmpKey, Tmp>::new(); | |
let value = Tmp::new("Hello"); | |
let value2 = Tmp::new("World"); | |
map.insert(value.borrow(), value); | |
map.insert(value2.borrow(), value2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment