@@ -484,6 +484,13 @@ impl<'a, K, V, S> Entry<'a, K, V, S> {
484
484
Entry :: Vacant ( ref entry) => entry. key ( ) ,
485
485
}
486
486
}
487
+
488
+ pub fn index ( & self ) -> usize {
489
+ match * self {
490
+ Entry :: Occupied ( ref entry) => entry. index ( ) ,
491
+ Entry :: Vacant ( ref entry) => entry. index ( ) ,
492
+ }
493
+ }
487
494
}
488
495
489
496
pub struct OccupiedEntry < ' a , K : ' a , V : ' a , S : ' a = RandomState > {
@@ -534,6 +541,7 @@ pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
534
541
impl < ' a , K , V , S > VacantEntry < ' a , K , V , S > {
535
542
pub fn key ( & self ) -> & K { & self . key }
536
543
pub fn into_key ( self ) -> K { self . key }
544
+ pub fn index ( & self ) -> usize { self . map . len ( ) }
537
545
pub fn insert ( self , value : V ) -> & ' a mut V {
538
546
if self . map . size_class_is_64bit ( ) {
539
547
self . insert_impl :: < u64 > ( value)
@@ -1733,4 +1741,27 @@ mod tests {
1733
1741
map. extend ( vec ! [ ( 5 , 6 ) ] ) ;
1734
1742
assert_eq ! ( map. into_iter( ) . collect:: <Vec <_>>( ) , vec![ ( 1 , 2 ) , ( 3 , 4 ) , ( 5 , 6 ) ] ) ;
1735
1743
}
1744
+
1745
+ #[ test]
1746
+ fn entry ( ) {
1747
+ let mut map = OrderMap :: new ( ) ;
1748
+
1749
+ map. insert ( 1 , "1" ) ;
1750
+ map. insert ( 2 , "2" ) ;
1751
+ {
1752
+ let e = map. entry ( 3 ) ;
1753
+ assert_eq ! ( e. index( ) , 2 ) ;
1754
+ let e = e. or_insert ( "3" ) ;
1755
+ assert_eq ! ( e, & "3" ) ;
1756
+ }
1757
+
1758
+ let e = map. entry ( 2 ) ;
1759
+ assert_eq ! ( e. index( ) , 1 ) ;
1760
+ assert_eq ! ( e. key( ) , & 2 ) ;
1761
+ match e {
1762
+ Entry :: Occupied ( ref e) => assert_eq ! ( e. get( ) , & "2" ) ,
1763
+ Entry :: Vacant ( _) => panic ! ( )
1764
+ }
1765
+ assert_eq ! ( e. or_insert( "4" ) , & "2" ) ;
1766
+ }
1736
1767
}
0 commit comments