Skip to content

Commit

Permalink
Implement for std::num::NonZero* on Rust 1.28+
Browse files Browse the repository at this point in the history
… regardless of the `unstable` feature. Fix serde-rs#1274.
  • Loading branch information
SimonSapin committed May 24, 2018
1 parent 0c5f20c commit c29be4d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 6 additions & 0 deletions serde/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ fn main() {
if minor >= 26 {
println!("cargo:rustc-cfg=integer128");
}

// Non-zero integers stabilized in Rust 1.28:
// https://github.com/rust-lang/rust/pull/50808
if minor >= 28 {
println!("cargo:rustc-cfg=num_nonzero");
}
}
8 changes: 4 additions & 4 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,16 +2006,16 @@ where
////////////////////////////////////////////////////////////////////////////////

macro_rules! nonzero_integers {
( $( $T: ty, )+ ) => {
( $( $T: ident, )+ ) => {
$(
#[cfg(feature = "unstable")]
impl<'de> Deserialize<'de> for $T {
#[cfg(any(num_nonzero, feature = "unstable"))]
impl<'de> Deserialize<'de> for num::$T {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = try!(Deserialize::deserialize(deserializer));
match <$T>::new(value) {
match <num::$T>::new(value) {
Some(nonzero) => Ok(nonzero),
None => Err(Error::custom("expected a non-zero value")),
}
Expand Down
5 changes: 1 addition & 4 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod lib {
pub use std::*;
}

pub use self::core::{cmp, iter, mem, ops, slice, str};
pub use self::core::{cmp, iter, mem, num, ops, slice, str};
pub use self::core::{f32, f64};
pub use self::core::{i16, i32, i64, i8, isize};
pub use self::core::{u16, u32, u64, u8, usize};
Expand Down Expand Up @@ -212,9 +212,6 @@ mod lib {
pub use std::sync::{Mutex, RwLock};
#[cfg(feature = "std")]
pub use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[cfg(feature = "unstable")]
pub use core::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ where
macro_rules! nonzero_integers {
( $( $T: ident, )+ ) => {
$(
#[cfg(feature = "unstable")]
impl Serialize for $T {
#[cfg(any(num_nonzero, feature = "unstable"))]
impl Serialize for num::$T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down

0 comments on commit c29be4d

Please sign in to comment.