ToStr for HashMap does not need value to implement Eq or Hash

This commit is contained in:
Stepan Koltsov 2013-07-23 16:40:25 +04:00 committed by Daniel Micay
parent 8d3bb7eb30
commit c50d3e3fca

View file

@ -50,7 +50,7 @@ impl<A:ToStr> ToStr for (A,) {
}
}
impl<A:ToStr+Hash+Eq, B:ToStr+Hash+Eq> ToStr for HashMap<A, B> {
impl<A:ToStr+Hash+Eq, B:ToStr> ToStr for HashMap<A, B> {
#[inline]
fn to_str(&self) -> ~str {
let mut acc = ~"{";
@ -182,6 +182,8 @@ mod tests {
use hashmap::HashMap;
use hashmap::HashSet;
use container::{MutableSet, MutableMap};
use super::*;
#[test]
fn test_simple_types() {
assert_eq!(1i.to_str(), ~"1");
@ -212,17 +214,27 @@ mod tests {
~"[[], [1], [1, 1]]");
}
struct StructWithToStrWithoutEqOrHash {
value: int
}
impl ToStr for StructWithToStrWithoutEqOrHash {
fn to_str(&self) -> ~str {
fmt!("s%d", self.value)
}
}
#[test]
fn test_hashmap() {
let mut table: HashMap<int, int> = HashMap::new();
let empty: HashMap<int, int> = HashMap::new();
let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
table.insert(3, 4);
table.insert(1, 2);
table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
let table_str = table.to_str();
assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}");
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
assert_eq!(empty.to_str(), ~"{}");
}