Skip to content

Commit 1d6ecf3

Browse files
committed
Remove usage of unstable core::num::Zero, which was removed upstream.
rust-lang/rust#41437 Backport of serde-rs#898 to 0.9.x
1 parent d4d2061 commit 1d6ecf3

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

serde/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "serde"
3-
version = "0.9.14"
3+
version = "0.9.15"
44
authors = ["Erick Tryzelaar <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "A generic serialization/deserialization framework"

serde/src/de/impls.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ use core::fmt;
2020
#[cfg(feature = "std")]
2121
use core::hash::{Hash, BuildHasher};
2222
use core::marker::PhantomData;
23+
#[cfg(all(feature="unstable"))]
24+
use core::mem;
2325
#[cfg(feature = "std")]
2426
use std::net;
2527
#[cfg(feature = "std")]
2628
use std::path;
29+
#[cfg(all(feature="unstable"))]
30+
use core::slice;
2731
use core::str;
2832
#[cfg(feature = "std")]
2933
use std::ffi::{CString, OsString};
@@ -57,10 +61,6 @@ use std;
5761
#[cfg(feature = "unstable")]
5862
use core::nonzero::{NonZero, Zeroable};
5963

60-
#[cfg(feature = "unstable")]
61-
#[allow(deprecated)] // required for impl Deserialize for NonZero<T>
62-
use core::num::Zero;
63-
6464
use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor, Unexpected,
6565
VariantVisitor, Visitor};
6666
use de::from_primitive::FromPrimitive;
@@ -1409,18 +1409,23 @@ impl<Idx: Deserialize> Deserialize for std::ops::Range<Idx> {
14091409
///////////////////////////////////////////////////////////////////////////////
14101410

14111411
#[cfg(feature = "unstable")]
1412-
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
14131412
impl<T> Deserialize for NonZero<T>
1414-
where T: Deserialize + PartialEq + Zeroable + Zero
1413+
where T: Deserialize + Zeroable
14151414
{
14161415
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error>
14171416
where D: Deserializer
14181417
{
14191418
let value = try!(Deserialize::deserialize(deserializer));
1420-
if value == Zero::zero() {
1421-
return Err(Error::custom("expected a non-zero value"));
1422-
}
1423-
unsafe { Ok(NonZero::new(value)) }
1419+
unsafe {
1420+
let ptr = &value as *const T as *const u8;
1421+
if slice::from_raw_parts(ptr, mem::size_of::<T>()).iter().all(|&b| b == 0) {
1422+
return Err(Error::custom("expected a non-zero value"));
1423+
}
1424+
// Waiting for a safe way to construct NonZero<T>:
1425+
// https://github.com/rust-lang/rust/issues/27730#issuecomment-269726075
1426+
Ok(NonZero::new(value))
1427+
}
1428+
14241429
}
14251430
}
14261431

serde/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
6262
#![doc(html_root_url="https://docs.serde.rs")]
6363
#![cfg_attr(not(feature = "std"), no_std)]
64-
#![cfg_attr(feature = "unstable", feature(nonzero, specialization, zero_one, into_boxed_c_str))]
64+
#![cfg_attr(feature = "unstable", feature(nonzero, specialization, into_boxed_c_str))]
6565
#![cfg_attr(feature = "alloc", feature(alloc))]
6666
#![cfg_attr(feature = "collections", feature(collections))]
6767
#![cfg_attr(feature = "cargo-clippy", allow(linkedlist, type_complexity, doc_markdown))]

0 commit comments

Comments
 (0)