Skip to content

Commit fe8a413

Browse files
committed
handling fallout from entry api
1 parent 8e58f30 commit fe8a413

File tree

14 files changed

+99
-44
lines changed

14 files changed

+99
-44
lines changed

src/librustc/driver/config.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use syntax::parse;
3131
use syntax::parse::token::InternedString;
3232

3333
use std::collections::HashMap;
34+
use std::collections::hashmap::{Occupied, Vacant};
3435
use getopts::{optopt, optmulti, optflag, optflagopt};
3536
use getopts;
3637
use std::cell::{RefCell};
@@ -808,8 +809,11 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
808809
Some(s) => s,
809810
None => early_error("--extern value must be of the format `foo=bar`"),
810811
};
811-
let locs = externs.find_or_insert(name.to_string(), Vec::new());
812-
locs.push(location.to_string());
812+
813+
match externs.entry(name.to_string()) {
814+
Vacant(entry) => { entry.set(vec![location.to_string()]); },
815+
Occupied(mut entry) => { entry.get_mut().push(location.to_string()); },
816+
}
813817
}
814818

815819
let crate_name = matches.opt_str("crate-name");

src/librustc/lint/builtin.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use lint::{Context, LintPass, LintArray};
3636

3737
use std::cmp;
3838
use std::collections::HashMap;
39+
use std::collections::hashmap::{Occupied, Vacant};
3940
use std::slice;
4041
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
4142
use syntax::abi;
@@ -1204,15 +1205,18 @@ impl UnusedMut {
12041205
fn check_unused_mut_pat(&self, cx: &Context, pats: &[P<ast::Pat>]) {
12051206
// collect all mutable pattern and group their NodeIDs by their Identifier to
12061207
// avoid false warnings in match arms with multiple patterns
1208+
12071209
let mut mutables = HashMap::new();
12081210
for p in pats.iter() {
12091211
pat_util::pat_bindings(&cx.tcx.def_map, &**p, |mode, id, _, path1| {
12101212
let ident = path1.node;
12111213
match mode {
12121214
ast::BindByValue(ast::MutMutable) => {
12131215
if !token::get_ident(ident).get().starts_with("_") {
1214-
mutables.insert_or_update_with(ident.name.uint(),
1215-
vec!(id), |_, old| { old.push(id); });
1216+
match mutables.entry(ident.name.uint()) {
1217+
Vacant(entry) => { entry.set(vec![id]); },
1218+
Occupied(mut entry) => { entry.get_mut().push(id); },
1219+
}
12161220
}
12171221
}
12181222
_ => {

src/librustc/metadata/creader.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use plugin::load::PluginMetadata;
2424

2525
use std::rc::Rc;
2626
use std::collections::HashMap;
27+
use std::collections::hashmap::{Occupied, Vacant};
2728
use syntax::ast;
2829
use syntax::abi;
2930
use syntax::attr;
@@ -82,7 +83,10 @@ fn dump_crates(cstore: &CStore) {
8283
fn warn_if_multiple_versions(diag: &SpanHandler, cstore: &CStore) {
8384
let mut map = HashMap::new();
8485
cstore.iter_crate_data(|cnum, data| {
85-
map.find_or_insert_with(data.name(), |_| Vec::new()).push(cnum);
86+
match map.entry(data.name()) {
87+
Vacant(entry) => { entry.set(vec![cnum]); },
88+
Occupied(mut entry) => { entry.get_mut().push(cnum); },
89+
}
8690
});
8791

8892
for (name, dupes) in map.into_iter() {

src/librustc/metadata/loader.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ use std::slice;
237237
use std::string;
238238

239239
use std::collections::{HashMap, HashSet};
240+
use std::collections::hashmap::{Occupied, Vacant};
240241
use flate;
241242
use time;
242243

@@ -428,15 +429,18 @@ impl<'a> Context<'a> {
428429
return FileDoesntMatch
429430
};
430431
info!("lib candidate: {}", path.display());
431-
let slot = candidates.find_or_insert_with(hash.to_string(), |_| {
432-
(HashSet::new(), HashSet::new())
433-
});
432+
433+
let slot = match candidates.entry(hash.to_string()) {
434+
Occupied(entry) => entry.into_mut(),
435+
Vacant(entry) => entry.set((HashSet::new(), HashSet::new())),
436+
};
434437
let (ref mut rlibs, ref mut dylibs) = *slot;
435438
if rlib {
436439
rlibs.insert(fs::realpath(path).unwrap());
437440
} else {
438441
dylibs.insert(fs::realpath(path).unwrap());
439442
}
443+
440444
FileMatches
441445
});
442446

src/librustc/middle/const_eval.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use syntax::visit;
2828
use syntax::{ast, ast_map, ast_util};
2929

3030
use std::rc::Rc;
31+
use std::collections::hashmap::Vacant;
3132

3233
//
3334
// This pass classifies expressions by their constant-ness.
@@ -321,7 +322,10 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> {
321322

322323
ExprCall(ref callee, ref args) => {
323324
let def = tcx.def_map.borrow().get_copy(&callee.id);
324-
tcx.def_map.borrow_mut().find_or_insert(expr.id, def);
325+
match tcx.def_map.borrow_mut().entry(expr.id) {
326+
Vacant(entry) => { entry.set(def); }
327+
_ => {}
328+
};
325329
let path = match def {
326330
def::DefStruct(def_id) => def_to_path(tcx, def_id),
327331
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),

src/librustc/middle/resolve.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use syntax::visit;
5858
use syntax::visit::Visitor;
5959

6060
use std::collections::{HashMap, HashSet};
61+
use std::collections::hashmap::{Occupied, Vacant};
6162
use std::cell::{Cell, RefCell};
6263
use std::gc::GC;
6364
use std::mem::replace;
@@ -2813,10 +2814,13 @@ impl<'a> Resolver<'a> {
28132814
let is_public = import_directive.is_public;
28142815

28152816
let mut import_resolutions = module_.import_resolutions.borrow_mut();
2816-
let dest_import_resolution = import_resolutions.find_or_insert_with(name, |_| {
2817-
// Create a new import resolution from this child.
2818-
ImportResolution::new(id, is_public)
2819-
});
2817+
let dest_import_resolution = match import_resolutions.entry(name) {
2818+
Occupied(entry) => entry.into_mut(),
2819+
Vacant(entry) => {
2820+
// Create a new import resolution from this child.
2821+
entry.set(ImportResolution::new(id, is_public))
2822+
}
2823+
};
28202824

28212825
debug!("(resolving glob import) writing resolution `{}` in `{}` \
28222826
to `{}`",
@@ -5991,19 +5995,21 @@ impl<'a> Resolver<'a> {
59915995
assert!(match lp {LastImport{..} => false, _ => true},
59925996
"Import should only be used for `use` directives");
59935997
self.last_private.insert(node_id, lp);
5994-
self.def_map.borrow_mut().insert_or_update_with(node_id, def, |_, old_value| {
5998+
5999+
match self.def_map.borrow_mut().entry(node_id) {
59956000
// Resolve appears to "resolve" the same ID multiple
59966001
// times, so here is a sanity check it at least comes to
59976002
// the same conclusion! - nmatsakis
5998-
if def != *old_value {
6003+
Occupied(entry) => if def != *entry.get() {
59996004
self.session
60006005
.bug(format!("node_id {:?} resolved first to {:?} and \
60016006
then {:?}",
60026007
node_id,
6003-
*old_value,
6008+
*entry.get(),
60046009
def).as_slice());
6005-
}
6006-
});
6010+
},
6011+
Vacant(entry) => { entry.set(def); },
6012+
}
60076013
}
60086014

60096015
fn enforce_default_binding_mode(&mut self,

src/librustc/middle/ty.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::mem;
5050
use std::ops;
5151
use std::rc::Rc;
5252
use std::collections::{HashMap, HashSet};
53+
use std::collections::hashmap::{Occupied, Vacant};
5354
use arena::TypedArena;
5455
use syntax::abi;
5556
use syntax::ast::{CrateNum, DefId, FnStyle, Ident, ItemTrait, LOCAL_CRATE};
@@ -4641,9 +4642,10 @@ pub fn lookup_field_type(tcx: &ctxt,
46414642
node_id_to_type(tcx, id.node)
46424643
} else {
46434644
let mut tcache = tcx.tcache.borrow_mut();
4644-
let pty = tcache.find_or_insert_with(id, |_| {
4645-
csearch::get_field_type(tcx, struct_id, id)
4646-
});
4645+
let pty = match tcache.entry(id) {
4646+
Occupied(entry) => entry.into_mut(),
4647+
Vacant(entry) => entry.set(csearch::get_field_type(tcx, struct_id, id)),
4648+
};
46474649
pty.ty
46484650
};
46494651
t.subst(tcx, substs)

src/librustc/middle/typeck/check/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ use util::nodemap::{DefIdMap, FnvHashMap, NodeMap};
122122

123123
use std::cell::{Cell, RefCell};
124124
use std::collections::HashMap;
125+
use std::collections::hashmap::{Occupied, Vacant};
125126
use std::mem::replace;
126127
use std::rc::Rc;
127128
use std::slice;
@@ -2017,11 +2018,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20172018
*/
20182019

20192020
let mut region_obligations = self.inh.region_obligations.borrow_mut();
2020-
let v = region_obligations.find_or_insert_with(self.body_id,
2021-
|_| Vec::new());
2022-
v.push(RegionObligation { sub_region: r,
2021+
let region_obligation = RegionObligation { sub_region: r,
20232022
sup_type: ty,
2024-
origin: origin });
2023+
origin: origin };
2024+
2025+
match region_obligations.entry(self.body_id) {
2026+
Vacant(entry) => { entry.set(vec![region_obligation]); },
2027+
Occupied(mut entry) => { entry.get_mut().push(region_obligation); },
2028+
}
20252029
}
20262030

20272031
pub fn add_obligations_for_parameters(&self,

src/librustc/middle/typeck/check/regionmanip.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use middle::ty_fold::TypeFolder;
1818
use syntax::ast;
1919

2020
use std::collections::HashMap;
21+
use std::collections::hashmap::{Occupied, Vacant};
2122
use util::ppaux::Repr;
2223

2324
// Helper functions related to manipulating region types.
@@ -35,7 +36,10 @@ pub fn replace_late_bound_regions_in_fn_sig(
3536
debug!("region r={}", r.to_string());
3637
match r {
3738
ty::ReLateBound(s, br) if s == fn_sig.binder_id => {
38-
*map.find_or_insert_with(br, |_| mapf(br))
39+
* match map.entry(br) {
40+
Vacant(entry) => entry.set(mapf(br)),
41+
Occupied(entry) => entry.into_mut(),
42+
}
3943
}
4044
_ => r
4145
}

src/librustdoc/html/render.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//! both occur before the crate is rendered.
3535
3636
use std::collections::{HashMap, HashSet};
37+
use std::collections::hashmap::{Occupied, Vacant};
3738
use std::fmt;
3839
use std::io::fs::PathExtensions;
3940
use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
@@ -801,9 +802,10 @@ impl DocFolder for Cache {
801802
clean::ImplItem(ref i) => {
802803
match i.trait_ {
803804
Some(clean::ResolvedPath{ did, .. }) => {
804-
let v = self.implementors.find_or_insert_with(did, |_| {
805-
Vec::new()
806-
});
805+
let v = match self.implementors.entry(did) {
806+
Vacant(entry) => entry.set(Vec::with_capacity(1)),
807+
Occupied(entry) => entry.into_mut(),
808+
};
807809
v.push(Implementor {
808810
def_id: item.def_id,
809811
generics: i.generics.clone(),
@@ -998,9 +1000,10 @@ impl DocFolder for Cache {
9981000

9991001
match did {
10001002
Some(did) => {
1001-
let v = self.impls.find_or_insert_with(did, |_| {
1002-
Vec::new()
1003-
});
1003+
let v = match self.impls.entry(did) {
1004+
Vacant(entry) => entry.set(Vec::with_capacity(1)),
1005+
Occupied(entry) => entry.into_mut(),
1006+
};
10041007
v.push(Impl {
10051008
impl_: i,
10061009
dox: dox,
@@ -2141,7 +2144,10 @@ fn build_sidebar(m: &clean::Module) -> HashMap<String, Vec<String>> {
21412144
None => continue,
21422145
Some(ref s) => s.to_string(),
21432146
};
2144-
let v = map.find_or_insert_with(short.to_string(), |_| Vec::new());
2147+
let v = match map.entry(short.to_string()) {
2148+
Vacant(entry) => entry.set(Vec::with_capacity(1)),
2149+
Occupied(entry) => entry.into_mut(),
2150+
};
21452151
v.push(myname);
21462152
}
21472153

src/librustdoc/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate time;
3131
use std::io;
3232
use std::io::{File, MemWriter};
3333
use std::collections::HashMap;
34+
use std::collections::hashmap::{Occupied, Vacant};
3435
use serialize::{json, Decodable, Encodable};
3536
use externalfiles::ExternalHtml;
3637

@@ -340,7 +341,10 @@ fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
340341
return Err("--extern value must be of the format `foo=bar`".to_string());
341342
}
342343
};
343-
let locs = externs.find_or_insert(name.to_string(), Vec::new());
344+
let locs = match externs.entry(name.to_string()) {
345+
Vacant(entry) => entry.set(Vec::with_capacity(1)),
346+
Occupied(entry) => entry.into_mut(),
347+
};
344348
locs.push(location.to_string());
345349
}
346350
Ok(externs)

src/libsyntax/ext/mtwt.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use ast::{Ident, Mrk, Name, SyntaxContext};
2020
use std::cell::RefCell;
2121
use std::rc::Rc;
2222
use std::collections::HashMap;
23+
use std::collections::hashmap::{Occupied, Vacant};
2324

2425
/// The SCTable contains a table of SyntaxContext_'s. It
2526
/// represents a flattened tree structure, to avoid having
@@ -65,10 +66,10 @@ pub fn apply_mark(m: Mrk, ctxt: SyntaxContext) -> SyntaxContext {
6566
/// Extend a syntax context with a given mark and sctable (explicit memoization)
6667
fn apply_mark_internal(m: Mrk, ctxt: SyntaxContext, table: &SCTable) -> SyntaxContext {
6768
let key = (ctxt, m);
68-
let new_ctxt = |_: &(SyntaxContext, Mrk)|
69-
idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt));
70-
71-
*table.mark_memo.borrow_mut().find_or_insert_with(key, new_ctxt)
69+
* match table.mark_memo.borrow_mut().entry(key) {
70+
Vacant(entry) => entry.set(idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt))),
71+
Occupied(entry) => entry.into_mut(),
72+
}
7273
}
7374

7475
/// Extend a syntax context with a given rename
@@ -83,10 +84,11 @@ fn apply_rename_internal(id: Ident,
8384
ctxt: SyntaxContext,
8485
table: &SCTable) -> SyntaxContext {
8586
let key = (ctxt, id, to);
86-
let new_ctxt = |_: &(SyntaxContext, Ident, Name)|
87-
idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt));
8887

89-
*table.rename_memo.borrow_mut().find_or_insert_with(key, new_ctxt)
88+
* match table.rename_memo.borrow_mut().entry(key) {
89+
Vacant(entry) => entry.set(idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt))),
90+
Occupied(entry) => entry.into_mut(),
91+
}
9092
}
9193

9294
/// Apply a list of renamings to a context

src/libtest/stats.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![allow(missing_doc)]
1212

1313
use std::collections::hashmap;
14+
use std::collections::hashmap::{Occupied, Vacant};
1415
use std::fmt::Show;
1516
use std::hash::Hash;
1617
use std::io;
@@ -443,7 +444,10 @@ pub fn write_boxplot<T: Float + Show + FromPrimitive>(
443444
pub fn freq_count<T: Iterator<U>, U: Eq+Hash>(mut iter: T) -> hashmap::HashMap<U, uint> {
444445
let mut map: hashmap::HashMap<U,uint> = hashmap::HashMap::new();
445446
for elem in iter {
446-
map.insert_or_update_with(elem, 1, |_, count| *count += 1);
447+
match map.entry(elem) {
448+
Occupied(mut entry) => { *entry.get_mut() += 1; },
449+
Vacant(entry) => { entry.set(1); },
450+
}
447451
}
448452
map
449453
}

src/liburl/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(default_type_params)]
2424

2525
use std::collections::HashMap;
26+
use std::collections::hashmap::{Occupied, Vacant};
2627
use std::fmt;
2728
use std::from_str::FromStr;
2829
use std::hash;
@@ -342,8 +343,10 @@ pub fn decode_form_urlencoded(s: &[u8])
342343
key: String,
343344
value: String) {
344345
if key.len() > 0 && value.len() > 0 {
345-
let values = map.find_or_insert_with(key, |_| vec!());
346-
values.push(value);
346+
match map.entry(key) {
347+
Vacant(entry) => { entry.set(vec![value]); },
348+
Occupied(mut entry) => { entry.get_mut().push(value); },
349+
}
347350
}
348351
}
349352

0 commit comments

Comments
 (0)