From 18b1604fc8598a1f8adcadb2fc0f832b584be24c Mon Sep 17 00:00:00 2001 From: Dmitry Shlagoff Date: Tue, 29 Jan 2019 21:32:41 +0700 Subject: [PATCH] Fix compatibility issues with syntax and Bound --- serde/build.rs | 15 +++++++++++++++ serde/src/de/impls.rs | 14 ++++++++------ serde/src/lib.rs | 11 ++++++++++- serde/src/ser/impls.rs | 3 ++- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/serde/build.rs b/serde/build.rs index c616d181..453514c7 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -14,6 +14,15 @@ fn main() { let target = env::var("TARGET").unwrap(); let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; + // std::collections::Bound was stabilized in Rust 1.17 + // but it was moved to core::ops later in Rust 1.26: + // https://doc.rust-lang.org/core/ops/enum.Bound.html + if minor >= 26 { + println!("cargo:rustc-cfg=ops_bound"); + } else if minor >= 17 && cfg!(feature = "std") { + println!("cargo:rustc-cfg=collections_bound"); + } + // CString::into_boxed_c_str stabilized in Rust 1.20: // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str if minor >= 20 { @@ -42,6 +51,12 @@ fn main() { println!("cargo:rustc-cfg=integer128"); } + // RangeToInclusive was stabilized in Rust 1.26: + // https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html + if minor >= 26 { + println!("cargo:rustc-cfg=range_to_inclusive"); + } + // Inclusive ranges methods stabilized in Rust 1.27: // https://github.com/rust-lang/rust/pull/50758 if minor >= 27 { diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 9da2b918..57d8307e 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -2153,7 +2153,7 @@ where range::UnboundedRangeVisitor { expecting: "struct RangeFrom", phantom: PhantomData, - field, + field: field, }, )?; Ok(start..) @@ -2175,13 +2175,14 @@ where range::UnboundedRangeVisitor { expecting: "struct RangeTo", phantom: PhantomData, - field, + field: field, }, )?; Ok(..end) } } +#[cfg(range_to_inclusive)] impl<'de, Idx> Deserialize<'de> for RangeToInclusive where Idx: Deserialize<'de>, @@ -2197,10 +2198,10 @@ where range::UnboundedRangeVisitor { expecting: "struct RangeToInclusive", phantom: PhantomData, - field, + field: field, }, )?; - Ok(..=end) + Ok(RangeToInclusive { end: end }) } } @@ -2226,14 +2227,14 @@ mod range { impl Field { fn name(&self) -> &'static str { - match self { + match *self { Field::Start => "start", Field::End => "end", } } pub fn name_slice(&self) -> &'static [&'static str] { - match self { + match *self { Field::Start => FIELDS_START_ONLY, Field::End => FIELD_END_ONLY, } @@ -2411,6 +2412,7 @@ mod range { //////////////////////////////////////////////////////////////////////////////// +#[cfg(any(ops_bound, collections_bound))] impl<'de, T> Deserialize<'de> for Bound where T: Deserialize<'de> diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 07202fa6..f242ca57 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -160,7 +160,7 @@ mod lib { pub use self::core::default::{self, Default}; pub use self::core::fmt::{self, Debug, Display}; pub use self::core::marker::{self, PhantomData}; - pub use self::core::ops::{Range, Bound, RangeFrom, RangeTo, RangeToInclusive}; + pub use self::core::ops::{Range, RangeFrom, RangeTo}; pub use self::core::option::{self, Option}; pub use self::core::result::{self, Result}; @@ -224,6 +224,15 @@ mod lib { #[cfg(range_inclusive)] pub use self::core::ops::RangeInclusive; + + #[cfg(all(feature = "std", collections_bound))] + pub use std::collections::Bound; + + #[cfg(ops_bound)] + pub use self::core::ops::Bound; + + #[cfg(range_to_inclusive)] + pub use self::core::ops::RangeToInclusive; } //////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index c290889b..0adfd535 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -289,7 +289,7 @@ where } //////////////////////////////////////////////////////////////////////////////// - +#[cfg(range_to_inclusive)] impl Serialize for RangeToInclusive where Idx: Serialize, @@ -307,6 +307,7 @@ where //////////////////////////////////////////////////////////////////////////////// +#[cfg(any(ops_bound, collections_bound))] impl Serialize for Bound where T: Serialize,