Skip to content

Commit

Permalink
default => or_insert per RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Mar 27, 2015
1 parent 93cdf1f commit 1b98f6d
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
#[unstable(feature = "std_misc",
reason = "will soon be replaced by or_insert")]
#[deprecated(since = "1.0",
reason = "replaced with more ergonomic `default` and `default_with`")]
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
match self {
Expand All @@ -1159,7 +1159,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
pub fn default(self, default: V) -> &'a mut V {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
Expand All @@ -1170,7 +1170,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
Expand Down Expand Up @@ -1592,7 +1592,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
///
/// // count the number of occurrences of letters in the vec
/// for x in vec!["a","b","a","c","a","b"] {
/// *count.entry(x).default(0) += 1;
/// *count.entry(x).or_insert(0) += 1;
/// }
///
/// assert_eq!(count["a"], 3);
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl<V> VecMap<V> {
///
/// // count the number of occurrences of numbers in the vec
/// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
/// *count.entry(x).default(0) += 1;
/// *count.entry(x).or_insert(0) += 1;
/// }
///
/// assert_eq!(count[1], 3);
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<'a, V> Entry<'a, V> {
#[unstable(feature = "collections",
reason = "will soon be replaced by or_insert")]
#[deprecated(since = "1.0",
reason = "replaced with more ergonomic `default` and `default_with`")]
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> {
match self {
Expand All @@ -680,7 +680,7 @@ impl<'a, V> Entry<'a, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
pub fn default(self, default: V) -> &'a mut V {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
Expand All @@ -691,7 +691,7 @@ impl<'a, V> Entry<'a, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ impl<'a> Context<'a> {
info!("lib candidate: {}", path.display());

let hash_str = hash.to_string();
let slot = candidates.entry(hash_str).default_with(|| (HashMap::new(), HashMap::new()));
let slot = candidates.entry(hash_str)
.or_insert_with(|| (HashMap::new(), HashMap::new()));
let (ref mut rlibs, ref mut dylibs) = *slot;
if rlib {
rlibs.insert(fs::realpath(path).unwrap(), kind);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,

cfg.graph.each_node(|node_idx, node| {
if let cfg::CFGNodeData::AST(id) = node.data {
index.entry(id).default(vec![]).push(node_idx);
index.entry(id).or_insert(vec![]).push(node_idx);
}
true
});
Expand All @@ -180,7 +180,7 @@ fn build_nodeid_to_index(decl: Option<&ast::FnDecl>,
visit::walk_fn_decl(&mut formals, decl);
impl<'a, 'v> visit::Visitor<'v> for Formals<'a> {
fn visit_pat(&mut self, p: &ast::Pat) {
self.index.entry(p.id).default(vec![]).push(self.entry);
self.index.entry(p.id).or_insert(vec![]).push(self.entry);
visit::walk_pat(self, p)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn register_region_obligation<'tcx>(tcx: &ty::ctxt<'tcx>,
debug!("register_region_obligation({})",
region_obligation.repr(tcx));

region_obligations.entry(region_obligation.cause.body_id).default(vec![])
region_obligations.entry(region_obligation.cause.body_id).or_insert(vec![])
.push(region_obligation);

}
4 changes: 2 additions & 2 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5669,7 +5669,7 @@ pub fn lookup_field_type<'tcx>(tcx: &ctxt<'tcx>,
node_id_to_type(tcx, id.node)
} else {
let mut tcache = tcx.tcache.borrow_mut();
tcache.entry(id).default_with(|| csearch::get_field_type(tcx, struct_id, id)).ty
tcache.entry(id).or_insert_with(|| csearch::get_field_type(tcx, struct_id, id)).ty
};
ty.subst(tcx, substs)
}
Expand Down Expand Up @@ -6817,7 +6817,7 @@ pub fn replace_late_bound_regions<'tcx, T, F>(
debug!("region={}", region.repr(tcx));
match region {
ty::ReLateBound(debruijn, br) if debruijn.depth == current_depth => {
let region = *map.entry(br).default_with(|| mapf(br));
let region = *map.entry(br).or_insert_with(|| mapf(br));

if let ty::ReLateBound(debruijn1, br) = region {
// If the callback returns a late-bound region,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
None => early_error("--extern value must be of the format `foo=bar`"),
};

externs.entry(name.to_string()).default(vec![]).push(location.to_string());
externs.entry(name.to_string()).or_insert(vec![]).push(location.to_string());
}

let crate_name = matches.opt_str("crate-name");
Expand Down
1 change: 0 additions & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#![feature(rustc_diagnostic_macros)]
#![feature(rustc_private)]
#![feature(staged_api)]
#![feature(std_misc)]

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
let is_public = import_directive.is_public;

let mut import_resolutions = module_.import_resolutions.borrow_mut();
let dest_import_resolution = import_resolutions.entry(name).get().unwrap_or_else(
|vacant_entry| {
// Create a new import resolution from this child.
vacant_entry.insert(ImportResolution::new(id, is_public))
});
let dest_import_resolution = import_resolutions.entry(name)
.or_insert_with(|| ImportResolution::new(id, is_public));

debug!("(resolving glob import) writing resolution `{}` in `{}` \
to `{}`",
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
closure_def_id: ast::DefId,
r: DeferredCallResolutionHandler<'tcx>) {
let mut deferred_call_resolutions = self.inh.deferred_call_resolutions.borrow_mut();
deferred_call_resolutions.entry(closure_def_id).default(vec![]).push(r);
deferred_call_resolutions.entry(closure_def_id).or_insert(vec![]).push(r);
}

fn remove_deferred_call_resolutions(&self,
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ impl DocFolder for Cache {
if let clean::ImplItem(ref i) = item.inner {
match i.trait_ {
Some(clean::ResolvedPath{ did, .. }) => {
self.implementors.entry(did).default(vec![]).push(Implementor {
self.implementors.entry(did).or_insert(vec![]).push(Implementor {
def_id: item.def_id,
generics: i.generics.clone(),
trait_: i.trait_.as_ref().unwrap().clone(),
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl DocFolder for Cache {
};

if let Some(did) = did {
self.impls.entry(did).default(vec![]).push(Impl {
self.impls.entry(did).or_insert(vec![]).push(Impl {
impl_: i,
dox: dox,
stability: item.stability.clone(),
Expand Down Expand Up @@ -1330,7 +1330,7 @@ impl Context {
Some(ref s) => s.to_string(),
};
let short = short.to_string();
map.entry(short).default(vec![])
map.entry(short).or_insert(vec![])
.push((myname, Some(plain_summary_line(item.doc_value()))));
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
}
};
let name = name.to_string();
externs.entry(name).default(vec![]).push(location.to_string());
externs.entry(name).or_insert(vec![]).push(location.to_string());
}
Ok(externs)
}
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,8 @@ impl<'a, K, V> Entry<'a, K, V> {
#[unstable(feature = "std_misc",
reason = "will soon be replaced by or_insert")]
#[deprecated(since = "1.0",
reason = "replaced with more ergonomic `default` and `default_with`")]
reason = "replaced with more ergonomic `or_insert` and `or_insert_with`")]
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
match self {
Occupied(entry) => Ok(entry.into_mut()),
Expand All @@ -1501,7 +1502,7 @@ impl<'a, K, V> Entry<'a, K, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
pub fn default(self, default: V) -> &'a mut V {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
Expand All @@ -1512,7 +1513,7 @@ impl<'a, K, V> Entry<'a, K, V> {
reason = "matches entry v3 specification, waiting for dust to settle")]
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
//! let message = "she sells sea shells by the sea shore";
//!
//! for c in message.chars() {
//! *count.entry(c).default(0) += 1;
//! *count.entry(c).or_insert(0) += 1;
//! }
//!
//! assert_eq!(count.get(&'s'), Some(&8));
Expand Down Expand Up @@ -340,7 +340,7 @@
//! for id in orders.into_iter() {
//! // If this is the first time we've seen this customer, initialize them
//! // with no blood alcohol. Otherwise, just retrieve them.
//! let person = blood_alcohol.entry(id).default(Person{id: id, blood_alcohol: 0.0});
//! let person = blood_alcohol.entry(id).or_insert(Person{id: id, blood_alcohol: 0.0});
//!
//! // Reduce their blood alcohol level. It takes time to order and drink a beer!
//! person.blood_alcohol *= 0.9;
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/mtwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn apply_mark(m: Mrk, ctxt: SyntaxContext) -> SyntaxContext {
fn apply_mark_internal(m: Mrk, ctxt: SyntaxContext, table: &SCTable) -> SyntaxContext {
let key = (ctxt, m);
* table.mark_memo.borrow_mut().entry(key)
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))
.or_insert_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))
}

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

* table.rename_memo.borrow_mut().entry(key)
.default_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))
.or_insert_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))
}

/// Apply a list of renamings to a context
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#![feature(quote, unsafe_destructor)]
#![feature(rustc_private)]
#![feature(staged_api)]
#![feature(std_misc)]
#![feature(unicode)]
#![feature(path_ext)]
#![feature(str_char)]
Expand Down

0 comments on commit 1b98f6d

Please sign in to comment.