77//! [`IndexMap`]: https://docs.rs/indexmap/*/indexmap/map/struct.IndexMap.html
88
99use crate :: value:: Value ;
10+ #[ cfg( feature = "preserve_order" ) ]
11+ use ahash:: RandomState ;
1012use alloc:: string:: String ;
1113use core:: borrow:: Borrow ;
1214use core:: fmt:: { self , Debug } ;
@@ -30,14 +32,17 @@ pub struct Map<K, V> {
3032#[ cfg( not( feature = "preserve_order" ) ) ]
3133type MapImpl < K , V > = BTreeMap < K , V > ;
3234#[ cfg( feature = "preserve_order" ) ]
33- type MapImpl < K , V > = IndexMap < K , V > ;
35+ type MapImpl < K , V > = IndexMap < K , V , RandomState > ;
3436
3537impl Map < String , Value > {
3638 /// Makes a new empty Map.
3739 #[ inline]
3840 pub fn new ( ) -> Self {
3941 Map {
42+ #[ cfg( not( feature = "preserve_order" ) ) ]
4043 map : MapImpl :: new ( ) ,
44+ #[ cfg( feature = "preserve_order" ) ]
45+ map : MapImpl :: with_hasher ( RandomState :: new ( ) ) ,
4146 }
4247 }
4348
@@ -52,7 +57,7 @@ impl Map<String, Value> {
5257 BTreeMap :: new ( )
5358 } ,
5459 #[ cfg( feature = "preserve_order" ) ]
55- map : IndexMap :: with_capacity ( capacity) ,
60+ map : IndexMap :: with_capacity_and_hasher ( capacity, RandomState :: new ( ) ) ,
5661 }
5762 }
5863
@@ -159,8 +164,10 @@ impl Map<String, Value> {
159164 #[ inline]
160165 pub fn append ( & mut self , other : & mut Self ) {
161166 #[ cfg( feature = "preserve_order" ) ]
162- self . map
163- . extend ( mem:: replace ( & mut other. map , MapImpl :: default ( ) ) ) ;
167+ self . map . extend ( mem:: replace (
168+ & mut other. map ,
169+ MapImpl :: with_hasher ( RandomState :: new ( ) ) ,
170+ ) ) ;
164171 #[ cfg( not( feature = "preserve_order" ) ) ]
165172 self . map . append ( & mut other. map ) ;
166173 }
@@ -252,7 +259,10 @@ impl Default for Map<String, Value> {
252259 #[ inline]
253260 fn default ( ) -> Self {
254261 Map {
262+ #[ cfg( not( feature = "preserve_order" ) ) ]
255263 map : MapImpl :: new ( ) ,
264+ #[ cfg( feature = "preserve_order" ) ]
265+ map : MapImpl :: with_hasher ( RandomState :: new ( ) ) ,
256266 }
257267 }
258268}
@@ -400,8 +410,16 @@ impl FromIterator<(String, Value)> for Map<String, Value> {
400410 where
401411 T : IntoIterator < Item = ( String , Value ) > ,
402412 {
403- Map {
413+ Self {
414+ #[ cfg( not( feature = "preserve_order" ) ) ]
404415 map : FromIterator :: from_iter ( iter) ,
416+ #[ cfg( feature = "preserve_order" ) ]
417+ map : {
418+ // TODO: replace with `iter.into_iter().collect();` when RandomState will impl Default
419+ let mut map = MapImpl :: with_hasher ( RandomState :: new ( ) ) ;
420+ map. extend ( iter) ;
421+ map
422+ } ,
405423 }
406424 }
407425}
0 commit comments