From 434641ed44aaa4f0ead85cfa7f21d66ed32f6ef3 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Thu, 17 Nov 2022 10:56:11 -0500 Subject: [PATCH] Use derive for common map trail implementations 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 `` pair. Additionally, use IndexMap::extend in Map::append, to take advantage of reservation logic --- src/map.rs | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/src/map.rs b/src/map.rs index 87cf54566..013791a3d 100644 --- a/src/map.rs +++ b/src/map.rs @@ -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 { map: MapImpl, } @@ -197,9 +198,8 @@ impl Map { #[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); } @@ -297,24 +297,6 @@ impl Default for Map { } } -impl Clone for Map { - #[inline] - fn clone(&self) -> Self { - Map { - map: self.map.clone(), - } - } -} - -impl PartialEq for Map { - #[inline] - fn eq(&self, other: &Self) -> bool { - self.map.eq(&other.map) - } -} - -impl Eq for Map {} - /// Access an element of this map. Panics if the given key is not present in the /// map. /// @@ -364,13 +346,6 @@ where } } -impl Debug for Map { - #[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 { #[inline]