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.
  • Loading branch information
SimonSapin authored and dtolnay committed Mar 25, 2018
1 parent 2b18b57 commit 05b22a0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
34 changes: 34 additions & 0 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
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,39 @@ where
}
}

macro_rules! nonzero_integers {
( @ $( $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")),
}
}
}
)+
};
( $( $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<T, E>
Expand Down
1 change: 1 addition & 0 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
//! - PathBuf
//! - Range\<T\>
//! - NonZero\<T\> (unstable)
//! - num::NonZero* (unstable)
//! - **Net types**:
//! - IpAddr
//! - Ipv4Addr
Expand Down
4 changes: 4 additions & 0 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
27 changes: 27 additions & 0 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
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,32 @@ where
}
}

macro_rules! nonzero_integers {
( $( $T: ident, )+ ) => {
$(
#[cfg(feature = "unstable")]
impl Serialize for ::lib::num::$T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<T> Serialize for Cell<T>
where
T: Serialize + Copy,
Expand Down
1 change: 1 addition & 0 deletions serde/src/ser/mod.rs
Original file line number Diff line number Diff line change
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 05b22a0

Please sign in to comment.