Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "0.9.14"
version = "0.9.15"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
Expand Down
25 changes: 15 additions & 10 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down