Skip to content

std: Revert stability of Entry-based APIs #20653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::Entry::*;

use core::prelude::*;

use core::borrow::{BorrowFrom, ToOwned};
use core::borrow::BorrowFrom;
use core::cmp::Ordering;
use core::default::Default;
use core::fmt::Show;
Expand Down Expand Up @@ -128,24 +128,24 @@ pub struct Values<'a, K: 'a, V: 'a> {
inner: Map<(&'a K, &'a V), &'a V, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
}

#[stable]
/// A view into a single entry in a map, which may either be vacant or occupied.
pub enum Entry<'a, Q: ?Sized +'a, K:'a, V:'a> {
#[unstable = "precise API still under development"]
pub enum Entry<'a, K:'a, V:'a> {
/// A vacant Entry
Vacant(VacantEntry<'a, Q, K, V>),
Vacant(VacantEntry<'a, K, V>),
/// An occupied Entry
Occupied(OccupiedEntry<'a, K, V>),
}

#[stable]
/// A vacant Entry.
pub struct VacantEntry<'a, Q: ?Sized +'a, K:'a, V:'a> {
key: &'a Q,
#[unstable = "precise API still under development"]
pub struct VacantEntry<'a, K:'a, V:'a> {
key: K,
stack: stack::SearchStack<'a, K, V, node::handle::Edge, node::handle::Leaf>,
}

#[stable]
/// An occupied Entry.
#[unstable = "precise API still under development"]
pub struct OccupiedEntry<'a, K:'a, V:'a> {
stack: stack::SearchStack<'a, K, V, node::handle::KV, node::handle::LeafOrInternal>,
}
Expand Down Expand Up @@ -1111,55 +1111,55 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
#[stable]
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}

impl<'a, Q: ?Sized, K: Ord, V> Entry<'a, Q, K, V> {
impl<'a, K: Ord, V> Entry<'a, K, V> {
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, Q, K, V>> {
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
match self {
Occupied(entry) => Ok(entry.into_mut()),
Vacant(entry) => Err(entry),
}
}
}

impl<'a, Q: ?Sized + ToOwned<K>, K: Ord, V> VacantEntry<'a, Q, K, V> {
#[stable]
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
/// Sets the value of the entry with the VacantEntry's key,
/// and returns a mutable reference to it.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn insert(self, value: V) -> &'a mut V {
self.stack.insert(self.key.to_owned(), value)
self.stack.insert(self.key, value)
}
}

impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
#[stable]
/// Gets a reference to the value in the entry.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn get(&self) -> &V {
self.stack.peek()
}

#[stable]
/// Gets a mutable reference to the value in the entry.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn get_mut(&mut self) -> &mut V {
self.stack.peek_mut()
}

#[stable]
/// Converts the entry into a mutable reference to its value.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn into_mut(self) -> &'a mut V {
self.stack.into_top()
}

#[stable]
/// Sets the value of the entry with the OccupiedEntry's key,
/// and returns the entry's old value.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn insert(&mut self, mut value: V) -> V {
mem::swap(self.stack.peek_mut(), &mut value);
value
}

