diff options
| author | Dylan Ede <dylanede@googlemail.com> | 2015-01-06 16:36:30 +0000 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 11:59:26 -0800 |
| commit | 25eada15740fbe12ee2cae7fc6fe8e2c228b699d (patch) | |
| tree | bc24dedaf97ab376c4ac4f73b4c40d068c5ef84b /src/libcollections | |
| parent | 6539cb417f4a7c2d9d1afce44c196578d2b67f38 (diff) | |
| download | rust-25eada15740fbe12ee2cae7fc6fe8e2c228b699d.tar.gz rust-25eada15740fbe12ee2cae7fc6fe8e2c228b699d.zip | |
[breaking change] Revert Entry behaviour to take keys by value.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/btree/map.rs | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index b85ea65f5ce..6e8b1a662ce 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -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; @@ -130,17 +130,17 @@ pub struct Values<'a, K: 'a, V: 'a> { #[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> { +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, +pub struct VacantEntry<'a, K:'a, V:'a> { + key: K, stack: stack::SearchStack<'a, K, V, node::handle::Edge, node::handle::Leaf>, } @@ -1111,10 +1111,10 @@ 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), @@ -1122,12 +1122,12 @@ impl<'a, Q: ?Sized, K: Ord, V> Entry<'a, Q, K, V> { } } -impl<'a, Q: ?Sized + ToOwned<K>, K: Ord, V> VacantEntry<'a, Q, K, V> { +impl<'a, K: Ord, V> VacantEntry<'a, K, V> { #[stable] /// Sets the value of the entry with the VacantEntry's key, /// and returns a mutable reference to it. pub fn insert(self, value: V) -> &'a mut V { - self.stack.insert(self.key.to_owned(), value) + self.stack.insert(self.key, value) } } @@ -1362,14 +1362,12 @@ impl<K: Ord, V> BTreeMap<K, V> { /// ``` /// 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> - { + 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 { @@ -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}; @@ -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); @@ -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(); @@ -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); @@ -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); |
