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
This commit is contained in:
Simon Sapin
2017-04-24 08:25:08 +09:00
parent d4d2061a2e
commit 1d6ecf3c2c
3 changed files with 17 additions and 12 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "0.9.14"
version = "0.9.15"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
+15 -10
View File
@@ -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<T>
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<Idx: Deserialize> Deserialize for std::ops::Range<Idx> {
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "unstable")]
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
impl<T> Deserialize for NonZero<T>
where T: Deserialize + PartialEq + Zeroable + Zero
where T: Deserialize + Zeroable
{
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, 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::<T>()).iter().all(|&b| b == 0) {
return Err(Error::custom("expected a non-zero value"));
}
// Waiting for a safe way to construct NonZero<T>:
// https://github.com/rust-lang/rust/issues/27730#issuecomment-269726075
Ok(NonZero::new(value))
}
}
}
+1 -1
View File
@@ -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))]