Skip to content

Commit af701fc

Browse files
committed
---
yaml --- r: 127758 b: refs/heads/master c: 2dd6bc6 h: refs/heads/master v: v3
1 parent e802993 commit af701fc

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 32f5898bea0ebed00b066a211749ce155aa2b8a6
2+
refs/heads/master: 2dd6bc6887858f91b846ffa7dbd9f0f49b9f3b87
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: aa98b25c4f0c10729dff37c699904ad57b8fbda8
55
refs/heads/try: d9c23fcbaea89871667272a67ecb8d3a512162f3

trunk/src/libcollections/treemap.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
237237
fn default() -> TreeMap<K, V> { TreeMap::new() }
238238
}
239239

240+
impl<K: Ord, V> Index<K, V> for TreeMap<K, V> {
241+
#[inline]
242+
fn index<'a>(&'a self, i: &K) -> &'a V {
243+
self.find(i).expect("no entry found for key")
244+
}
245+
}
246+
247+
/*impl<K: Ord, V> IndexMut<K, V> for TreeMap<K, V> {
248+
#[inline]
249+
fn index_mut<'a>(&'a mut self, i: &K) -> &'a mut V {
250+
self.find_mut(i).expect("no entry found for key")
251+
}
252+
}*/
253+
240254
impl<K: Ord, V> TreeMap<K, V> {
241255
/// Create an empty `TreeMap`.
242256
///
@@ -2131,6 +2145,28 @@ mod test_treemap {
21312145
}
21322146
}
21332147

2148+
#[test]
2149+
fn test_index() {
2150+
let mut map: TreeMap<int, int> = TreeMap::new();
2151+
2152+
map.insert(1, 2);
2153+
map.insert(2, 1);
2154+
map.insert(3, 4);
2155+
2156+
assert_eq!(map[2], 1);
2157+
}
2158+
2159+
#[test]
2160+
#[should_fail]
2161+
fn test_index_nonexistent() {
2162+
let mut map: TreeMap<int, int> = TreeMap::new();
2163+
2164+
map.insert(1, 2);
2165+
map.insert(2, 1);
2166+
map.insert(3, 4);
2167+
2168+
map[4];
2169+
}
21342170
}
21352171

21362172
#[cfg(test)]

0 commit comments

Comments
 (0)