From 1d6ecf3c2cb046f32ded7f6c1ba4e6f73433c4c3 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 24 Apr 2017 08:25:08 +0900 Subject: [PATCH] Remove usage of unstable core::num::Zero, which was removed upstream. https://github.com/rust-lang/rust/pull/41437 Backport of https://github.com/serde-rs/serde/pull/898 to 0.9.x --- serde/Cargo.toml | 2 +- serde/src/de/impls.rs | 25 +++++++++++++++---------- serde/src/lib.rs | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 1c7e1c41..7d0330fb 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde" -version = "0.9.14" +version = "0.9.15" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "A generic serialization/deserialization framework" diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index c67a1572..dbdcc01d 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -20,10 +20,14 @@ use core::fmt; #[cfg(feature = "std")] use core::hash::{Hash, BuildHasher}; use core::marker::PhantomData; +#[cfg(all(feature="unstable"))] +use core::mem; #[cfg(feature = "std")] use std::net; #[cfg(feature = "std")] use std::path; +#[cfg(all(feature="unstable"))] +use core::slice; use core::str; #[cfg(feature = "std")] use std::ffi::{CString, OsString}; @@ -57,10 +61,6 @@ use std; #[cfg(feature = "unstable")] use core::nonzero::{NonZero, Zeroable}; -#[cfg(feature = "unstable")] -#[allow(deprecated)] // required for impl Deserialize for NonZero -use core::num::Zero; - use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor, Unexpected, VariantVisitor, Visitor}; use de::from_primitive::FromPrimitive; @@ -1409,18 +1409,23 @@ impl Deserialize for std::ops::Range { /////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "unstable")] -#[allow(deprecated)] // num::Zero is deprecated but there is no replacement impl Deserialize for NonZero - where T: Deserialize + PartialEq + Zeroable + Zero + where T: Deserialize + Zeroable { fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer { let value = try!(Deserialize::deserialize(deserializer)); - if value == Zero::zero() { - return Err(Error::custom("expected a non-zero value")); - } - unsafe { Ok(NonZero::new(value)) } + unsafe { + let ptr = &value as *const T as *const u8; + if slice::from_raw_parts(ptr, mem::size_of::()).iter().all(|&b| b == 0) { + return Err(Error::custom("expected a non-zero value")); + } + // Waiting for a safe way to construct NonZero: + // https://github.com/rust-lang/rust/issues/27730#issuecomment-269726075 + Ok(NonZero::new(value)) + } + } } diff --git a/serde/src/lib.rs b/serde/src/lib.rs index cfd2f7d0..ec11a462 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -61,7 +61,7 @@ #![doc(html_root_url="https://docs.serde.rs")] #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(feature = "unstable", feature(nonzero, specialization, zero_one, into_boxed_c_str))] +#![cfg_attr(feature = "unstable", feature(nonzero, specialization, into_boxed_c_str))] #![cfg_attr(feature = "alloc", feature(alloc))] #![cfg_attr(feature = "collections", feature(collections))] #![cfg_attr(feature = "cargo-clippy", allow(linkedlist, type_complexity, doc_markdown))]