From 1c35953cf8baa2e721401ba6354f0bd9a9f2abaf Mon Sep 17 00:00:00 2001 From: Alexis Date: Sun, 1 Mar 2015 09:50:08 -0500 Subject: [PATCH 1/3] entry API v3: replace Entry::get with Entry::default and Entry::default_with --- src/libcollections/btree/map.rs | 26 +++++++++++++++++++++++++- src/libcollections/vec_map.rs | 24 ++++++++++++++++++++++++ src/libstd/collections/hash/map.rs | 26 +++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index c2f6fbc0b2602..ee4b66b043ce9 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1143,15 +1143,39 @@ impl<'a, K, V> DoubleEndedIterator for RangeMut<'a, K, V> { } impl<'a, K: Ord, V> Entry<'a, K, V> { - /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant #[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`")] + /// 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()), Vacant(entry) => Err(entry), } } + + #[unstable(feature = "collections", + 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 { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default), + } + } + + #[unstable(feature = "collections", + 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 V>(self, default: F) -> &'a mut V { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default()), + } + } } impl<'a, K: Ord, V> VacantEntry<'a, K, V> { diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index c994064d34724..c73caccef8053 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -675,6 +675,8 @@ impl VecMap { 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`")] /// 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 { @@ -682,6 +684,28 @@ impl<'a, V> Entry<'a, V> { Vacant(entry) => Err(entry), } } + + #[unstable(feature = "collections", + 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 { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default), + } + } + + #[unstable(feature = "collections", + 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 V>(self, default: F) -> &'a mut V { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default()), + } + } } impl<'a, V> VacantEntry<'a, V> { diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index f9558b85825d2..b3557529a668f 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -23,7 +23,7 @@ use hash::{Hash, SipHasher}; use iter::{self, Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator, Extend, Map}; use marker::Sized; use mem::{self, replace}; -use ops::{Deref, FnMut, Index}; +use ops::{Deref, FnMut, FnOnce, Index}; use option::Option::{self, Some, None}; use rand::{self, Rng}; use result::Result::{self, Ok, Err}; @@ -1488,12 +1488,36 @@ impl<'a, K, V> Entry<'a, K, V> { /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant. #[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`")] pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> { match self { Occupied(entry) => Ok(entry.into_mut()), Vacant(entry) => Err(entry), } } + + #[unstable(feature = "collections", + 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 { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default), + } + } + + #[unstable(feature = "collections", + 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 V>(self, default: F) -> &'a mut V { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default()), + } + } } impl<'a, K, V> OccupiedEntry<'a, K, V> { From 93cdf1f2783e3a863929a5ef2032e7de752e4e40 Mon Sep 17 00:00:00 2001 From: Alexis Date: Sun, 1 Mar 2015 09:42:11 -0500 Subject: [PATCH 2/3] update everything to use Entry defaults --- src/libcollections/btree/map.rs | 13 ++----------- src/libcollections/vec_map.rs | 13 ++----------- src/librustc/metadata/loader.rs | 3 +-- src/librustc/middle/dataflow.rs | 14 ++------------ src/librustc/middle/traits/fulfill.rs | 7 ++----- src/librustc/middle/ty.rs | 8 ++------ src/librustc/session/config.rs | 6 +----- src/librustc_typeck/check/mod.rs | 7 +------ src/librustdoc/html/render.rs | 13 ++++--------- src/librustdoc/lib.rs | 4 +--- src/libstd/collections/mod.rs | 10 ++-------- src/libsyntax/ext/mtwt.rs | 10 ++++------ 12 files changed, 24 insertions(+), 84 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index ee4b66b043ce9..1c484d5dd5eea 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1587,21 +1587,12 @@ impl BTreeMap { /// ``` /// # #![feature(collections)] /// use std::collections::BTreeMap; - /// use std::collections::btree_map::Entry; /// /// let mut count: BTreeMap<&str, usize> = BTreeMap::new(); /// /// // count the number of occurrences of letters in the vec - /// for x in vec!["a","b","a","c","a","b"].iter() { - /// match count.entry(*x) { - /// Entry::Vacant(view) => { - /// view.insert(1); - /// }, - /// Entry::Occupied(mut view) => { - /// let v = view.get_mut(); - /// *v += 1; - /// }, - /// } + /// for x in vec!["a","b","a","c","a","b"] { + /// *count.entry(x).default(0) += 1; /// } /// /// assert_eq!(count["a"], 3); diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index c73caccef8053..94dd997947c39 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -632,21 +632,12 @@ impl VecMap { /// ``` /// # #![feature(collections)] /// use std::collections::VecMap; - /// use std::collections::vec_map::Entry; /// /// let mut count: VecMap = VecMap::new(); /// /// // count the number of occurrences of numbers in the vec - /// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4].iter() { - /// match count.entry(*x) { - /// Entry::Vacant(view) => { - /// view.insert(1); - /// }, - /// Entry::Occupied(mut view) => { - /// let v = view.get_mut(); - /// *v += 1; - /// }, - /// } + /// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] { + /// *count.entry(x).default(0) += 1; /// } /// /// assert_eq!(count[1], 3); diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs index 7854db8114665..a36d425c03e58 100644 --- a/src/librustc/metadata/loader.rs +++ b/src/librustc/metadata/loader.rs @@ -426,8 +426,7 @@ impl<'a> Context<'a> { info!("lib candidate: {}", path.display()); let hash_str = hash.to_string(); - let slot = candidates.entry(hash_str).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert((HashMap::new(), HashMap::new()))); + let slot = candidates.entry(hash_str).default_with(|| (HashMap::new(), HashMap::new())); let (ref mut rlibs, ref mut dylibs) = *slot; if rlib { rlibs.insert(fs::realpath(path).unwrap(), kind); diff --git a/src/librustc/middle/dataflow.rs b/src/librustc/middle/dataflow.rs index a1ce8d18184e3..71b36e21d3e6f 100644 --- a/src/librustc/middle/dataflow.rs +++ b/src/librustc/middle/dataflow.rs @@ -160,12 +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 { - match index.entry(id).get() { - Ok(v) => v.push(node_idx), - Err(e) => { - e.insert(vec![node_idx]); - } - } + index.entry(id).default(vec![]).push(node_idx); } true }); @@ -185,12 +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) { - match self.index.entry(p.id).get() { - Ok(v) => v.push(self.entry), - Err(e) => { - e.insert(vec![self.entry]); - } - } + self.index.entry(p.id).default(vec![]).push(self.entry); visit::walk_pat(self, p) } } diff --git a/src/librustc/middle/traits/fulfill.rs b/src/librustc/middle/traits/fulfill.rs index c1066aa899eae..76050d53a7448 100644 --- a/src/librustc/middle/traits/fulfill.rs +++ b/src/librustc/middle/traits/fulfill.rs @@ -11,7 +11,6 @@ use middle::infer::{InferCtxt}; use middle::ty::{self, RegionEscape, Ty}; use std::collections::HashSet; -use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::default::Default; use syntax::ast; use util::common::ErrorReported; @@ -437,9 +436,7 @@ fn register_region_obligation<'tcx>(tcx: &ty::ctxt<'tcx>, debug!("register_region_obligation({})", region_obligation.repr(tcx)); - match region_obligations.entry(region_obligation.cause.body_id) { - Vacant(entry) => { entry.insert(vec![region_obligation]); }, - Occupied(mut entry) => { entry.get_mut().push(region_obligation); }, - } + region_obligations.entry(region_obligation.cause.body_id).default(vec![]) + .push(region_obligation); } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 92b444e85d8c3..975d1560d6cfb 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -5669,9 +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(); - let pty = tcache.entry(id).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(csearch::get_field_type(tcx, struct_id, id))); - pty.ty + tcache.entry(id).default_with(|| csearch::get_field_type(tcx, struct_id, id)).ty }; ty.subst(tcx, substs) } @@ -6819,9 +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).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(mapf(br))); + let region = *map.entry(br).default_with(|| mapf(br)); if let ty::ReLateBound(debruijn1, br) = region { // If the callback returns a late-bound region, diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index a7c67a0863182..5f0ab10488557 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -35,7 +35,6 @@ use syntax::parse::token::InternedString; use getopts; use std::collections::HashMap; -use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::env; use std::fmt; use std::path::PathBuf; @@ -1037,10 +1036,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { None => early_error("--extern value must be of the format `foo=bar`"), }; - match externs.entry(name.to_string()) { - Vacant(entry) => { entry.insert(vec![location.to_string()]); }, - Occupied(mut entry) => { entry.get_mut().push(location.to_string()); }, - } + externs.entry(name.to_string()).default(vec![]).push(location.to_string()); } let crate_name = matches.opt_str("crate-name"); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 1e38a7d2d9f94..4aa5f09c1427c 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -112,7 +112,6 @@ use util::nodemap::{DefIdMap, FnvHashMap, NodeMap}; use util::lev_distance::lev_distance; use std::cell::{Cell, Ref, RefCell}; -use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::mem::replace; use std::rc::Rc; use std::iter::repeat; @@ -1362,11 +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(); - let mut vec = match deferred_call_resolutions.entry(closure_def_id) { - Occupied(entry) => entry.into_mut(), - Vacant(entry) => entry.insert(Vec::new()), - }; - vec.push(r); + deferred_call_resolutions.entry(closure_def_id).default(vec![]).push(r); } fn remove_deferred_call_resolutions(&self, diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index d57739c400249..5ca31ae26e126 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -876,9 +876,7 @@ impl DocFolder for Cache { if let clean::ImplItem(ref i) = item.inner { match i.trait_ { Some(clean::ResolvedPath{ did, .. }) => { - let v = self.implementors.entry(did).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(Vec::with_capacity(1))); - v.push(Implementor { + self.implementors.entry(did).default(vec![]).push(Implementor { def_id: item.def_id, generics: i.generics.clone(), trait_: i.trait_.as_ref().unwrap().clone(), @@ -1080,9 +1078,7 @@ impl DocFolder for Cache { }; if let Some(did) = did { - let v = self.impls.entry(did).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(Vec::with_capacity(1))); - v.push(Impl { + self.impls.entry(did).default(vec![]).push(Impl { impl_: i, dox: dox, stability: item.stability.clone(), @@ -1334,9 +1330,8 @@ impl Context { Some(ref s) => s.to_string(), }; let short = short.to_string(); - let v = map.entry(short).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(Vec::with_capacity(1))); - v.push((myname, Some(plain_summary_line(item.doc_value())))); + map.entry(short).default(vec![]) + .push((myname, Some(plain_summary_line(item.doc_value())))); } for (_, items) in &mut map { diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 9f1d876432c38..4db5a262c2ccf 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -352,9 +352,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result { } }; let name = name.to_string(); - let locs = externs.entry(name).get().unwrap_or_else( - |vacant_entry| vacant_entry.insert(Vec::with_capacity(1))); - locs.push(location.to_string()); + externs.entry(name).default(vec![]).push(location.to_string()); } Ok(externs) } diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index 8d24f6b191659..b51948c160b55 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -307,10 +307,7 @@ //! let message = "she sells sea shells by the sea shore"; //! //! for c in message.chars() { -//! match count.entry(c) { -//! Entry::Vacant(entry) => { entry.insert(1); }, -//! Entry::Occupied(mut entry) => *entry.get_mut() += 1, -//! } +//! *count.entry(c).default(0) += 1; //! } //! //! assert_eq!(count.get(&'s'), Some(&8)); @@ -343,10 +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 = match blood_alcohol.entry(id) { -//! Entry::Vacant(entry) => entry.insert(Person{id: id, blood_alcohol: 0.0}), -//! Entry::Occupied(entry) => entry.into_mut(), -//! }; +//! let person = blood_alcohol.entry(id).default(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; diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index 72431d8e6aa2c..2b811ce914f7e 100644 --- a/src/libsyntax/ext/mtwt.rs +++ b/src/libsyntax/ext/mtwt.rs @@ -66,9 +66,8 @@ pub fn apply_mark(m: Mrk, ctxt: SyntaxContext) -> SyntaxContext { /// Extend a syntax context with a given mark and sctable (explicit memoization) fn apply_mark_internal(m: Mrk, ctxt: SyntaxContext, table: &SCTable) -> SyntaxContext { let key = (ctxt, m); - * table.mark_memo.borrow_mut().entry(key).get().unwrap_or_else( - |vacant_entry| - vacant_entry.insert(idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt)))) + * table.mark_memo.borrow_mut().entry(key) + .default_with(|| idx_push(&mut *table.table.borrow_mut(), Mark(m, ctxt))) } /// Extend a syntax context with a given rename @@ -84,9 +83,8 @@ fn apply_rename_internal(id: Ident, table: &SCTable) -> SyntaxContext { let key = (ctxt, id, to); - * table.rename_memo.borrow_mut().entry(key).get().unwrap_or_else( - |vacant_entry| - vacant_entry.insert(idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt)))) + * table.rename_memo.borrow_mut().entry(key) + .default_with(|| idx_push(&mut *table.table.borrow_mut(), Rename(id, to, ctxt))) } /// Apply a list of renamings to a context From 1b98f6da7af8cea31066588776b7190c511455b1 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Fri, 20 Mar 2015 13:43:01 -0400 Subject: [PATCH 3/3] default => or_insert per RFC --- src/libcollections/btree/map.rs | 8 ++++---- src/libcollections/vec_map.rs | 8 ++++---- src/librustc/metadata/loader.rs | 3 ++- src/librustc/middle/dataflow.rs | 4 ++-- src/librustc/middle/traits/fulfill.rs | 2 +- src/librustc/middle/ty.rs | 4 ++-- src/librustc/session/config.rs | 2 +- src/librustc_resolve/lib.rs | 1 - src/librustc_resolve/resolve_imports.rs | 7 ++----- src/librustc_typeck/check/mod.rs | 2 +- src/librustdoc/html/render.rs | 6 +++--- src/librustdoc/lib.rs | 2 +- src/libstd/collections/hash/map.rs | 7 ++++--- src/libstd/collections/mod.rs | 4 ++-- src/libsyntax/ext/mtwt.rs | 4 ++-- src/libsyntax/lib.rs | 1 - 16 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 1c484d5dd5eea..04a692cc3aea2 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -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 { @@ -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), @@ -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 V>(self, default: F) -> &'a mut V { + pub fn or_insert_with V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default()), @@ -1592,7 +1592,7 @@ impl BTreeMap { /// /// // 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); diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 94dd997947c39..58f9101d1d366 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -637,7 +637,7 @@ impl VecMap { /// /// // 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); @@ -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 { @@ -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), @@ -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 V>(self, default: F) -> &'a mut V { + pub fn or_insert_with V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default()), diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs index a36d425c03e58..80fc376945342 100644 --- a/src/librustc/metadata/loader.rs +++ b/src/librustc/metadata/loader.rs @@ -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); diff --git a/src/librustc/middle/dataflow.rs b/src/librustc/middle/dataflow.rs index 71b36e21d3e6f..0d58fd2702f68 100644 --- a/src/librustc/middle/dataflow.rs +++ b/src/librustc/middle/dataflow.rs @@ -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 }); @@ -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) } } diff --git a/src/librustc/middle/traits/fulfill.rs b/src/librustc/middle/traits/fulfill.rs index 76050d53a7448..3d46f93914a58 100644 --- a/src/librustc/middle/traits/fulfill.rs +++ b/src/librustc/middle/traits/fulfill.rs @@ -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); } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 975d1560d6cfb..23fba5ead2259 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -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) } @@ -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, diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 5f0ab10488557..931cfc7999281 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -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"); diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 566af2590e6c0..5bf561c218d04 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -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; diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 46451019760dd..662b5a366431c 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -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 `{}`", diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4aa5f09c1427c..0f9836bc07346 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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, diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 5ca31ae26e126..d36124247945b 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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(), @@ -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(), @@ -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())))); } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 4db5a262c2ccf..a85f770f63ca4 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -352,7 +352,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result { } }; 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) } diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index b3557529a668f..91225891338a5 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -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()), @@ -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), @@ -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 V>(self, default: F) -> &'a mut V { + pub fn or_insert_with V>(self, default: F) -> &'a mut V { match self { Occupied(entry) => entry.into_mut(), Vacant(entry) => entry.insert(default()), diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index b51948c160b55..0ac97b71298b8 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -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)); @@ -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; diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index 2b811ce914f7e..a2023d6832efb 100644 --- a/src/libsyntax/ext/mtwt.rs +++ b/src/libsyntax/ext/mtwt.rs @@ -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 @@ -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 diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 9af7b9ab63311..72498afa3201e 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -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)]