File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 8
8
9
9
use crate :: value:: Value ;
10
10
use alloc:: string:: String ;
11
+ #[ cfg( feature = "preserve_order" ) ]
12
+ use alloc:: vec:: Vec ;
11
13
use core:: borrow:: Borrow ;
12
14
use core:: fmt:: { self , Debug } ;
13
15
use core:: hash:: Hash ;
@@ -368,6 +370,23 @@ impl PartialEq for Map<String, Value> {
368
370
369
371
impl Eq for Map < String , Value > { }
370
372
373
+ #[ cfg( not( feature = "preserve_order" ) ) ]
374
+ impl Hash for Map < String , Value > {
375
+ #[ inline]
376
+ fn hash < H : core:: hash:: Hasher > ( & self , state : & mut H ) {
377
+ self . map . hash ( state)
378
+ }
379
+ }
380
+ #[ cfg( feature = "preserve_order" ) ]
381
+ impl Hash for Map < String , Value > {
382
+ #[ inline]
383
+ fn hash < H : core:: hash:: Hasher > ( & self , state : & mut H ) {
384
+ let mut kv = self . map . iter ( ) . collect :: < Vec < _ > > ( ) ;
385
+ kv. sort_unstable_by ( |a, b| a. 0 . cmp ( b. 0 ) ) ;
386
+ kv. hash ( state) ;
387
+ }
388
+ }
389
+
371
390
/// Access an element of this map. Panics if the given key is not present in the
372
391
/// map.
373
392
///
Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ pub use crate::raw::{to_raw_value, RawValue};
112
112
/// Represents any valid JSON value.
113
113
///
114
114
/// See the [`serde_json::value` module documentation](self) for usage examples.
115
- #[ derive( Clone , Eq , PartialEq ) ]
115
+ #[ derive( Clone , Eq , PartialEq , Hash ) ]
116
116
pub enum Value {
117
117
/// Represents a JSON null value.
118
118
///
You can’t perform that action at this time.
0 commit comments