Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/libstd/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ~"{";
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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(), ~"{}");
}

Expand Down