@@ -19,7 +19,7 @@ pub use self::Entry::*;
1919
2020use core:: prelude:: * ;
2121
22- use core:: borrow:: BorrowFrom ;
22+ use core:: borrow:: { BorrowFrom , ToOwned } ;
2323use core:: cmp:: Ordering ;
2424use core:: default:: Default ;
2525use core:: fmt:: Show ;
@@ -128,20 +128,23 @@ pub struct Values<'a, K: 'a, V: 'a> {
128128 inner : Map < ( & ' a K , & ' a V ) , & ' a V , Iter < ' a , K , V > , fn ( ( & ' a K , & ' a V ) ) -> & ' a V >
129129}
130130
131+ #[ stable]
131132/// A view into a single entry in a map, which may either be vacant or occupied.
132- pub enum Entry < ' a , K : ' a , V : ' a > {
133+ pub enum Entry < ' a , Sized ? Q : ' a , K : ' a , V : ' a > {
133134 /// A vacant Entry
134- Vacant ( VacantEntry < ' a , K , V > ) ,
135+ Vacant ( VacantEntry < ' a , Q , K , V > ) ,
135136 /// An occupied Entry
136137 Occupied ( OccupiedEntry < ' a , K , V > ) ,
137138}
138139
140+ #[ stable]
139141/// A vacant Entry.
140- pub struct VacantEntry < ' a , K : ' a , V : ' a > {
141- key : K ,
142+ pub struct VacantEntry < ' a , Sized ? Q : ' a , K : ' a , V : ' a > {
143+ key : & ' a Q ,
142144 stack : stack:: SearchStack < ' a , K , V , node:: handle:: Edge , node:: handle:: Leaf > ,
143145}
144146
147+ #[ stable]
145148/// An occupied Entry.
146149pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
147150 stack : stack:: SearchStack < ' a , K , V , node:: handle:: KV , node:: handle:: LeafOrInternal > ,
@@ -1132,40 +1135,56 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
11321135#[ stable]
11331136impl < ' a , K , V > ExactSizeIterator for Values < ' a , K , V > { }
11341137
1138+ impl < ' a , Sized ? Q , K : Ord , V > Entry < ' a , Q , K , V > {
1139+ #[ unstable = "matches collection reform v2 specification, waiting for dust to settle" ]
1140+ /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
1141+ pub fn get ( self ) -> Result < & ' a mut V , VacantEntry < ' a , Q , K , V > > {
1142+ match self {
1143+ Occupied ( entry) => Ok ( entry. into_mut ( ) ) ,
1144+ Vacant ( entry) => Err ( entry) ,
1145+ }
1146+ }
1147+ }
11351148
1136- impl < ' a , K : Ord , V > VacantEntry < ' a , K , V > {
1149+ impl < ' a , Sized ? Q : ToOwned < K > , K : Ord , V > VacantEntry < ' a , Q , K , V > {
1150+ #[ stable]
11371151 /// Sets the value of the entry with the VacantEntry's key,
11381152 /// and returns a mutable reference to it.
1139- pub fn set ( self , value : V ) -> & ' a mut V {
1140- self . stack . insert ( self . key , value)
1153+ pub fn insert ( self , value : V ) -> & ' a mut V {
1154+ self . stack . insert ( self . key . to_owned ( ) , value)
11411155 }
11421156}
11431157
11441158impl < ' a , K : Ord , V > OccupiedEntry < ' a , K , V > {
1159+ #[ stable]
11451160 /// Gets a reference to the value in the entry.
11461161 pub fn get ( & self ) -> & V {
11471162 self . stack . peek ( )
11481163 }
11491164
1165+ #[ stable]
11501166 /// Gets a mutable reference to the value in the entry.
11511167 pub fn get_mut ( & mut self ) -> & mut V {
11521168 self . stack . peek_mut ( )
11531169 }
11541170
1171+ #[ stable]
11551172 /// Converts the entry into a mutable reference to its value.
11561173 pub fn into_mut ( self ) -> & ' a mut V {
11571174 self . stack . into_top ( )
11581175 }
11591176
1177+ #[ stable]
11601178 /// Sets the value of the entry with the OccupiedEntry's key,
11611179 /// and returns the entry's old value.
1162- pub fn set ( & mut self , mut value : V ) -> V {
1180+ pub fn insert ( & mut self , mut value : V ) -> V {
11631181 mem:: swap ( self . stack . peek_mut ( ) , & mut value) ;
11641182 value
11651183 }
11661184
1185+ #[ stable]
11671186 /// Takes the value of the entry out of the map, and returns it.
1168- pub fn take ( self ) -> V {
1187+ pub fn remove ( self ) -> V {
11691188 self . stack . remove ( )
11701189 }
11711190}
@@ -1352,9 +1371,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
13521371 ///
13531372 /// // count the number of occurrences of letters in the vec
13541373 /// for x in vec!["a","b","a","c","a","b"].iter() {
1355- /// match count.entry(* x) {
1374+ /// match count.entry(x) {
13561375 /// Entry::Vacant(view) => {
1357- /// view.set (1);
1376+ /// view.insert (1);
13581377 /// },
13591378 /// Entry::Occupied(mut view) => {
13601379 /// let v = view.get_mut();
@@ -1365,12 +1384,16 @@ impl<K: Ord, V> BTreeMap<K, V> {
13651384 ///
13661385 /// assert_eq!(count["a"], 3u);
13671386 /// ```
1368- pub fn entry < ' a > ( & ' a mut self , mut key : K ) -> Entry < ' a , K , V > {
1387+ /// The key must have the same ordering before or after `.to_owned()` is called.
1388+ #[ stable]
1389+ pub fn entry < ' a , Sized ? Q > ( & ' a mut self , mut key : & ' a Q ) -> Entry < ' a , Q , K , V >
1390+ where Q : Ord + ToOwned < K >
1391+ {
13691392 // same basic logic of `swap` and `pop`, blended together
13701393 let mut stack = stack:: PartialSearchStack :: new ( self ) ;
13711394 loop {
13721395 let result = stack. with ( move |pusher, node| {
1373- return match Node :: search ( node, & key) {
1396+ return match Node :: search ( node, key) {
13741397 Found ( handle) => {
13751398 // Perfect match
13761399 Finished ( Occupied ( OccupiedEntry {
@@ -1413,6 +1436,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
14131436#[ cfg( test) ]
14141437mod test {
14151438 use prelude:: * ;
1439+ use std:: borrow:: { ToOwned , BorrowFrom } ;
14161440
14171441 use super :: { BTreeMap , Occupied , Vacant } ;
14181442
@@ -1562,19 +1586,19 @@ mod test {
15621586 let mut map: BTreeMap < int , int > = xs. iter ( ) . map ( |& x| x) . collect ( ) ;
15631587
15641588 // Existing key (insert)
1565- match map. entry ( 1 ) {
1589+ match map. entry ( & 1 ) {
15661590 Vacant ( _) => unreachable ! ( ) ,
15671591 Occupied ( mut view) => {
15681592 assert_eq ! ( view. get( ) , & 10 ) ;
1569- assert_eq ! ( view. set ( 100 ) , 10 ) ;
1593+ assert_eq ! ( view. insert ( 100 ) , 10 ) ;
15701594 }
15711595 }
15721596 assert_eq ! ( map. get( & 1 ) . unwrap( ) , & 100 ) ;
15731597 assert_eq ! ( map. len( ) , 6 ) ;
15741598
15751599
15761600 // Existing key (update)
1577- match map. entry ( 2 ) {
1601+ match map. entry ( & 2 ) {
15781602 Vacant ( _) => unreachable ! ( ) ,
15791603 Occupied ( mut view) => {
15801604 let v = view. get_mut ( ) ;
@@ -1585,21 +1609,21 @@ mod test {
15851609 assert_eq ! ( map. len( ) , 6 ) ;
15861610
15871611 // Existing key (take)
1588- match map. entry ( 3 ) {
1612+ match map. entry ( & 3 ) {
15891613 Vacant ( _) => unreachable ! ( ) ,
15901614 Occupied ( view) => {
1591- assert_eq ! ( view. take ( ) , 30 ) ;
1615+ assert_eq ! ( view. remove ( ) , 30 ) ;
15921616 }
15931617 }
15941618 assert_eq ! ( map. get( & 3 ) , None ) ;
15951619 assert_eq ! ( map. len( ) , 5 ) ;
15961620
15971621
15981622 // Inexistent key (insert)
1599- match map. entry ( 10 ) {
1623+ match map. entry ( & 10 ) {
16001624 Occupied ( _) => unreachable ! ( ) ,
16011625 Vacant ( view) => {
1602- assert_eq ! ( * view. set ( 1000 ) , 1000 ) ;
1626+ assert_eq ! ( * view. insert ( 1000 ) , 1000 ) ;
16031627 }
16041628 }
16051629 assert_eq ! ( map. get( & 10 ) . unwrap( ) , & 1000 ) ;
0 commit comments