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..02b0a3f01 100644 --- a/src/map.rs +++ b/src/map.rs @@ -430,6 +430,15 @@ 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() + } +} + 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..7fe0d8ece 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -233,6 +233,25 @@ 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 { + // FromIterator::from_iter cannot be used before Rust 1.53 + #[allow(deprecated)] + core::array::IntoIter::new(arr).collect() + } +} + impl, V: Into> FromIterator<(K, V)> for Value { /// Convert an iteratable type to a `Value` ///