Skip to content

Commit 1b98f6d

Browse files
committed
default => or_insert per RFC
1 parent 93cdf1f commit 1b98f6d

File tree

16 files changed

+31
-34
lines changed

16 files changed

+31
-34
lines changed

src/libcollections/btree/map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
11461146
#[unstable(feature = "std_misc",
11471147
reason = "will soon be replaced by or_insert")]
11481148
#[deprecated(since = "1.0",
1149-
reason = "replaced with more ergonomic `default` and `default_with`")]
1149+
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
11501150
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
11511151
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
11521152
match self {
@@ -1159,7 +1159,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
11591159
reason = "matches entry v3 specification, waiting for dust to settle")]
11601160
/// Ensures a value is in the entry by inserting the default if empty, and returns
11611161
/// a mutable reference to the value in the entry.
1162-
pub fn default(self, default: V) -> &'a mut V {
1162+
pub fn or_insert(self, default: V) -> &'a mut V {
11631163
match self {
11641164
Occupied(entry) => entry.into_mut(),
11651165
Vacant(entry) => entry.insert(default),
@@ -1170,7 +1170,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
11701170
reason = "matches entry v3 specification, waiting for dust to settle")]
11711171
/// Ensures a value is in the entry by inserting the result of the default function if empty,
11721172
/// and returns a mutable reference to the value in the entry.
1173-
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1173+
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
11741174
match self {
11751175
Occupied(entry) => entry.into_mut(),
11761176
Vacant(entry) => entry.insert(default()),
@@ -1592,7 +1592,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
15921592
///
15931593
/// // count the number of occurrences of letters in the vec
15941594
/// for x in vec!["a","b","a","c","a","b"] {
1595-
/// *count.entry(x).default(0) += 1;
1595+
/// *count.entry(x).or_insert(0) += 1;
15961596
/// }
15971597
///
15981598
/// assert_eq!(count["a"], 3);

src/libcollections/vec_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ impl<V> VecMap<V> {
637637
///
638638
/// // count the number of occurrences of numbers in the vec
639639
/// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
640-
/// *count.entry(x).default(0) += 1;
640+
/// *count.entry(x).or_insert(0) += 1;
641641
/// }
642642
///
643643
/// assert_eq!(count[1], 3);
@@ -667,7 +667,7 @@ impl<'a, V> Entry<'a, V> {
667667
#[unstable(feature = "collections",
668668
reason = "will soon be replaced by or_insert")]
669669
#[deprecated(since = "1.0",
670-
reason = "replaced with more ergonomic `default` and `default_with`")]
670+
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
671671
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
672672
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> {
673673
match self {
@@ -680,7 +680,7 @@ impl<'a, V> Entry<'a, V> {
680680
reason = "matches entry v3 specification, waiting for dust to settle")]
681681
/// Ensures a value is in the entry by inserting the default if empty, and returns
682682
/// a mutable reference to the value in the entry.
683-
pub fn default(self, default: V) -> &'a mut V {
683+
pub fn or_insert(self, default: V) -> &'a mut V {
684684
match self {
685685
Occupied(entry) => entry.into_mut(),
686686
Vacant(entry) => entry.insert(default),
@@ -691,7 +691,7 @@ impl<'a, V> Entry<'a, V> {
691691
reason = "matches entry v3 specification, waiting for dust to settle")]
692692
/// Ensures a value is in the entry by inserting the result of the default function if empty,
693693
/// and returns a mutable reference to the value in the entry.
694-
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
694+
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
695695
match self {
696696
Occupied(entry) => entry.into_mut(),
697697
Vacant(entry) => entry.insert(default()),

src/librustc/metadata/loader.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ 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).default_with(|| (HashMap::new(), HashMap::new()));
429+
let slot = candidates.entry(hash_str)
430+
.or_insert_with(|| (HashMap::new(), HashMap::new()));
430431
let (ref mut rlibs, ref mut dylibs) = *slot;
431432
if rlib {
432433
rlibs.insert(fs::realpath(path).unwrap(), kind);

src/librustc/middle/dataflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +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-
index.entry(id).default(vec![]).push(node_idx);
163+
index.entry(id).or_insert(vec![]).push(node_idx);
164164
}
165165
true
166166
});
@@ -180,7 +180,7 @@ fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,
180180
visit::walk_fn_decl(&mut formals, decl);
181181
impl<'a, 'v> visit::Visitor<'v> for Formals<'a> {
182182
fn visit_pat(&mut self, p: &ast::Pat) {
183-
self.index.entry(p.id).default(vec![]).push(self.entry);
183+
self.index.entry(p.id).or_insert(vec![]).push(self.entry);
184184
visit::walk_pat(self, p)
185185
}
186186
}

