Skip to content

Commit 93cdf1f

Browse files
committed
update everything to use Entry defaults
1 parent 1c35953 commit 93cdf1f

File tree

12 files changed

+24
-84
lines changed

12 files changed

+24
-84
lines changed

src/libcollections/btree/map.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -1587,21 +1587,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
15871587
/// ```
15881588
/// # #![feature(collections)]
15891589
/// use std::collections::BTreeMap;
1590-
/// use std::collections::btree_map::Entry;
15911590
///
15921591
/// let mut count: BTreeMap<&str, usize> = BTreeMap::new();
15931592
///
15941593
/// // count the number of occurrences of letters in the vec
1595-
/// for x in vec!["a","b","a","c","a","b"].iter() {
1596-
/// match count.entry(*x) {
1597-
/// Entry::Vacant(view) => {
1598-
/// view.insert(1);
1599-
/// },
1600-
/// Entry::Occupied(mut view) => {
1601-
/// let v = view.get_mut();
1602-
/// *v += 1;
1603-
/// },
1604-
/// }
1594+
/// for x in vec!["a","b","a","c","a","b"] {
1595+
/// *count.entry(x).default(0) += 1;
16051596
/// }
16061597
///
16071598
/// assert_eq!(count["a"], 3);

src/libcollections/vec_map.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -632,21 +632,12 @@ impl<V> VecMap<V> {
632632
/// ```
633633
/// # #![feature(collections)]
634634
/// use std::collections::VecMap;
635-
/// use std::collections::vec_map::Entry;
636635
///
637636
/// let mut count: VecMap<u32> = VecMap::new();
638637
///
639638
/// // count the number of occurrences of numbers in the vec
640-
/// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4].iter() {
641-
/// match count.entry(*x) {
642-
/// Entry::Vacant(view) => {
643-
/// view.insert(1);
644-
/// },
645-
/// Entry::Occupied(mut view) => {
646-
/// let v = view.get_mut();
647-
/// *v += 1;
648-
/// },
649-
/// }
639+
/// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
640+
/// *count.entry(x).default(0) += 1;
650641
/// }
651642
///
652643
/// assert_eq!(count[1], 3);

src/librustc/metadata/loader.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,7 @@ impl<'a> Context<'a> {
426426
info!("lib candidate: {}", path.display());
427427

428428
let hash_str = hash.to_string();
429-
let slot = candidates.entry(hash_str).get().unwrap_or_else(
430-
|vacant_entry| vacant_entry.insert((HashMap::new(), HashMap::new())));
429+
let slot = candidates.entry(hash_str).default_with(|| (HashMap::new(), HashMap::new()));
431430
let (ref mut rlibs, ref mut dylibs) = *slot;
432431
if rlib {
433432
rlibs.insert(fs::realpath(path).unwrap(), kind);

src/librustc/middle/dataflow.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,7 @@ fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,
160160

161161
cfg.graph.each_node(|node_idx, node| {
162162
if let cfg::CFGNodeData::AST(id) = node.data {
163-
match index.entry(id).get() {
164-
Ok(v) => v.push(node_idx),
165-
Err(e) => {
166-
e.insert(vec![node_idx]);
167-
}
168-
}
163+
index.entry(id).default(vec![]).push(node_idx);
169164
}
170165
true
171166
});
@@ -185,12 +180,7 @@ fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,
185180
visit::walk_fn_decl(&mut formals, decl);
186181
impl<'a, 'v> visit::Visitor<'v> for Formals<'a> {
187182
fn visit_pat(&mut self, p: &ast::Pat) {
188-
match self.index.entry(p.id).get() {
189-
Ok(v) => v.push(self.entry),
190-
Err(e) => {
191-
e.insert(vec![self.entry]);
192-
}
193-
}
183+
self.index.entry(p.id).default(vec![]).push(self.entry);
194184
visit::walk_pat(self, p)
195185
}
196186
}

src/librustc/middle/traits/fulfill.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use middle::infer::{InferCtxt};
1212
use middle::ty::{self, RegionEscape, Ty};
1313
use std::collections::HashSet;
14-
use std::collections::hash_map::Entry::{Occupied, Vacant};
1514
use std::default::Default;
1615
use syntax::ast;
1716
use util::common::ErrorReported;
@@ -437,9 +436,7 @@ fn register_region_obligation<'tcx>(tcx: &ty::ctxt<'tcx>,
437436
debug!("register_region_obligation({})",
438437
region_obligation.repr(tcx));
439438

440-
match region_obligations.entry(region_obligation.cause.body_id) {
441-
Vacant(entry) => { entry.insert(vec![region_obligation]); },
442-
Occupied(mut entry) => { entry.get_mut().push(region_obligation); },
443-
}
439+
region_obligations.entry(region_obligation.cause.body_id).default(vec![])
440+
.push(region_obligation);
444441

445442
}

src/librustc/middle/ty.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5669,9 +5669,7 @@ pub fn lookup_field_type<'tcx>(tcx: &ctxt<'tcx>,
56695669
node_id_to_type(tcx, id.node)
56705670
} else {
56715671
let mut tcache = tcx.tcache.borrow_mut();
5672-
let pty = tcache.entry(id).get().unwrap_or_else(
5673-
|vacant_entry| vacant_entry.insert(csearch::get_field_type(tcx, struct_id, id)));
5674-
pty.ty
5672+
tcache.entry(id).default_with(|| csearch::get_field_type(tcx, struct_id, id)).ty
56755673
};
56765674
ty.subst(tcx, substs)
56775675
}
@@ -6819,9 +6817,7 @@ pub fn replace_late_bound_regions<'tcx, T, F>(
68196817
debug!("region={}", region.repr(tcx));
68206818
match region {
68216819
ty::ReLateBound(debruijn, br) if debruijn.depth == current_depth => {
6822-
let region =
6823-
* map.entry(br).get().unwrap_or_else(
6824-
|vacant_entry| vacant_entry.insert(mapf(br)));
6820+
let region = *map.entry(br).default_with(|| mapf(br));
68256821

68266822
if let ty::ReLateBound(debruijn1, br) = region {
68276823
// If the callback returns a late-bound region,

src/librustc/session/config.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use syntax::parse::token::InternedString;
3535

3636
use getopts;
3737
use std::collections::HashMap;
38-
use std::collections::hash_map::Entry::{Occupied, Vacant};
3938
use std::env;
4039
use std::fmt;
4140
use std::path::PathBuf;
@@ -1037,10 +1036,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10371036
None => early_error("--extern value must be of the format `foo=bar`"),
10381037
};
10391038

1040-
match externs.entry(name.to_string()) {
1041-
Vacant(entry) => { entry.insert(vec![location.to_string()]); },
1042-
Occupied(mut entry) => { entry.get_mut().push(location.to_string()); },
1043-
}
1039+
externs.entry(name.to_string()).default(vec![]).push(location.to_string());
10441040
}
10451041

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

src/librustc_typeck/check/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ use util::nodemap::{DefIdMap, FnvHashMap, NodeMap};
112112
use util::lev_distance::lev_distance;
113113

114114
use std::cell::{Cell, Ref, RefCell};
115-
use std::collections::hash_map::Entry::{Occupied, Vacant};
116115
use std::mem::replace;
117116
use std::rc::Rc;
118117
use std::iter::repeat;
@@ -1362,11 +1361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13621361
closure_def_id: ast::DefId,
13631362
r: DeferredCallResolutionHandler<'tcx>) {
13641363
let mut deferred_call_resolutions = self.inh.deferred_call_resolutions.borrow_mut();
1365-
let mut vec = match deferred_call_resolutions.entry(closure_def_id) {
1366-
Occupied(entry) => entry.into_mut(),
1367-
Vacant(entry) => entry.insert(Vec::new()),
1368-
};
1369-
vec.push(r);
1364+
deferred_call_resolutions.entry(closure_def_id).default(vec![]).push(r);
13701365
}
13711366

