diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 2fb3143b0bf6e..a1dd49bbf898d 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -166,7 +166,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: ast::DefId, p: &clean::Path, if ast_util::is_local(did) || cache.inlined.contains(&did) { Some(("../".repeat(loc.len())).to_string()) } else { - match *cache.extern_locations.get(&did.krate) { + match cache.extern_locations[did.krate] { render::Remote(ref s) => Some(s.to_string()), render::Local => { Some(("../".repeat(loc.len())).to_string()) @@ -291,11 +291,11 @@ fn primitive_link(f: &mut fmt::Formatter, needs_termination = true; } Some(&cnum) => { - let path = m.paths.get(&ast::DefId { + let path = &m.paths[ast::DefId { krate: cnum, node: ast::CRATE_NODE_ID, - }); - let loc = match *m.extern_locations.get(&cnum) { + }]; + let loc = match m.extern_locations[cnum] { render::Remote(ref s) => Some(s.to_string()), render::Local => { let loc = current_location_key.get().unwrap(); @@ -343,11 +343,11 @@ impl fmt::Show for clean::Type { match *self { clean::TyParamBinder(id) => { let m = cache_key.get().unwrap(); - f.write(m.typarams.get(&ast_util::local_def(id)).as_bytes()) + f.write(m.typarams[ast_util::local_def(id)].as_bytes()) } clean::Generic(did) => { let m = cache_key.get().unwrap(); - f.write(m.typarams.get(&did).as_bytes()) + f.write(m.typarams[did].as_bytes()) } clean::ResolvedPath{ did, ref typarams, ref path } => { try!(resolved_path(f, did, path, false)); diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 084ba46797ef9..552165399dca3 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1252,8 +1252,8 @@ impl<'a> Item<'a> { // located, then we return `None`. } else { let cache = cache_key.get().unwrap(); - let path = cache.external_paths.get(&self.item.def_id); - let root = match *cache.extern_locations.get(&self.item.def_id.krate) { + let path = &cache.external_paths[self.item.def_id]; + let root = match cache.extern_locations[self.item.def_id.krate] { Remote(ref s) => s.to_string(), Local => self.cx.root_path.clone(), Unknown => return None, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index d28069da6ba06..5b2a542ca9664 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -229,7 +229,7 @@ impl<'a> RustdocVisitor<'a> { core::Typed(ref tcx) => tcx, core::NotTyped(_) => return false }; - let def = tcx.def_map.borrow().get(&id).def_id(); + let def = (*tcx.def_map.borrow())[id].def_id(); if !ast_util::is_local(def) { return false } let analysis = match self.analysis { Some(analysis) => analysis, None => return false diff --git a/src/libstd/collections/hashmap.rs b/src/libstd/collections/hashmap.rs index a569ee4a32a18..2532cf4b93d7d 100644 --- a/src/libstd/collections/hashmap.rs +++ b/src/libstd/collections/hashmap.rs @@ -26,6 +26,7 @@ use mem::replace; use num; use option::{Some, None, Option}; use result::{Ok, Err}; +use ops::Index; mod table { use clone::Clone; @@ -1341,7 +1342,7 @@ impl, V, S, H: Hasher> HashMap { /// /// // Update and return the existing value /// assert_eq!(*map.insert_or_update_with("a", 9, |_key, val| *val = 7), 7); - /// assert_eq!(map.get(&"a"), &7); + /// assert_eq!(map["a"], 7); /// ``` pub fn insert_or_update_with<'a>( &'a mut self, @@ -1392,9 +1393,9 @@ impl, V, S, H: Hasher> HashMap { /// } /// /// assert_eq!(map.len(), 3); - /// assert_eq!(map.get(&"a key"), &vec!["value", "new value"]); - /// assert_eq!(map.get(&"b key"), &vec!["new value"]); - /// assert_eq!(map.get(&"z key"), &vec!["new value", "value"]); + /// assert_eq!(map["a key"], vec!["value", "new value"]); + /// assert_eq!(map["b key"], vec!["new value"]); + /// assert_eq!(map["z key"], vec!["new value", "value"]); /// ``` pub fn find_with_or_insert_with<'a, A>(&'a mut self, k: K, @@ -1426,12 +1427,15 @@ impl, V, S, H: Hasher> HashMap { /// # Example /// /// ``` + /// #![allow(deprecated)] + /// /// use std::collections::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1i); /// assert_eq!(map.get(&"a"), &1); /// ``` + #[deprecated = "prefer indexing instead, e.g., map[key]"] pub fn get<'a>(&'a self, k: &K) -> &'a V { match self.find(k) { Some(v) => v, @@ -1458,11 +1462,11 @@ impl, V, S, H: Hasher> HashMap { /// let val = map.get_mut(&"a"); /// *val = 40; /// } - /// assert_eq!(map.get(&"a"), &40); + /// assert_eq!(map["a"], 40); /// /// // A more direct way could be: /// *map.get_mut(&"a") = -2; - /// assert_eq!(map.get(&"a"), &-2); + /// assert_eq!(map["a"], -2); /// ``` pub fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V { match self.find_mut(k) { @@ -1738,6 +1742,21 @@ impl, V, S, H: Hasher + Default> Default for HashMap } } +impl, V, S, H: Hasher> Index for HashMap { + #[inline] + fn index<'a>(&'a self, index: &K) -> &'a V { + self.get(index) + } +} + +// FIXME(#12825) Indexing will always try IndexMut first and that causes issues. +/*impl, V, S, H: Hasher> ops::IndexMut for HashMap { + #[inline] + fn index_mut<'a>(&'a mut self, index: &K) -> &'a mut V { + self.get_mut(index) + } +}*/ + /// HashMap iterator pub type Entries<'a, K, V> = table::Entries<'a, K, V>; @@ -2694,6 +2713,29 @@ mod test_map { assert_eq!(iter.size_hint(), (3, Some(3))); } + + #[test] + fn test_index() { + let mut map: HashMap = HashMap::new(); + + map.insert(1, 2); + map.insert(2, 1); + map.insert(3, 4); + + assert_eq!(map[2], 1); + } + + #[test] + #[should_fail] + fn test_index_nonexistent() { + let mut map: HashMap = HashMap::new(); + + map.insert(1, 2); + map.insert(2, 1); + map.insert(3, 4); + + map[4]; + } } #[cfg(test)]