Skip to content

Commit 32f5898

Browse files
committed
Implement Index for HashMap
This also deprecates HashMap::get. Use indexing instead.
1 parent fd10d20 commit 32f5898

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

src/librustdoc/html/format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: ast::DefId, p: &clean::Path,
166166
if ast_util::is_local(did) || cache.inlined.contains(&did) {
167167
Some(("../".repeat(loc.len())).to_string())
168168
} else {
169-
match *cache.extern_locations.get(&did.krate) {
169+
match cache.extern_locations[did.krate] {
170170
render::Remote(ref s) => Some(s.to_string()),
171171
render::Local => {
172172
Some(("../".repeat(loc.len())).to_string())
@@ -291,11 +291,11 @@ fn primitive_link(f: &mut fmt::Formatter,
291291
needs_termination = true;
292292
}
293293
Some(&cnum) => {
294-
let path = m.paths.get(&ast::DefId {
294+
let path = &m.paths[ast::DefId {
295295
krate: cnum,
296296
node: ast::CRATE_NODE_ID,
297-
});
298-
let loc = match *m.extern_locations.get(&cnum) {
297+
}];
298+
let loc = match m.extern_locations[cnum] {
299299
render::Remote(ref s) => Some(s.to_string()),
300300
render::Local => {
301301
let loc = current_location_key.get().unwrap();
@@ -343,11 +343,11 @@ impl fmt::Show for clean::Type {
343343
match *self {
344344
clean::TyParamBinder(id) => {
345345
let m = cache_key.get().unwrap();
346-
f.write(m.typarams.get(&ast_util::local_def(id)).as_bytes())
346+
f.write(m.typarams[ast_util::local_def(id)].as_bytes())
347347
}
348348
clean::Generic(did) => {
349349
let m = cache_key.get().unwrap();
350-
f.write(m.typarams.get(&did).as_bytes())
350+
f.write(m.typarams[did].as_bytes())
351351
}
352352
clean::ResolvedPath{ did, ref typarams, ref path } => {
353353
try!(resolved_path(f, did, path, false));

src/librustdoc/html/render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,8 @@ impl<'a> Item<'a> {
12521252
// located, then we return `None`.
12531253
} else {
12541254
let cache = cache_key.get().unwrap();
1255-
let path = cache.external_paths.get(&self.item.def_id);
1256-
let root = match *cache.extern_locations.get(&self.item.def_id.krate) {
1255+
let path = &cache.external_paths[self.item.def_id];
1256+
let root = match cache.extern_locations[self.item.def_id.krate] {
12571257
Remote(ref s) => s.to_string(),
12581258
Local => self.cx.root_path.clone(),
12591259
Unknown => return None,

src/librustdoc/visit_ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a> RustdocVisitor<'a> {
229229
core::Typed(ref tcx) => tcx,
230230
core::NotTyped(_) => return false
231231
};
232-
let def = tcx.def_map.borrow().get(&id).def_id();
232+
let def = (*tcx.def_map.borrow())[id].def_id();
233233
if !ast_util::is_local(def) { return false }
234234
let analysis = match self.analysis {
235235
Some(analysis) => analysis, None => return false

src/libstd/collections/hashmap.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use mem::replace;
2626
use num;
2727
use option::{Some, None, Option};
2828
use result::{Ok, Err};
29+
use ops::Index;
2930

3031
mod table {
3132
use clone::Clone;
@@ -1341,7 +1342,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
13411342
///
13421343
/// // Update and return the existing value
13431344
/// assert_eq!(*map.insert_or_update_with("a", 9, |_key, val| *val = 7), 7);
1344-
/// assert_eq!(map.get(&"a"), &7);
1345+
/// assert_eq!(map["a"], 7);
13451346
/// ```
13461347
pub fn insert_or_update_with<'a>(
13471348
&'a mut self,
@@ -1392,9 +1393,9 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
13921393
/// }
13931394
///
13941395
/// assert_eq!(map.len(), 3);
1395-
/// assert_eq!(map.get(&"a key"), &vec!["value", "new value"]);
1396-
/// assert_eq!(map.get(&"b key"), &vec!["new value"]);
1397-
/// assert_eq!(map.get(&"z key"), &vec!["new value", "value"]);
1396+
/// assert_eq!(map["a key"], vec!["value", "new value"]);
1397+
/// assert_eq!(map["b key"], vec!["new value"]);
1398+
/// assert_eq!(map["z key"], vec!["new value", "value"]);
13981399
/// ```
13991400
pub fn find_with_or_insert_with<'a, A>(&'a mut self,
14001401
k: K,
@@ -1426,12 +1427,15 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14261427
/// # Example
14271428
///
14281429
/// ```
1430+
/// #![allow(deprecated)]
1431+
///
14291432
/// use std::collections::HashMap;
14301433
///
14311434
/// let mut map = HashMap::new();
14321435
/// map.insert("a", 1i);
14331436
/// assert_eq!(map.get(&"a"), &1);
14341437
/// ```
1438+
#[deprecated = "prefer indexing instead, e.g., map[key]"]
14351439
pub fn get<'a>(&'a self, k: &K) -> &'a V {
14361440
match self.find(k) {
14371441
Some(v) => v,
@@ -1458,11 +1462,11 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14581462
/// let val = map.get_mut(&"a");
14591463
/// *val = 40;
14601464
/// }
1461-
/// assert_eq!(map.get(&"a"), &40);
1465+
/// assert_eq!(map["a"], 40);
14621466
///
14631467
/// // A more direct way could be:
14641468
/// *map.get_mut(&"a") = -2;
1465-
/// assert_eq!(map.get(&"a"), &-2);
1469+
/// assert_eq!(map["a"], -2);
14661470
/// ```
14671471
pub fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V {
14681472
match self.find_mut(k) {
@@ -1738,6 +1742,21 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Default for HashMap<K, V, H>
17381742
}
17391743
}
17401744

1745+
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Index<K, V> for HashMap<K, V, H> {
1746+
#[inline]
1747+
fn index<'a>(&'a self, index: &K) -> &'a V {
1748+
self.get(index)
1749+
}
1750+
}
1751+
1752+
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
1753+
/*impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> ops::IndexMut<K, V> for HashMap<K, V, H> {
1754+
#[inline]
1755+
fn index_mut<'a>(&'a mut self, index: &K) -> &'a mut V {
1756+
self.get_mut(index)
1757+
}
1758+
}*/
1759+
17411760
/// HashMap iterator
17421761
pub type Entries<'a, K, V> = table::Entries<'a, K, V>;
17431762

@@ -2694,6 +2713,29 @@ mod test_map {
26942713

26952714
assert_eq!(iter.size_hint(), (3, Some(3)));
26962715
}
2716+
2717+
#[test]
2718+
fn test_index() {
2719+
let mut map: HashMap<int, int> = HashMap::new();
2720+
2721+
map.insert(1, 2);
2722+
map.insert(2, 1);
2723+
map.insert(3, 4);
2724+
2725+
assert_eq!(map[2], 1);
2726+
}
2727+
2728+
#[test]
2729+
#[should_fail]
2730+
fn test_index_nonexistent() {
2731+
let mut map: HashMap<int, int> = HashMap::new();
2732+
2733+
map.insert(1, 2);
2734+
map.insert(2, 1);
2735+
map.insert(3, 4);
2736+
2737+
map[4];
2738+
}
26972739
}
26982740

26992741
#[cfg(test)]

0 commit comments

Comments
 (0)