From 05b22a06d71064ac3e6c9d88b5dd23f9cedcf3d9 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 24 Mar 2018 10:39:40 +0100 Subject: [PATCH] impl Serialize and Deserialize for std::num::NonZero* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … gated on the `unstable` Cargo feature. These are new standard library types. --- serde/src/de/impls.rs | 34 ++++++++++++++++++++++++++++++++++ serde/src/de/mod.rs | 1 + serde/src/lib.rs | 4 ++++ serde/src/ser/impls.rs | 27 +++++++++++++++++++++++++++ serde/src/ser/mod.rs | 1 + 5 files changed, 67 insertions(+) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index d872cbe61..5ea67a21a 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -1918,6 +1918,7 @@ where //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "unstable")] +#[allow(deprecated)] impl<'de, T> Deserialize<'de> for NonZero where T: Deserialize<'de> + Zeroable, @@ -1934,6 +1935,39 @@ where } } +macro_rules! nonzero_integers { + ( @ $( $T: ty, )+ ) => { + $( + #[cfg(feature = "unstable")] + impl<'de> Deserialize<'de> for $T { + fn deserialize(deserializer: D) -> Result<$T, D::Error> + where + D: Deserializer<'de>, + { + let value = try!(Deserialize::deserialize(deserializer)); + match <$T>::new(value) { + Some(nonzero) => Ok(nonzero), + None => Err(Error::custom("expected a non-zero value")), + } + } + } + )+ + }; + ( $( $T: ident, )+ ) => { + nonzero_integers!(@ $(::lib::num::$T,)+ ); + } +} + +nonzero_integers! { + // Not including signed NonZeroI* since they might be removed + NonZeroU8, + NonZeroU16, + NonZeroU32, + NonZeroU64, + // FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, + NonZeroUsize, +} + //////////////////////////////////////////////////////////////////////////////// impl<'de, T, E> Deserialize<'de> for Result diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index cc2c65024..d3c4d6492 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -99,6 +99,7 @@ //! - PathBuf //! - Range\ //! - NonZero\ (unstable) +//! - num::NonZero* (unstable) //! - **Net types**: //! - IpAddr //! - Ipv4Addr diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 727897c0d..b42c0f392 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -211,7 +211,11 @@ mod lib { pub use std::sync::{Mutex, RwLock}; #[cfg(feature = "unstable")] + #[allow(deprecated)] pub use core::nonzero::{NonZero, Zeroable}; + + #[cfg(feature = "unstable")] + pub use core::num; } //////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index f62a3b2cb..bba65a712 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -351,6 +351,7 @@ deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwne //////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "unstable")] +#[allow(deprecated)] impl Serialize for NonZero where T: Serialize + Zeroable + Clone, @@ -363,6 +364,32 @@ where } } +macro_rules! nonzero_integers { + ( $( $T: ident, )+ ) => { + $( + #[cfg(feature = "unstable")] + impl Serialize for ::lib::num::$T { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.get().serialize(serializer) + } + } + )+ + } +} + +nonzero_integers! { + // Not including signed NonZeroI* since they might be removed + NonZeroU8, + NonZeroU16, + NonZeroU32, + NonZeroU64, + // FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, + NonZeroUsize, +} + impl Serialize for Cell where T: Serialize + Copy, diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index e23ef04aa..71274e365 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -94,6 +94,7 @@ //! - PathBuf //! - Range\ //! - NonZero\ (unstable) +//! - num::NonZero* (unstable) //! - **Net types**: //! - IpAddr //! - Ipv4Addr