#[stable]
/// Takes the value of the entry out of the map, and returns it.
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
pub fn remove(self) -> V {
self.stack.remove()
}
Expand Down Expand Up @@ -1361,15 +1361,13 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// assert_eq!(count["a"], 3u);
/// ```
/// The key must have the same ordering before or after `.to_owned()` is called.
#[stable]
pub fn entry<'a, Q: ?Sized>(&'a mut self, mut key: &'a Q) -> Entry<'a, Q, K, V>
where Q: Ord + ToOwned<K>
{
#[unstable = "precise API still under development"]
pub fn entry<'a>(&'a mut self, mut key: K) -> Entry<'a, K, V> {
// same basic logic of `swap` and `pop`, blended together
let mut stack = stack::PartialSearchStack::new(self);
loop {
let result = stack.with(move |pusher, node| {
return match Node::search(node, key) {
return match Node::search(node, &key) {
Found(handle) => {
// Perfect match
Finished(Occupied(OccupiedEntry {
Expand Down Expand Up @@ -1412,7 +1410,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
#[cfg(test)]
mod test {
use prelude::*;
use std::borrow::{ToOwned, BorrowFrom};
use std::borrow::BorrowFrom;

use super::{BTreeMap, Occupied, Vacant};

Expand Down Expand Up @@ -1562,7 +1560,7 @@ mod test {
let mut map: BTreeMap<int, int> = xs.iter().map(|&x| x).collect();

// Existing key (insert)
match map.entry(&1) {
match map.entry(1) {
Vacant(_) => unreachable!(),
Occupied(mut view) => {
assert_eq!(view.get(), &10);
Expand All @@ -1574,7 +1572,7 @@ mod test {


// Existing key (update)
match map.entry(&2) {
match map.entry(2) {
Vacant(_) => unreachable!(),
Occupied(mut view) => {
let v = view.get_mut();
Expand All @@ -1585,7 +1583,7 @@ mod test {
assert_eq!(map.len(), 6);

// Existing key (take)
match map.entry(&3) {
match map.entry(3) {
Vacant(_) => unreachable!(),
Occupied(view) => {
assert_eq!(view.remove(), 30);
Expand All @@ -1596,7 +1594,7 @@ mod test {


// Inexistent key (insert)
match map.entry(&10) {
match map.entry(10) {
Occupied(_) => unreachable!(),
Vacant(view) => {
assert_eq!(*view.insert(1000), 1000);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ impl UnusedMut {
let ident = path1.node;
if let ast::BindByValue(ast::MutMutable) = mode {
if !token::get_ident(ident).get().starts_with("_") {
match mutables.entry(&ident.name.uint()) {
match mutables.entry(ident.name.uint()) {
Vacant(entry) => { entry.insert(vec![id]); },
Occupied(mut entry) => { entry.get_mut().push(id); },
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,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(
let slot = candidates.entry(hash_str).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert((HashSet::new(), HashSet::new())));
let (ref mut rlibs, ref mut dylibs) = *slot;
if rlib {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<ast::Pat> {

ast::ExprCall(ref callee, ref args) => {
let def = tcx.def_map.borrow()[callee.id].clone();
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(&expr.id) {
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
entry.insert(def);
}
let path = match def {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
None => { }
}

match self.freshen_map.entry(&key) {
match self.freshen_map.entry(key) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let index = self.freshen_count;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/infer/region_inference/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a, 'tcx> ConstraintGraph<'a, 'tcx> {
let mut node_ids = FnvHashMap::new();
{
let mut add_node = |&mut : node| {
if let Vacant(e) = node_ids.entry(&node) {
if let Vacant(e) = node_ids.entry(node) {
e.insert(i);
i += 1;
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,9 @@ fn register_region_obligation<'tcx>(tcx: &ty::ctxt<'tcx>,
debug!("register_region_obligation({})",
region_obligation.repr(tcx));

let body_id = region_obligation.cause.body_id;
match region_obligations.entry(&body_id) {
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); },
}

}

4 changes: 2 additions & 2 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5584,7 +5584,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(
let pty = tcache.entry(id).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(csearch::get_field_type(tcx, struct_id, id)));
pty.ty
};
Expand Down Expand Up @@ -6747,7 +6747,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 => {
* map.entry(&br).get().unwrap_or_else(
* map.entry(br).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(mapf(br, debruijn)))
}
_ => {
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 @@ -1115,7 +1115,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()) {
match externs.entry(name.to_string()) {
Vacant(entry) => { entry.insert(vec![location.to_string()]); },
Occupied(mut entry) => { entry.get_mut().push(location.to_string()); },
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ impl<'a, 'tcx> Resolver<'a, '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(
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))
Expand Down Expand Up @@ -2639,14 +2639,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
def = DefUpvar(node_id, function_id, last_proc_body_id);

let mut seen = self.freevars_seen.borrow_mut();
let seen = match seen.entry(&function_id) {
let seen = match seen.entry(function_id) {
Occupied(v) => v.into_mut(),
Vacant(v) => v.insert(NodeSet::new()),
};
if seen.contains(&node_id) {
continue;
}
match self.freevars.borrow_mut().entry(&function_id) {
match self.freevars.borrow_mut().entry(function_id) {
Occupied(v) => v.into_mut(),
Vacant(v) => v.insert(vec![]),
}.push(Freevar { def: prev_def, span: span });
Expand Down Expand Up @@ -4723,7 +4723,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
"Import should only be used for `use` directives");
self.last_private.insert(node_id, lp);

match self.def_map.borrow_mut().entry(&node_id) {
match self.def_map.borrow_mut().entry(node_id) {
// Resolve appears to "resolve" the same ID multiple
// times, so here is a sanity check it at least comes to
// the same conclusion! - nmatsakis
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ pub fn check_struct_pat_fields<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,

// Typecheck each field.
for &Spanned { node: ref field, span } in fields.iter() {
let field_type = match used_fields.entry(&field.ident.name) {
let field_type = match used_fields.entry(field.ident.name) {
Occupied(occupied) => {
span_err!(tcx.sess, span, E0025,
"field `{}` bound multiple times in the pattern",
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 @@ -821,7 +821,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(
let v = self.implementors.entry(did).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
v.push(Implementor {
def_id: item.def_id,
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl DocFolder for Cache {
};

if let Some(did) = did {
let v = self.impls.entry(&did).get().unwrap_or_else(
let v = self.impls.entry(did).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
v.push(Impl {
impl_: i,
Expand Down Expand Up @@ -1260,7 +1260,7 @@ impl Context {
Some(ref s) => s.to_string(),
};
let short = short.to_string();
let v = map.entry(&short).get().unwrap_or_else(
let v = map.entry(short).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
v.push(myname);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
}
};
let name = name.to_string();
let locs = externs.entry(&name).get().unwrap_or_else(
let locs = externs.entry(name).get().unwrap_or_else(
|vacant_entry| vacant_entry.insert(Vec::with_capacity(1)));
locs.push(location.to_string());
}
Expand Down
Loading