src/librustc/middle/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn register_region_obligation<'tcx>(tcx: &ty::ctxt<'tcx>,
436436
debug!("register_region_obligation({})",
437437
region_obligation.repr(tcx));
438438

439-
region_obligations.entry(region_obligation.cause.body_id).default(vec![])
439+
region_obligations.entry(region_obligation.cause.body_id).or_insert(vec![])
440440
.push(region_obligation);
441441

442442
}

src/librustc/middle/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5669,7 +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-
tcache.entry(id).default_with(|| csearch::get_field_type(tcx, struct_id, id)).ty
5672+
tcache.entry(id).or_insert_with(|| csearch::get_field_type(tcx, struct_id, id)).ty
56735673
};
56745674
ty.subst(tcx, substs)
56755675
}
@@ -6817,7 +6817,7 @@ pub fn replace_late_bound_regions<'tcx, T, F>(
68176817
debug!("region={}", region.repr(tcx));
68186818
match region {
68196819
ty::ReLateBound(debruijn, br) if debruijn.depth == current_depth => {
6820-
let region = *map.entry(br).default_with(|| mapf(br));
6820+
let region = *map.entry(br).or_insert_with(|| mapf(br));
68216821

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

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10361036
None => early_error("--extern value must be of the format `foo=bar`"),
10371037
};
10381038

1039-
externs.entry(name.to_string()).default(vec![]).push(location.to_string());
1039+
externs.entry(name.to_string()).or_insert(vec![]).push(location.to_string());
10401040
}
10411041

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

src/librustc_resolve/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#![feature(rustc_diagnostic_macros)]
2727
#![feature(rustc_private)]
2828
#![feature(staged_api)]
29-
#![feature(std_misc)]
3029

3130
#[macro_use] extern crate log;
3231
#[macro_use] extern crate syntax;

src/librustc_resolve/resolve_imports.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
836836
let is_public = import_directive.is_public;
837837

838838
let mut import_resolutions = module_.import_resolutions.borrow_mut();
839-
let dest_import_resolution = import_resolutions.entry(name).get().unwrap_or_else(
840-
|vacant_entry| {
841-
// Create a new import resolution from this child.
842-
vacant_entry.insert(ImportResolution::new(id, is_public))
843-
});
839+
let dest_import_resolution = import_resolutions.entry(name)
840+
.or_insert_with(|| ImportResolution::new(id, is_public));
844841

845842
debug!("(resolving glob import) writing resolution `{}` in `{}` \
846843
to `{}`",

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13611361
closure_def_id: ast::DefId,
13621362
r: DeferredCallResolutionHandler<'tcx>) {
13631363
let mut deferred_call_resolutions = self.inh.deferred_call_resolutions.borrow_mut();
1364-
deferred_call_resolutions.entry(closure_def_id).default(vec![]).push(r);
1364+
deferred_call_resolutions.entry(closure_def_id).or_insert(vec![]).push(r);
13651365
}
13661366