13721367
fn remove_deferred_call_resolutions(&self,

src/librustdoc/html/render.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,7 @@ impl DocFolder for Cache {
876876
if let clean::ImplItem(ref i) = item.inner {
877877
match i.trait_ {
878878
Some(clean::ResolvedPath{ did, .. }) => {
879-
let v = self.implementors.entry(did).get().unwrap_or_else(
880-
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
881-
v.push(Implementor {
879+
self.implementors.entry(did).default(vec![]).push(Implementor {
882880
def_id: item.def_id,
883881
generics: i.generics.clone(),
884882
trait_: i.trait_.as_ref().unwrap().clone(),
@@ -1080,9 +1078,7 @@ impl DocFolder for Cache {
10801078
};
10811079

10821080
if let Some(did) = did {
1083-
let v = self.impls.entry(did).get().unwrap_or_else(
1084-
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
1085-
v.push(Impl {
1081+
self.impls.entry(did).default(vec![]).push(Impl {
10861082
impl_: i,
10871083
dox: dox,
10881084
stability: item.stability.clone(),
@@ -1334,9 +1330,8 @@ impl Context {
13341330
Some(ref s) => s.to_string(),
13351331
};
13361332
let short = short.to_string();
1337-
let v = map.entry(short).get().unwrap_or_else(
1338-
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
1339-
v.push((myname, Some(plain_summary_line(item.doc_value()))));
1333+
map.entry(short).default(vec![])
1334+
.push((myname, Some(plain_summary_line(item.doc_value()))));
13401335
}
13411336

13421337
for (_, items) in &mut map {

src/librustdoc/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
352352
}
353353
};
354354
let name = name.to_string();
355-
let locs = externs.entry(name).get().unwrap_or_else(
356-
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
357-
locs.push(location.to_string());
355+
externs.entry(name).default(vec![]).push(location.to_string());
358356
}
359357
Ok(externs)
360358
}

src/libstd/collections/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,7 @@
307307
//! let message = "she sells sea shells by the sea shore";
308308
//!
309309
//! for c in message.chars() {
310-
//! match count.entry(c) {
311-
//! Entry::Vacant(entry) => { entry.insert(1); },
312-
//! Entry::Occupied(mut entry) => *entry.get_mut() += 1,
313-
//! }
310+
//! *count.entry(c).default(0) += 1;
314311
//! }
315312
//!
316313
//! assert_eq!(count.get(&'s'), Some(&8));
@@ -343,10 +340,7 @@
343340
//! for id in orders.into_iter() {
344341
//! // If this is the first time we've seen this customer, initialize them
345342
//! // with no blood alcohol. Otherwise, just retrieve them.
346-
//! let person = match blood_alcohol.entry(id) {
347-
//! Entry::Vacant(entry) => entry.insert(Person{id: id, blood_alcohol: 0.0}),
348-
//! Entry::Occupied(entry) => entry.into_mut(),
349-
//! };
343+
//! let person = blood_alcohol.entry(id).default(Person{id: id, blood_alcohol: 0.0});
350344
//!
351345
//! // Reduce their blood alcohol level. It takes time to order and drink a beer!
352346
//! person.blood_alcohol *= 0.9;

src/libsyntax/ext/mtwt.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ pub fn apply_mark(m: Mrk, ctxt: SyntaxContext) -> SyntaxContext {
6666
/// Extend a syntax context with a given mark and sctable (explicit memoization)
6767
fn apply_mark_internal(m: Mrk, ctxt: SyntaxContext, table: &SCTable) -> SyntaxContext {
6868
let key = (ctxt, m);
69-
* table.mark_memo.borrow_mut().entry(key).get().unwrap_or_else(
70-
|vacant_entry|
71-
vacant_entry.insert(idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt))))
69+
* table.mark_memo.borrow_mut().entry(key)
70+
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))
7271
}
7372

7473
/// Extend a syntax context with a given rename
@@ -84,9 +83,8 @@ fn apply_rename_internal(id: Ident,
8483
table: &SCTable) -> SyntaxContext {
8584
let key = (ctxt, id, to);
8685

87-
* table.rename_memo.borrow_mut().entry(key).get().unwrap_or_else(
88-
|vacant_entry|
89-
vacant_entry.insert(idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt))))
86+
* table.rename_memo.borrow_mut().entry(key)
87+
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))
9088
}
9189

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

0 commit comments

Comments
 (0)