Skip to content

Commit

Permalink
stabilize core Entry API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Feb 5, 2015
1 parent ba2f13e commit 5cbbc12
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
26 changes: 9 additions & 17 deletions src/libcollections/btree/map.rs
Expand Up @@ -15,7 +15,7 @@
// writing (August 2014) freely licensed under the following Creative Commons Attribution
// License: [CC BY 2.5 CA](http://creativecommons.org/licenses/by/2.5/ca/).

pub use self::Entry::*;
use self::Entry::*;

use core::prelude::*;

Expand Down Expand Up @@ -1137,47 +1137,41 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
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(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V {
self.stack.insert(self.key, value)
}
}

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

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

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

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

/// Takes the value of the entry out of the map, and returns it.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove(self) -> V {
self.stack.remove()
}
Expand Down Expand Up @@ -1563,10 +1557,8 @@ 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.
#[unstable(feature = "collections",
reason = "precise API still under development")]
pub fn entry<'a>(&'a mut self, mut key: K) -> Entry<'a, K, V> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn entry(&mut self, mut key: K) -> Entry<K, V> {
// same basic logic of `swap` and `pop`, blended together
let mut stack = stack::PartialSearchStack::new(self);
loop {
Expand Down
23 changes: 8 additions & 15 deletions src/libcollections/vec_map.rs
Expand Up @@ -13,7 +13,7 @@

#![allow(missing_docs)]

pub use self::Entry::*;
use self::Entry::*;

use core::prelude::*;

Expand Down Expand Up @@ -539,8 +539,7 @@ impl<V> VecMap<V> {
///
/// assert_eq!(count[1], 3);
/// ```
#[unstable(feature = "collections",
reason = "precise API still under development")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn entry(&mut self, key: usize) -> Entry<V> {
// FIXME(Gankro): this is basically the dumbest implementation of
// entry possible, because weird non-lexical borrows issues make it
Expand Down Expand Up @@ -576,8 +575,7 @@ impl<'a, V> Entry<'a, V> {
impl<'a, V> VacantEntry<'a, V> {
/// Sets the value of the entry with the VacantEntry's key,
/// and returns a mutable reference to it.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V {
let index = self.index;
self.map.insert(index, value);
Expand All @@ -587,41 +585,36 @@ impl<'a, V> VacantEntry<'a, V> {

impl<'a, V> OccupiedEntry<'a, V> {
/// Gets a reference to the value in the entry.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> &V {
let index = self.index;
&self.map[index]
}

/// Gets a mutable reference to the value in the entry.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut V {
let index = self.index;
&mut self.map[index]
}

/// Converts the entry into a mutable reference to its value.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_mut(self) -> &'a mut V {
let index = self.index;
&mut self.map[index]
}

/// Sets the value of the entry with the OccupiedEntry's key,
/// and returns the entry's old value.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, value: V) -> V {
let index = self.index;
self.map.insert(index, value).unwrap()
}

/// Takes the value of the entry out of the map, and returns it.
#[unstable(feature = "collections",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove(self) -> V {
let index = self.index;
self.map.remove(&index).unwrap()
Expand Down
16 changes: 8 additions & 8 deletions src/libstd/collections/hash/map.rs
Expand Up @@ -929,10 +929,8 @@ impl<K, V, S, H> HashMap<K, V, S>
}

/// Gets the given key's corresponding entry in the map for in-place manipulation.
#[unstable(feature = "std_misc",
reason = "precise API still being fleshed out")]
pub fn entry<'a>(&'a mut self, key: K) -> Entry<'a, K, V>
{
#[stable(feature = "rust1", since = "1.0.0")]
pub fn entry(&mut self, key: K) -> Entry<K, V> {
// Gotta resize now.
self.reserve(1);

Expand Down Expand Up @@ -1496,43 +1494,45 @@ impl<'a, K, V> Entry<'a, K, V> {
}
}

#[unstable(feature = "std_misc",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
impl<'a, K, V> OccupiedEntry<'a, K, V> {
/// Gets a reference to the value in the entry.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> &V {
self.elem.read().1
}

/// Gets a mutable reference to the value in the entry.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut V {
self.elem.read_mut().1
}

/// Converts the OccupiedEntry into a mutable reference to the value in the entry
/// with a lifetime bound to the map itself
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_mut(self) -> &'a mut V {
self.elem.into_mut_refs().1
}

/// Sets the value of the entry, and returns the entry's old value
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, mut value: V) -> V {
let old_value = self.get_mut();
mem::swap(&mut value, old_value);
value
}

/// Takes the value out of the entry, and returns it
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove(self) -> V {
pop_internal(self.elem).1
}
}

#[unstable(feature = "std_misc",
reason = "matches collection reform v2 specification, waiting for dust to settle")]
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
/// Sets the value of the entry with the VacantEntry's key,
/// and returns a mutable reference to it
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V {
match self.elem {
NeqElem(bucket, ib) => {
Expand Down

0 comments on commit 5cbbc12

Please sign in to comment.