mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-29 03:47:55 +00:00
Implement serialization for NonZero values in nightly.
This commit is contained in:
@@ -11,3 +11,7 @@ keywords = ["serialization"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num = "*"
|
num = "*"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
nightly = []
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use num::FromPrimitive;
|
use num::FromPrimitive;
|
||||||
|
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
use core::nonzero::{NonZero, Zeroable};
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
use std::num::Zero;
|
||||||
|
|
||||||
use de::{
|
use de::{
|
||||||
Deserialize,
|
Deserialize,
|
||||||
Deserializer,
|
Deserializer,
|
||||||
@@ -865,3 +870,18 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deser
|
|||||||
Ok(Cow::Owned(val))
|
Ok(Cow::Owned(val))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
|
||||||
|
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
|
||||||
|
let value = try!(Deserialize::deserialize(deserializer));
|
||||||
|
if value == Zero::zero() {
|
||||||
|
return Err(Error::syntax_error())
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
Ok(NonZero::new(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,13 @@
|
|||||||
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
||||||
//! type.
|
//! type.
|
||||||
#![doc(html_root_url="http://erickt.github.io/rust-serde")]
|
#![doc(html_root_url="http://erickt.github.io/rust-serde")]
|
||||||
|
#![cfg_attr(feature = "nightly", feature(core, nonzero, zero_one))]
|
||||||
|
|
||||||
extern crate num;
|
extern crate num;
|
||||||
|
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
extern crate core;
|
||||||
|
|
||||||
pub use ser::{Serialize, Serializer};
|
pub use ser::{Serialize, Serializer};
|
||||||
pub use de::{Deserialize, Deserializer, Error};
|
pub use de::{Deserialize, Deserializer, Error};
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ use std::path;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
use core::nonzero::{NonZero, Zeroable};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
Serialize,
|
Serialize,
|
||||||
Serializer,
|
Serializer,
|
||||||
@@ -545,3 +548,10 @@ impl Serialize for path::PathBuf {
|
|||||||
self.to_str().unwrap().serialize(serializer)
|
self.to_str().unwrap().serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
|
||||||
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||||
|
(**self).serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user