Skip to content

Commit a32edef

Browse files
committed
Add support to preserve_order in no_std
1 parent 0131ac6 commit a32edef

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ repository = "https://github.com/serde-rs/json"
1212
rust-version = "1.56"
1313

1414
[dependencies]
15-
indexmap = { version = "2", optional = true }
15+
ahash = { version = "0.8", default-features = false, optional = true }
16+
indexmap = { version = "2", default-features = false, optional = true }
1617
itoa = "1.0"
1718
ryu = "1.0"
1819
serde = { version = "1.0.194", default-features = false }
@@ -45,7 +46,7 @@ features = ["raw_value"]
4546
[features]
4647
default = ["std"]
4748

48-
std = ["serde/std"]
49+
std = ["serde/std", "ahash?/std", "indexmap?/std"]
4950

5051
# Provide integration for heap-allocated collections without depending on the
5152
# rest of the Rust standard library.
@@ -55,7 +56,7 @@ alloc = ["serde/alloc"]
5556
# Make serde_json::Map use a representation which maintains insertion order.
5657
# This allows data to be read into a Value and written back to a JSON string
5758
# while preserving the order of map keys in the input.
58-
preserve_order = ["indexmap", "std"]
59+
preserve_order = ["ahash", "indexmap"]
5960

6061
# Use sufficient precision when parsing fixed precision floats from JSON to
6162
# ensure that they maintain accuracy when round-tripped through JSON. This comes

src/map.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//! [`IndexMap`]: https://docs.rs/indexmap/*/indexmap/map/struct.IndexMap.html
88
99
use crate::value::Value;
10+
#[cfg(feature = "preserve_order")]
11+
use ahash::RandomState;
1012
use alloc::string::String;
1113
use core::borrow::Borrow;
1214
use core::fmt::{self, Debug};
@@ -30,14 +32,17 @@ pub struct Map<K, V> {
3032
#[cfg(not(feature = "preserve_order"))]
3133
type 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

3537
impl 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

Comments
 (0)