From f3ffcfd61eefe2a4c69ce0f5dcbe819dc7881947 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Wed, 20 Feb 2019 04:30:53 +0300 Subject: [PATCH 1/2] impl Serialize and Deserialize for core::cmp::Reverse --- serde/src/de/impls.rs | 2 ++ serde/src/lib.rs | 1 + serde/src/ser/impls.rs | 13 +++++++++++++ 3 files changed, 16 insertions(+) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 13c8b5a7..9a036180 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -578,6 +578,8 @@ macro_rules! forwarded_impl { #[cfg(all(feature = "std", de_boxed_c_str))] forwarded_impl!((), Box, CString::into_boxed_c_str); +forwarded_impl!((T), Reverse, Reverse); + //////////////////////////////////////////////////////////////////////////////// struct OptionVisitor { diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 1d953d84..21a8ecd9 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -139,6 +139,7 @@ mod lib { pub use self::core::cell::{Cell, RefCell}; pub use self::core::clone::{self, Clone}; + pub use self::core::cmp::Reverse; pub use self::core::convert::{self, From, Into}; pub use self::core::default::{self, Default}; pub use self::core::fmt::{self, Debug, Display}; diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 8b40bda3..a8dd49bf 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -824,3 +824,16 @@ where self.0.serialize(serializer) } } + +impl Serialize for Reverse + where + T: Serialize, +{ + #[inline] + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.serialize(serializer) + } +} From 71fe2a55349b8384430417ebc20e05c35c4d69de Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Wed, 20 Feb 2019 04:53:34 +0300 Subject: [PATCH 2/2] Reverse impls available for Rust >=1.19 --- serde/build.rs | 6 ++++++ serde/src/de/impls.rs | 1 + serde/src/lib.rs | 1 + serde/src/ser/impls.rs | 1 + 4 files changed, 9 insertions(+) diff --git a/serde/build.rs b/serde/build.rs index 82129bc7..e27e2f34 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -23,6 +23,12 @@ fn main() { println!("cargo:rustc-cfg=collections_bound"); } + // core::cmp::Reverse stabilized in Rust 1.19: + // https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html + if minor >= 19 { + println!("cargo:rustc-cfg=core_reverse"); + } + // 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 { diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 9a036180..1c18fd96 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -578,6 +578,7 @@ macro_rules! forwarded_impl { #[cfg(all(feature = "std", de_boxed_c_str))] forwarded_impl!((), Box, CString::into_boxed_c_str); +#[cfg(core_reverse)] forwarded_impl!((T), Reverse, Reverse); //////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 21a8ecd9..17d07dc3 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -139,6 +139,7 @@ mod lib { pub use self::core::cell::{Cell, RefCell}; pub use self::core::clone::{self, Clone}; + #[cfg(core_reverse)] pub use self::core::cmp::Reverse; pub use self::core::convert::{self, From, Into}; pub use self::core::default::{self, Default}; diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index a8dd49bf..46d406b2 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -825,6 +825,7 @@ where } } +#[cfg(core_reverse)] impl Serialize for Reverse where T: Serialize,