13671367
fn remove_deferred_call_resolutions(&self,

src/librustdoc/html/render.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -876,7 +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-
self.implementors.entry(did).default(vec![]).push(Implementor {
879+
self.implementors.entry(did).or_insert(vec![]).push(Implementor {
880880
def_id: item.def_id,
881881
generics: i.generics.clone(),
882882
trait_: i.trait_.as_ref().unwrap().clone(),
@@ -1078,7 +1078,7 @@ impl DocFolder for Cache {
10781078
};
10791079

10801080
if let Some(did) = did {
1081-
self.impls.entry(did).default(vec![]).push(Impl {
1081+
self.impls.entry(did).or_insert(vec![]).push(Impl {
10821082
impl_: i,
10831083
dox: dox,
10841084
stability: item.stability.clone(),
@@ -1330,7 +1330,7 @@ impl Context {
13301330
Some(ref s) => s.to_string(),
13311331
};
13321332
let short = short.to_string();
1333-
map.entry(short).default(vec![])
1333+
map.entry(short).or_insert(vec![])
13341334
.push((myname, Some(plain_summary_line(item.doc_value()))));
13351335
}
13361336

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
352352
}
353353
};
354354
let name = name.to_string();
355-
externs.entry(name).default(vec![]).push(location.to_string());
355+
externs.entry(name).or_insert(vec![]).push(location.to_string());
356356
}
357357
Ok(externs)
358358
}

src/libstd/collections/hash/map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,8 @@ impl<'a, K, V> Entry<'a, K, V> {
14891489
#[unstable(feature = "std_misc",
14901490
reason = "will soon be replaced by or_insert")]
14911491
#[deprecated(since = "1.0",
1492-
reason = "replaced with more ergonomic `default` and `default_with`")]
1492+
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
1493+
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
14931494
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
14941495
match self {
14951496
Occupied(entry) => Ok(entry.into_mut()),
@@ -1501,7 +1502,7 @@ impl<'a, K, V> Entry<'a, K, V> {
15011502
reason = "matches entry v3 specification, waiting for dust to settle")]
15021503
/// Ensures a value is in the entry by inserting the default if empty, and returns
15031504
/// a mutable reference to the value in the entry.
1504-
pub fn default(self, default: V) -> &'a mut V {
1505+
pub fn or_insert(self, default: V) -> &'a mut V {
15051506
match self {
15061507
Occupied(entry) => entry.into_mut(),
15071508
Vacant(entry) => entry.insert(default),
@@ -1512,7 +1513,7 @@ impl<'a, K, V> Entry<'a, K, V> {
15121513
reason = "matches entry v3 specification, waiting for dust to settle")]
15131514
/// Ensures a value is in the entry by inserting the result of the default function if empty,
15141515
/// and returns a mutable reference to the value in the entry.
1515-
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1516+
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
15161517
match self {
15171518
Occupied(entry) => entry.into_mut(),
15181519
Vacant(entry) => entry.insert(default()),

src/libstd/collections/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@
307307
//! let message = "she sells sea shells by the sea shore";
308308
//!
309309
//! for c in message.chars() {
310-
//! *count.entry(c).default(0) += 1;
310+
//! *count.entry(c).or_insert(0) += 1;
311311
//! }
312312
//!
313313
//! assert_eq!(count.get(&'s'), Some(&8));
@@ -340,7 +340,7 @@
340340
//! for id in orders.into_iter() {
341341
//! // If this is the first time we've seen this customer, initialize them
342342
//! // with no blood alcohol. Otherwise, just retrieve them.
343-
//! let person = blood_alcohol.entry(id).default(Person{id: id, blood_alcohol: 0.0});
343+
//! let person = blood_alcohol.entry(id).or_insert(Person{id: id, blood_alcohol: 0.0});
344344
//!
345345
//! // Reduce their blood alcohol level. It takes time to order and drink a beer!
346346
//! person.blood_alcohol *= 0.9;

src/libsyntax/ext/mtwt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn apply_mark(m: Mrk, ctxt: SyntaxContext) -> SyntaxContext {
6767
fn apply_mark_internal(m: Mrk, ctxt: SyntaxContext, table: &SCTable) -> SyntaxContext {
6868
let key = (ctxt, m);
6969
* table.mark_memo.borrow_mut().entry(key)
70-
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))
70+
.or_insert_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))
7171
}
7272

7373
/// Extend a syntax context with a given rename
@@ -84,7 +84,7 @@ fn apply_rename_internal(id: Ident,
8484
let key = (ctxt, id, to);
8585

8686
* table.rename_memo.borrow_mut().entry(key)
87-
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))
87+
.or_insert_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))
8888
}
8989

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

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#![feature(quote, unsafe_destructor)]
3636
#![feature(rustc_private)]
3737
#![feature(staged_api)]
38-
#![feature(std_misc)]
3938
#![feature(unicode)]
4039
#![feature(path_ext)]
4140
#![feature(str_char)]

0 commit comments

Comments
 (0)