Skip to content

Commit

Permalink
Use derive for common map trait implementations
Browse files Browse the repository at this point in the history
Use derive for `Clone`, `Debug`, `PartialEq`, and `Eq`, which all currently definitions identical to the derived versions. The derived versions are now generic over any `<K, V>` pair.

Additionally, use IndexMap::extend in Map::append, to take advantage of reservation logic
  • Loading branch information
Lucretiel committed Nov 17, 2022
1 parent ca41bdd commit 0248fbb
Showing 1 changed file with 3 additions and 28 deletions.
31 changes: 3 additions & 28 deletions src/map.rs
Expand Up @@ -23,6 +23,7 @@ use alloc::collections::{btree_map, BTreeMap};
use indexmap::{self, IndexMap};

/// Represents a JSON key/value type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Map<K, V> {
map: MapImpl<K, V>,
}
Expand Down Expand Up @@ -197,9 +198,8 @@ impl Map<String, Value> {
#[inline]
pub fn append(&mut self, other: &mut Self) {
#[cfg(feature = "preserve_order")]
for (k, v) in mem::replace(&mut other.map, MapImpl::default()) {
self.map.insert(k, v);
}
self.map
.extend(mem::replace(&mut other.map, MapImpl::default()));
#[cfg(not(feature = "preserve_order"))]
self.map.append(&mut other.map);
}
Expand Down Expand Up @@ -297,24 +297,6 @@ impl Default for Map<String, Value> {
}
}

impl Clone for Map<String, Value> {
#[inline]
fn clone(&self) -> Self {
Map {
map: self.map.clone(),
}
}
}

impl PartialEq for Map<String, Value> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.map.eq(&other.map)
}
}

impl Eq for Map<String, Value> {}

/// Access an element of this map. Panics if the given key is not present in the
/// map.
///
Expand Down Expand Up @@ -364,13 +346,6 @@ where
}
}

impl Debug for Map<String, Value> {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.map.fmt(formatter)
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
impl serde::ser::Serialize for Map<String, Value> {
#[inline]
Expand Down

0 comments on commit 0248fbb

Please sign in to comment.