From 7335c8b71366e99420c9b670beec403777b2b0e5 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Mon, 6 Mar 2023 16:47:07 -0800 Subject: [PATCH] Support FromIterator for Map with broader K, V This matches the equivalent `FromIterator for Value` which allows for `K: Into` and `V: Into` --- src/map.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/map.rs b/src/map.rs index 3e8a3814c..7ce53069c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -434,13 +434,26 @@ impl<'de> de::Deserialize<'de> for Map { } } -impl FromIterator<(String, Value)> for Map { +impl, V: Into> FromIterator<(K, V)> for Map { + /// Convert an iteratable type to a `Map` + /// + /// # Examples + /// + /// ``` + /// use serde_json::Map; + /// + /// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)]; + /// let x: Map<_, _> = v.into_iter().collect(); + /// ``` fn from_iter(iter: T) -> Self where - T: IntoIterator, + T: IntoIterator, { Map { - map: FromIterator::from_iter(iter), + map: iter + .into_iter() + .map(|(k, v)| (k.into(), v.into())) + .collect(), } } }