Skip to content

Commit c50d3e3

Browse files
stepanchegthestinger
authored andcommitted
ToStr for HashMap does not need value to implement Eq or Hash
1 parent 8d3bb7e commit c50d3e3

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/libstd/to_str.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<A:ToStr> ToStr for (A,) {
5050
}
5151
}
5252

53-
impl<A:ToStr+Hash+Eq, B:ToStr+Hash+Eq> ToStr for HashMap<A, B> {
53+
impl<A:ToStr+Hash+Eq, B:ToStr> ToStr for HashMap<A, B> {
5454
#[inline]
5555
fn to_str(&self) -> ~str {
5656
let mut acc = ~"{";
@@ -182,6 +182,8 @@ mod tests {
182182
use hashmap::HashMap;
183183
use hashmap::HashSet;
184184
use container::{MutableSet, MutableMap};
185+
use super::*;
186+
185187
#[test]
186188
fn test_simple_types() {
187189
assert_eq!(1i.to_str(), ~"1");
@@ -212,17 +214,27 @@ mod tests {
212214
~"[[], [1], [1, 1]]");
213215
}
214216
217+
struct StructWithToStrWithoutEqOrHash {
218+
value: int
219+
}
220+
221+
impl ToStr for StructWithToStrWithoutEqOrHash {
222+
fn to_str(&self) -> ~str {
223+
fmt!("s%d", self.value)
224+
}
225+
}
226+
215227
#[test]
216228
fn test_hashmap() {
217-
let mut table: HashMap<int, int> = HashMap::new();
218-
let empty: HashMap<int, int> = HashMap::new();
229+
let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
230+
let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
219231
220-
table.insert(3, 4);
221-
table.insert(1, 2);
232+
table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
233+
table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
222234
223235
let table_str = table.to_str();
224236
225-
assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}");
237+
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
226238
assert_eq!(empty.to_str(), ~"{}");
227239
}
228240

0 commit comments

Comments
 (0)