diff options
| author | Stepan Koltsov <stepan.koltsov@gmail.com> | 2013-07-23 16:40:25 +0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-07-24 09:45:21 -0400 |
| commit | c50d3e3fca5760fff19f6c8304c608d2b41eba45 (patch) | |
| tree | f7a486487e9ff54057cbad8a9adf5144d2d6fbe6 /src/libstd | |
| parent | 8d3bb7eb306993430e5f5acf3e3fbf22d7a9d97f (diff) | |
| download | rust-c50d3e3fca5760fff19f6c8304c608d2b41eba45.tar.gz rust-c50d3e3fca5760fff19f6c8304c608d2b41eba45.zip | |
ToStr for HashMap does not need value to implement Eq or Hash
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/to_str.rs | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/libstd/to_str.rs b/src/libstd/to_str.rs index 227712e31e6..50cbd36ced9 100644 --- a/src/libstd/to_str.rs +++ b/src/libstd/to_str.rs @@ -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(), ~"{}"); } |
