@@ -19,7 +19,7 @@ pub use self::Entry::*;
19
19
20
20
use core:: prelude:: * ;
21
21
22
- use core:: borrow:: BorrowFrom ;
22
+ use core:: borrow:: { BorrowFrom , ToOwned } ;
23
23
use core:: cmp:: Ordering ;
24
24
use core:: default:: Default ;
25
25
use core:: fmt:: Show ;
@@ -128,20 +128,23 @@ pub struct Values<'a, K: 'a, V: 'a> {
128
128
inner : Map < ( & ' a K , & ' a V ) , & ' a V , Iter < ' a , K , V > , fn ( ( & ' a K , & ' a V ) ) -> & ' a V >
129
129
}
130
130
131
+ #[ stable]
131
132
/// 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 > {
133
134
/// A vacant Entry
134
- Vacant ( VacantEntry < ' a , K , V > ) ,
135
+ Vacant ( VacantEntry < ' a , Q , K , V > ) ,
135
136
/// An occupied Entry
136
137
Occupied ( OccupiedEntry < ' a , K , V > ) ,
137
138
}
138
139
140
+ #[ stable]
139
141
/// 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 ,
142
144
stack : stack:: SearchStack < ' a , K , V , node:: handle:: Edge , node:: handle:: Leaf > ,
143
145
}
144
146
147
+ #[ stable]
145
148
/// An occupied Entry.
146
149
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
147
150
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> {
1132
1135
#[ stable]
1133
1136
impl < ' a , K , V > ExactSizeIterator for Values < ' a , K , V > { }
1134
1137
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
+ }
1135
1148
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]
1137
1151
/// Sets the value of the entry with the VacantEntry's key,
1138
1152
/// 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)
1141
1155
}
1142
1156
}
1143
1157
1144
1158
impl < ' a , K : Ord , V > OccupiedEntry < ' a , K , V > {
1159
+ #[ stable]
1145
1160
/// Gets a reference to the value in the entry.
1146
1161
pub fn get ( & self ) -> & V {
1147
1162
self . stack . peek ( )
1148
1163
}
1149
1164
1165
+ #[ stable]
1150
1166
/// Gets a mutable reference to the value in the entry.
1151
1167
pub fn get_mut ( & mut self ) -> & mut V {
1152
1168
self . stack . peek_mut ( )
1153
1169
}
1154
1170
1171
+ #[ stable]
1155
1172
/// Converts the entry into a mutable reference to its value.
1156
1173
pub fn into_mut ( self ) -> & ' a mut V {
1157
1174
self . stack . into_top ( )
1158
1175
}
1159
1176
1177
+ #[ stable]
1160
1178
/// Sets the value of the entry with the OccupiedEntry's key,
1161
1179
/// 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 {
1163
1181
mem:: swap ( self . stack . peek_mut ( ) , & mut value) ;
1164
1182
value
1165
1183
}
1166
1184
1185
+ #[ stable]
1167
1186
/// 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 {
1169
1188
self . stack . remove ( )
1170
1189
}
1171
1190
}
@@ -1352,9 +1371,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
1352
1371
///
1353
1372
/// // count the number of occurrences of letters in the vec
1354
1373
/// for x in vec!["a","b","a","c","a","b"].iter() {
1355
- /// match count.entry(* x) {
1374
+ /// match count.entry(x) {
1356
1375
/// Entry::Vacant(view) => {
1357
- /// view.set (1);
1376
+ /// view.insert (1);
1358
1377
/// },
1359
1378
/// Entry::Occupied(mut view) => {
1360
1379
/// let v = view.get_mut();
@@ -1365,12 +1384,16 @@ impl<K: Ord, V> BTreeMap<K, V> {
1365
1384
///
1366
1385
/// assert_eq!(count["a"], 3u);
1367
1386
/// ```
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
+ {
1369
1392
// same basic logic of `swap` and `pop`, blended together
1370
1393
let mut stack = stack:: PartialSearchStack :: new ( self ) ;
1371
1394
loop {
1372
1395
let result = stack. with ( move |pusher, node| {
1373
- return match Node :: search ( node, & key) {
1396
+ return match Node :: search ( node, key) {
1374
1397
Found ( handle) => {
1375
1398
// Perfect match
1376
1399
Finished ( Occupied ( OccupiedEntry {
@@ -1413,6 +1436,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
1413
1436
#[ cfg( test) ]
1414
1437
mod test {
1415
1438
use prelude:: * ;
1439
+ use std:: borrow:: { ToOwned , BorrowFrom } ;
1416
1440
1417
1441
use super :: { BTreeMap , Occupied , Vacant } ;
1418
1442
@@ -1562,19 +1586,19 @@ mod test {
1562
1586
let mut map: BTreeMap < int , int > = xs. iter ( ) . map ( |& x| x) . collect ( ) ;
1563
1587
1564
1588
// Existing key (insert)
1565
- match map. entry ( 1 ) {
1589
+ match map. entry ( & 1 ) {
1566
1590
Vacant ( _) => unreachable ! ( ) ,
1567
1591
Occupied ( mut view) => {
1568
1592
assert_eq ! ( view. get( ) , & 10 ) ;
1569
- assert_eq ! ( view. set ( 100 ) , 10 ) ;
1593
+ assert_eq ! ( view. insert ( 100 ) , 10 ) ;
1570
1594
}
1571
1595
}
1572
1596
assert_eq ! ( map. get( & 1 ) . unwrap( ) , & 100 ) ;
1573
1597
assert_eq ! ( map. len( ) , 6 ) ;
1574
1598
1575
1599
1576
1600
// Existing key (update)
1577
- match map. entry ( 2 ) {
1601
+ match map. entry ( & 2 ) {
1578
1602
Vacant ( _) => unreachable ! ( ) ,
1579
1603
Occupied ( mut view) => {
1580
1604
let v = view. get_mut ( ) ;
@@ -1585,21 +1609,21 @@ mod test {
1585
1609
assert_eq ! ( map. len( ) , 6 ) ;
1586
1610
1587
1611
// Existing key (take)
1588
- match map. entry ( 3 ) {
1612
+ match map. entry ( & 3 ) {
1589
1613
Vacant ( _) => unreachable ! ( ) ,
1590
1614
Occupied ( view) => {
1591
- assert_eq ! ( view. take ( ) , 30 ) ;
1615
+ assert_eq ! ( view. remove ( ) , 30 ) ;
1592
1616
}
1593
1617
}
1594
1618
assert_eq ! ( map. get( & 3 ) , None ) ;
1595
1619
assert_eq ! ( map. len( ) , 5 ) ;
1596
1620
1597
1621
1598
1622
// Inexistent key (insert)
1599
- match map. entry ( 10 ) {
1623
+ match map. entry ( & 10 ) {
1600
1624
Occupied ( _) => unreachable ! ( ) ,
1601
1625
Vacant ( view) => {
1602
- assert_eq ! ( * view. set ( 1000 ) , 1000 ) ;
1626
+ assert_eq ! ( * view. insert ( 1000 ) , 1000 ) ;
1603
1627
}
1604
1628
}
1605
1629
assert_eq ! ( map. get( & 10 ) . unwrap( ) , & 1000 ) ;
0 commit comments