From 77d8b7c5e47d9f992d8fd4d72f50d924f03209cc Mon Sep 17 00:00:00 2001 From: Paul Soporan Date: Sun, 16 Oct 2022 01:53:21 +0300 Subject: [PATCH 1/3] feat: impl `From<[(K, V); N]>` for `Map` and `Value` --- build.rs | 6 ++++++ src/map.rs | 9 ++++++++- src/value/from.rs | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index e9ec7d56a..b4169bb04 100644 --- a/build.rs +++ b/build.rs @@ -32,6 +32,12 @@ fn main() { println!("cargo:rustc-cfg=no_btreemap_remove_entry"); } + // Const generics + // https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#const-generics-mvp + if minor < 51 { + println!("cargo:rustc-cfg=no_const_generics"); + } + // BTreeMap::retain // https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#stabilized-apis if minor < 53 { diff --git a/src/map.rs b/src/map.rs index 87cf54566..102dfc1c7 100644 --- a/src/map.rs +++ b/src/map.rs @@ -14,7 +14,7 @@ use core::hash::Hash; use core::iter::{FromIterator, FusedIterator}; #[cfg(feature = "preserve_order")] use core::mem; -use core::ops; +use core::{array, ops}; use serde::de; #[cfg(not(feature = "preserve_order"))] @@ -430,6 +430,13 @@ impl<'de> de::Deserialize<'de> for Map { } } +#[cfg(not(no_const_generics))] +impl From<[(String, Value); N]> for Map { + fn from(arr: [(String, Value); N]) -> Self { + array::IntoIter::new(arr).collect() + } +} + impl FromIterator<(String, Value)> for Map { fn from_iter(iter: T) -> Self where diff --git a/src/value/from.rs b/src/value/from.rs index 858a6e48a..983145d59 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -4,6 +4,7 @@ use crate::number::Number; use alloc::borrow::Cow; use alloc::string::{String, ToString}; use alloc::vec::Vec; +use core::array; use core::iter::FromIterator; #[cfg(feature = "arbitrary_precision")] @@ -233,6 +234,23 @@ impl> FromIterator for Value { } } +#[cfg(not(no_const_generics))] +impl, V: Into, const N: usize> From<[(K, V); N]> for Value { + /// Convert a list of map entry tuples to `Value` + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let v = [("lorem", 40), ("ipsum", 2)]; + /// let x: Value = v.into(); + /// ``` + fn from(arr: [(K, V); N]) -> Self { + array::IntoIter::new(arr).collect() + } +} + impl, V: Into> FromIterator<(K, V)> for Value { /// Convert an iteratable type to a `Value` /// From 5761163a6b742c312aaffe3c643d4071bb426582 Mon Sep 17 00:00:00 2001 From: Paul Soporan Date: Sun, 16 Oct 2022 02:00:28 +0300 Subject: [PATCH 2/3] chore: fix unused import --- src/map.rs | 4 ++-- src/value/from.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/map.rs b/src/map.rs index 102dfc1c7..5bc9b4735 100644 --- a/src/map.rs +++ b/src/map.rs @@ -14,7 +14,7 @@ use core::hash::Hash; use core::iter::{FromIterator, FusedIterator}; #[cfg(feature = "preserve_order")] use core::mem; -use core::{array, ops}; +use core::ops; use serde::de; #[cfg(not(feature = "preserve_order"))] @@ -433,7 +433,7 @@ impl<'de> de::Deserialize<'de> for Map { #[cfg(not(no_const_generics))] impl From<[(String, Value); N]> for Map { fn from(arr: [(String, Value); N]) -> Self { - array::IntoIter::new(arr).collect() + core::array::IntoIter::new(arr).collect() } } diff --git a/src/value/from.rs b/src/value/from.rs index 983145d59..7c28b0d38 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -4,7 +4,6 @@ use crate::number::Number; use alloc::borrow::Cow; use alloc::string::{String, ToString}; use alloc::vec::Vec; -use core::array; use core::iter::FromIterator; #[cfg(feature = "arbitrary_precision")] @@ -247,7 +246,7 @@ impl, V: Into, const N: usize> From<[(K, V); N]> for Valu /// let x: Value = v.into(); /// ``` fn from(arr: [(K, V); N]) -> Self { - array::IntoIter::new(arr).collect() + core::array::IntoIter::new(arr).collect() } } From 705feedf3808bb8d4c43652dd840d5019798e6bd Mon Sep 17 00:00:00 2001 From: Paul Soporan Date: Sun, 16 Oct 2022 02:07:54 +0300 Subject: [PATCH 3/3] chore: allow(deprecated) --- src/map.rs | 2 ++ src/value/from.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/map.rs b/src/map.rs index 5bc9b4735..02b0a3f01 100644 --- a/src/map.rs +++ b/src/map.rs @@ -433,6 +433,8 @@ impl<'de> de::Deserialize<'de> for Map { #[cfg(not(no_const_generics))] impl From<[(String, Value); N]> for Map { fn from(arr: [(String, Value); N]) -> Self { + // FromIterator::from_iter cannot be used before Rust 1.53 + #[allow(deprecated)] core::array::IntoIter::new(arr).collect() } } diff --git a/src/value/from.rs b/src/value/from.rs index 7c28b0d38..7fe0d8ece 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -246,6 +246,8 @@ impl, V: Into, const N: usize> From<[(K, V); N]> for Valu /// let x: Value = v.into(); /// ``` fn from(arr: [(K, V); N]) -> Self { + // FromIterator::from_iter cannot be used before Rust 1.53 + #[allow(deprecated)] core::array::IntoIter::new(arr).collect() } }