Skip to content

Commit

Permalink
impl Serialize and Deserialize for std::num::NonZero*
Browse files Browse the repository at this point in the history
… gated on the `unstable` Cargo feature.

These are new standard library types. Tracking issue:
rust-lang/rust#49137
  • Loading branch information
SimonSapin committed Mar 24, 2018
1 parent 2b18b57 commit c24c649
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion serde/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "1.0.34" # remember to update html_root_url
version = "1.0.35" # remember to update html_root_url
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
Expand Down
33 changes: 33 additions & 0 deletions serde/src/de/impls.rs
Expand Up @@ -1918,6 +1918,7 @@ where
////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "unstable")]
#[allow(deprecated)]
impl<'de, T> Deserialize<'de> for NonZero<T>
where
T: Deserialize<'de> + Zeroable,
Expand All @@ -1934,6 +1935,38 @@ where
}
}

macro_rules! nonzero_integers {
( $( $T: ident, )+ ) => {
nonzero_integers!(@ $(::std::num::$T,)+);
};
( @ $( $T: ty, )+ ) => {
$(
#[cfg(feature = "unstable")]
impl<'de> Deserialize<'de> for $T {
fn deserialize<D>(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")),
}
}
}
)+
}
}

nonzero_integers! {
NonZeroU8, NonZeroI8,
NonZeroU16, NonZeroI16,
NonZeroU32, NonZeroI32,
NonZeroU64, NonZeroI64,
// FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, NonZeroI128,
NonZeroUsize, NonZeroIsize,
}

////////////////////////////////////////////////////////////////////////////////

impl<'de, T, E> Deserialize<'de> for Result<T, E>
Expand Down
1 change: 1 addition & 0 deletions serde/src/de/mod.rs
Expand Up @@ -99,6 +99,7 @@
//! - PathBuf
//! - Range\<T\>
//! - NonZero\<T\> (unstable)
//! - num::NonZero* (unstable)
//! - **Net types**:
//! - IpAddr
//! - Ipv4Addr
Expand Down
3 changes: 2 additions & 1 deletion serde/src/lib.rs
Expand Up @@ -79,7 +79,7 @@
////////////////////////////////////////////////////////////////////////////////

// Serde types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/serde/1.0.34")]
#![doc(html_root_url = "https://docs.rs/serde/1.0.35")]
// Support using Serde without the standard library!
#![cfg_attr(not(feature = "std"), no_std)]
// Unstable functionality only if the user asks for it. For tracking and
Expand Down Expand Up @@ -211,6 +211,7 @@ mod lib {
pub use std::sync::{Mutex, RwLock};

#[cfg(feature = "unstable")]
#[allow(deprecated)]
pub use core::nonzero::{NonZero, Zeroable};
}

Expand Down
26 changes: 26 additions & 0 deletions serde/src/ser/impls.rs
Expand Up @@ -351,6 +351,7 @@ deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwne
////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "unstable")]
#[allow(deprecated)]
impl<T> Serialize for NonZero<T>
where
T: Serialize + Zeroable + Clone,
Expand All @@ -363,6 +364,31 @@ where
}
}

macro_rules! nonzero_integers {
( $( $T: ident, )+ ) => {
$(
#[cfg(feature = "unstable")]
impl Serialize for ::std::num::$T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.clone().get().serialize(serializer)
}
}
)+
}
}

nonzero_integers! {
NonZeroU8, NonZeroI8,
NonZeroU16, NonZeroI16,
NonZeroU32, NonZeroI32,
NonZeroU64, NonZeroI64,
// FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, NonZeroI128,
NonZeroUsize, NonZeroIsize,
}

impl<T> Serialize for Cell<T>
where
T: Serialize + Copy,
Expand Down
1 change: 1 addition & 0 deletions serde/src/ser/mod.rs
Expand Up @@ -94,6 +94,7 @@
//! - PathBuf
//! - Range\<T\>
//! - NonZero\<T\> (unstable)
//! - num::NonZero* (unstable)
//! - **Net types**:
//! - IpAddr
//! - Ipv4Addr
Expand Down

0 comments on commit c24c649

Please sign in to comment.