about summary refs log tree commit diff
diff options
context:
space:
mode:
authorP1start <rewi-github@whanau.org>2014-08-02 19:08:08 +1200
committerP1start <rewi-github@whanau.org>2014-08-12 15:33:05 +1200
commit8f71cb06bc019af707f119b5dda3d96a5ca1330c (patch)
treec8142c6e83f0a96998a115b2912c362127ba8065
parent11b8f9c3f666e3a0533da0f3a00188689fc60cb4 (diff)
downloadrust-8f71cb06bc019af707f119b5dda3d96a5ca1330c.tar.gz
rust-8f71cb06bc019af707f119b5dda3d96a5ca1330c.zip
Implement Index for TrieMap
-rw-r--r--src/libcollections/trie.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs
index ec7ed919177..c3dcebfc815 100644
--- a/src/libcollections/trie.rs
+++ b/src/libcollections/trie.rs
@@ -502,6 +502,21 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
     }
 }
 
+impl<T> Index<uint, T> for TrieMap<T> {
+    #[inline]
+    fn index<'a>(&'a self, i: &uint) -> &'a T {
+        self.find(i).expect("key not present")
+    }
+}
+
+// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
+/*impl<T> IndexMut<uint, T> for TrieMap<T> {
+    #[inline]
+    fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut T {
+        self.find_mut(i).expect("key not present")
+    }
+}*/
+
 /// A set implemented as a radix trie.
 ///
 /// # Example
@@ -1391,6 +1406,29 @@ mod test_map {
         assert!(map_str == "{1: a, 2: b}".to_string());
         assert_eq!(format!("{}", empty), "{}".to_string());
     }
+
+    #[test]
+    fn test_index() {
+        let mut map = TrieMap::new();
+
+        map.insert(1, 2i);
+        map.insert(2, 1i);
+        map.insert(3, 4i);
+
+        assert_eq!(map[2], 1);
+    }
+
+    #[test]
+    #[should_fail]
+    fn test_index_nonexistent() {
+        let mut map = TrieMap::new();
+
+        map.insert(1, 2i);
+        map.insert(2, 1i);
+        map.insert(3, 4i);
+
+        map[4];
+    }
 }
 
 #[cfg(test)]