8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
-
12
11
//! A HashMap wrapper that holds key-value pairs in insertion order.
13
12
//!
14
13
//! # Examples
30
29
31
30
#![ forbid( missing_docs) ]
32
31
#![ feature( hashmap_hasher) ]
33
- #![ feature( box_raw) ]
34
32
#![ feature( iter_order) ]
35
33
#![ cfg_attr( test, feature( test) ) ]
36
34
@@ -69,7 +67,7 @@ impl<K: Hash> Hash for KeyRef<K> {
69
67
}
70
68
71
69
impl < K : PartialEq > PartialEq for KeyRef < K > {
72
- fn eq ( & self , other : & KeyRef < K > ) -> bool {
70
+ fn eq ( & self , other : & Self ) -> bool {
73
71
unsafe { ( * self . k ) . eq ( & * other. k ) }
74
72
}
75
73
}
@@ -83,7 +81,7 @@ impl<K: Eq> Eq for KeyRef<K> {}
83
81
struct Qey < Q : ?Sized > ( Q ) ;
84
82
85
83
impl < Q : ?Sized > Qey < Q > {
86
- fn from_ref ( q : & Q ) -> & Qey < Q > { unsafe { mem:: transmute ( q) } }
84
+ fn from_ref ( q : & Q ) -> & Self { unsafe { mem:: transmute ( q) } }
87
85
}
88
86
89
87
impl < K , Q : ?Sized > Borrow < Qey < Q > > for KeyRef < K > where K : Borrow < Q > {
@@ -93,7 +91,7 @@ impl<K, Q: ?Sized> Borrow<Qey<Q>> for KeyRef<K> where K: Borrow<Q> {
93
91
}
94
92
95
93
impl < K , V > LinkedHashMapEntry < K , V > {
96
- fn new ( k : K , v : V ) -> LinkedHashMapEntry < K , V > {
94
+ fn new ( k : K , v : V ) -> Self {
97
95
LinkedHashMapEntry {
98
96
key : k,
99
97
value : v,
@@ -112,11 +110,11 @@ unsafe fn drop_empty_entry_box<K, V>(the_box: *mut LinkedHashMapEntry<K, V>) {
112
110
113
111
impl < K : Hash + Eq , V > LinkedHashMap < K , V > {
114
112
/// Creates a linked hash map.
115
- pub fn new ( ) -> LinkedHashMap < K , V > { LinkedHashMap :: with_map ( HashMap :: new ( ) ) }
113
+ pub fn new ( ) -> Self { Self :: with_map ( HashMap :: new ( ) ) }
116
114
117
115
/// Creates an empty linked hash map with the given initial capacity.
118
- pub fn with_capacity ( capacity : usize ) -> LinkedHashMap < K , V > {
119
- LinkedHashMap :: with_map ( HashMap :: with_capacity ( capacity) )
116
+ pub fn with_capacity ( capacity : usize ) -> Self {
117
+ Self :: with_map ( HashMap :: with_capacity ( capacity) )
120
118
}
121
119
}
122
120
@@ -135,7 +133,7 @@ impl<K, V, S> LinkedHashMap<K, V, S> {
135
133
}
136
134
137
135
impl < K : Hash + Eq , V , S : HashState > LinkedHashMap < K , V , S > {
138
- fn with_map ( map : HashMap < KeyRef < K > , Box < LinkedHashMapEntry < K , V > > , S > ) -> LinkedHashMap < K , V , S > {
136
+ fn with_map ( map : HashMap < KeyRef < K > , Box < LinkedHashMapEntry < K , V > > , S > ) -> Self {
139
137
LinkedHashMap {
140
138
map : map,
141
139
head : ptr:: null_mut ( ) ,
@@ -144,13 +142,13 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
144
142
}
145
143
146
144
/// Creates an empty linked hash map with the given initial hash state.
147
- pub fn with_hash_state ( hash_state : S ) -> LinkedHashMap < K , V , S > {
148
- LinkedHashMap :: with_map ( HashMap :: with_hash_state ( hash_state) )
145
+ pub fn with_hash_state ( hash_state : S ) -> Self {
146
+ Self :: with_map ( HashMap :: with_hash_state ( hash_state) )
149
147
}
150
148
151
149
/// Creates an empty linked hash map with the given initial capacity and hash state.
152
- pub fn with_capacity_and_hash_state ( capacity : usize , hash_state : S ) -> LinkedHashMap < K , V , S > {
153
- LinkedHashMap :: with_map ( HashMap :: with_capacity_and_hash_state ( capacity, hash_state) )
150
+ pub fn with_capacity_and_hash_state ( capacity : usize , hash_state : S ) -> Self {
151
+ Self :: with_map ( HashMap :: with_capacity_and_hash_state ( capacity, hash_state) )
154
152
}
155
153
156
154
/// Reserves capacity for at least `additional` more elements to be inserted into the map. The
@@ -378,7 +376,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
378
376
None
379
377
}
380
378
381
- /// Get the first entry.
379
+ /// Gets the first entry.
382
380
///
383
381
/// # Examples
384
382
///
@@ -424,7 +422,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
424
422
None
425
423
}
426
424
427
- /// Get the last entry.
425
+ /// Gets the last entry.
428
426
///
429
427
/// # Examples
430
428
///
@@ -451,7 +449,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
451
449
/// Returns whether the map is currently empty.
452
450
pub fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
453
451
454
- /// Clear the map of all key-value pairs.
452
+ /// Clears the map of all key-value pairs.
455
453
pub fn clear ( & mut self ) {
456
454
self . map . clear ( ) ;
457
455
// update the guard node if present
@@ -620,13 +618,13 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
620
618
// `LinkedHashMap`s without cloning the original map and clearing it. For now, only
621
619
// `LinkedHashMap<K, V>`s implement `Clone`.
622
620
impl < K : Hash + Eq + Clone , V : Clone > Clone for LinkedHashMap < K , V > {
623
- fn clone ( & self ) -> LinkedHashMap < K , V > {
621
+ fn clone ( & self ) -> Self {
624
622
self . iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. clone ( ) ) ) . collect ( )
625
623
}
626
624
}
627
625
628
626
impl < K : Hash + Eq , V , S : HashState + Default > Default for LinkedHashMap < K , V , S > {
629
- fn default ( ) -> LinkedHashMap < K , V , S > { LinkedHashMap :: with_hash_state ( Default :: default ( ) ) }
627
+ fn default ( ) -> Self { LinkedHashMap :: with_hash_state ( Default :: default ( ) ) }
630
628
}
631
629
632
630
impl < K : Hash + Eq , V , S : HashState > Extend < ( K , V ) > for LinkedHashMap < K , V , S > {
@@ -638,9 +636,9 @@ impl<K: Hash + Eq, V, S: HashState> Extend<(K, V)> for LinkedHashMap<K, V, S> {
638
636
}
639
637
640
638
impl < K : Hash + Eq , V , S : HashState + Default > iter:: FromIterator < ( K , V ) > for LinkedHashMap < K , V , S > {
641
- fn from_iter < I : IntoIterator < Item =( K , V ) > > ( iter : I ) -> LinkedHashMap < K , V , S > {
639
+ fn from_iter < I : IntoIterator < Item =( K , V ) > > ( iter : I ) -> Self {
642
640
let iter = iter. into_iter ( ) ;
643
- let mut map = LinkedHashMap :: with_capacity_and_hash_state ( iter. size_hint ( ) . 0 , Default :: default ( ) ) ;
641
+ let mut map = Self :: with_capacity_and_hash_state ( iter. size_hint ( ) . 0 , Default :: default ( ) ) ;
644
642
map. extend ( iter) ;
645
643
map
646
644
}
@@ -649,54 +647,47 @@ impl<K: Hash + Eq, V, S: HashState + Default> iter::FromIterator<(K, V)> for Lin
649
647
impl < A : fmt:: Debug + Hash + Eq , B : fmt:: Debug , S : HashState > fmt:: Debug for LinkedHashMap < A , B , S > {
650
648
/// Returns a string that lists the key-value pairs in insertion order.
651
649
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
652
- try!( write ! ( f, "{{" ) ) ;
653
-
654
- for ( i, ( k, v) ) in self . iter ( ) . enumerate ( ) {
655
- if i != 0 { try!( write ! ( f, ", " ) ) ; }
656
- try!( write ! ( f, "{:?}: {:?}" , * k, * v) ) ;
657
- }
658
-
659
- write ! ( f, "}}" )
650
+ f. debug_map ( ) . entries ( self ) . finish ( )
660
651
}
661
652
}
662
653
663
654
impl < K : Hash + Eq , V : PartialEq , S : HashState > PartialEq for LinkedHashMap < K , V , S > {
664
- fn eq ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
665
- self . len ( ) == other. len ( ) && iter :: order :: eq ( self . iter ( ) , other . iter ( ) )
655
+ fn eq ( & self , other : & Self ) -> bool {
656
+ self . len ( ) == other. len ( ) && self . iter ( ) . eq ( other )
666
657
}
667
658
668
- fn ne ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
669
- self . len ( ) != other. len ( ) || iter :: order :: ne ( self . iter ( ) , other . iter ( ) )
659
+ fn ne ( & self , other : & Self ) -> bool {
660
+ self . len ( ) != other. len ( ) || self . iter ( ) . ne ( other )
670
661
}
671
662
}
672
663
673
664
impl < K : Hash + Eq , V : Eq , S : HashState > Eq for LinkedHashMap < K , V , S > { }
674
665
675
666
impl < K : Hash + Eq + PartialOrd , V : PartialOrd , S : HashState > PartialOrd for LinkedHashMap < K , V , S > {
676
- fn partial_cmp ( & self , other : & LinkedHashMap < K , V , S > ) -> Option < Ordering > {
677
- iter :: order :: partial_cmp ( self . iter ( ) , other . iter ( ) )
667
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
668
+ self . iter ( ) . partial_cmp ( other )
678
669
}
679
670
680
- fn lt ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
681
- iter :: order :: lt ( self . iter ( ) , other . iter ( ) )
671
+ fn lt ( & self , other : & Self ) -> bool {
672
+ self . iter ( ) . lt ( other )
682
673
}
683
674
684
- fn le ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
685
- iter :: order :: le ( self . iter ( ) , other . iter ( ) )
675
+ fn le ( & self , other : & Self ) -> bool {
676
+ self . iter ( ) . le ( other )
686
677
}
687
678
688
- fn ge ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
689
- iter :: order :: ge ( self . iter ( ) , other . iter ( ) )
679
+ fn ge ( & self , other : & Self ) -> bool {
680
+ self . iter ( ) . ge ( other )
690
681
}
691
682
692
- fn gt ( & self , other : & LinkedHashMap < K , V , S > ) -> bool {
693
- iter :: order :: gt ( self . iter ( ) , other . iter ( ) )
683
+ fn gt ( & self , other : & Self ) -> bool {
684
+ self . iter ( ) . gt ( other )
694
685
}
695
686
}
696
687
697
688
impl < K : Hash + Eq + Ord , V : Ord , S : HashState > Ord for LinkedHashMap < K , V , S > {
698
- fn cmp ( & self , other : & LinkedHashMap < K , V , S > ) -> Ordering {
699
- iter :: order :: cmp ( self . iter ( ) , other . iter ( ) )
689
+ fn cmp ( & self , other : & Self ) -> Ordering {
690
+ self . iter ( ) . cmp ( other )
700
691
}
701
692
}
702
693
@@ -738,7 +729,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
738
729
}
739
730
740
731
impl < ' a , K , V > Clone for Iter < ' a , K , V > {
741
- fn clone ( & self ) -> Iter < ' a , K , V > { Iter { ..* self } }
732
+ fn clone ( & self ) -> Self { Iter { ..* self } }
742
733
}
743
734
744
735
impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
@@ -821,14 +812,13 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
821
812
fn len ( & self ) -> usize { self . remaining }
822
813
}
823
814
824
-
825
815
/// An insertion-order iterator over a `LinkedHashMap`'s keys.
826
816
pub struct Keys < ' a , K : ' a , V : ' a > {
827
817
inner : iter:: Map < Iter < ' a , K , V > , fn ( ( & ' a K , & ' a V ) ) -> & ' a K >
828
818
}
829
819
830
820
impl < ' a , K , V > Clone for Keys < ' a , K , V > {
831
- fn clone ( & self ) -> Keys < ' a , K , V > { Keys { inner : self . inner . clone ( ) } }
821
+ fn clone ( & self ) -> Self { Keys { inner : self . inner . clone ( ) } }
832
822
}
833
823
834
824
impl < ' a , K , V > Iterator for Keys < ' a , K , V > {
@@ -846,14 +836,13 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
846
836
fn len ( & self ) -> usize { self . inner . len ( ) }
847
837
}
848
838
849
-
850
839
/// An insertion-order iterator over a `LinkedHashMap`'s values.
851
840
pub struct Values < ' a , K : ' a , V : ' a > {
852
841
inner : iter:: Map < Iter < ' a , K , V > , fn ( ( & ' a K , & ' a V ) ) -> & ' a V >
853
842
}
854
843
855
844
impl < ' a , K , V > Clone for Values < ' a , K , V > {
856
- fn clone ( & self ) -> Values < ' a , K , V > { Values { inner : self . inner . clone ( ) } }
845
+ fn clone ( & self ) -> Self { Values { inner : self . inner . clone ( ) } }
857
846
}
858
847
859
848
impl < ' a , K , V > Iterator for Values < ' a , K , V > {
@@ -1140,5 +1129,4 @@ mod tests {
1140
1129
}
1141
1130
} )
1142
1131
}
1143
-
1144
1132
}
0 commit comments