Skip to content

Commit c24c649

Browse files
committed
impl Serialize and Deserialize for std::num::NonZero*
… gated on the `unstable` Cargo feature. These are new standard library types. Tracking issue: rust-lang/rust#49137
1 parent 2b18b57 commit c24c649

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
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 = "1.0.34" # remember to update html_root_url
3+
version = "1.0.35" # remember to update html_root_url
44
authors = ["Erick Tryzelaar <[email protected]>", "David Tolnay <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "A generic serialization/deserialization framework"

serde/src/de/impls.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,7 @@ where
19181918
////////////////////////////////////////////////////////////////////////////////
19191919

19201920
#[cfg(feature = "unstable")]
1921+
#[allow(deprecated)]
19211922
impl<'de, T> Deserialize<'de> for NonZero<T>
19221923
where
19231924
T: Deserialize<'de> + Zeroable,
@@ -1934,6 +1935,38 @@ where
19341935
}
19351936
}
19361937

1938+
macro_rules! nonzero_integers {
1939+
( $( $T: ident, )+ ) => {
1940+
nonzero_integers!(@ $(::std::num::$T,)+);
1941+
};
1942+
( @ $( $T: ty, )+ ) => {
1943+
$(
1944+
#[cfg(feature = "unstable")]
1945+
impl<'de> Deserialize<'de> for $T {
1946+
fn deserialize<D>(deserializer: D) -> Result<$T, D::Error>
1947+
where
1948+
D: Deserializer<'de>,
1949+
{
1950+
let value = try!(Deserialize::deserialize(deserializer));
1951+
match <$T>::new(value) {
1952+
Some(nonzero) => Ok(nonzero),
1953+
None => Err(Error::custom("expected a non-zero value")),
1954+
}
1955+
}
1956+
}
1957+
)+
1958+
}
1959+
}
1960+
1961+
nonzero_integers! {
1962+
NonZeroU8, NonZeroI8,
1963+
NonZeroU16, NonZeroI16,
1964+
NonZeroU32, NonZeroI32,
1965+
NonZeroU64, NonZeroI64,
1966+
// FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, NonZeroI128,
1967+
NonZeroUsize, NonZeroIsize,
1968+
}
1969+
19371970
////////////////////////////////////////////////////////////////////////////////
19381971

19391972
impl<'de, T, E> Deserialize<'de> for Result<T, E>

serde/src/de/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
//! - PathBuf
100100
//! - Range\<T\>
101101
//! - NonZero\<T\> (unstable)
102+
//! - num::NonZero* (unstable)
102103
//! - **Net types**:
103104
//! - IpAddr
104105
//! - Ipv4Addr

serde/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
////////////////////////////////////////////////////////////////////////////////
8080

8181
// Serde types in rustdoc of other crates get linked to here.
82-
#![doc(html_root_url = "https://docs.rs/serde/1.0.34")]
82+
#![doc(html_root_url = "https://docs.rs/serde/1.0.35")]
8383
// Support using Serde without the standard library!
8484
#![cfg_attr(not(feature = "std"), no_std)]
8585
// Unstable functionality only if the user asks for it. For tracking and
@@ -211,6 +211,7 @@ mod lib {
211211
pub use std::sync::{Mutex, RwLock};
212212

213213
#[cfg(feature = "unstable")]
214+
#[allow(deprecated)]
214215
pub use core::nonzero::{NonZero, Zeroable};
215216
}
216217

serde/src/ser/impls.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwne
351351
////////////////////////////////////////////////////////////////////////////////
352352

353353
#[cfg(feature = "unstable")]
354+
#[allow(deprecated)]
354355
impl<T> Serialize for NonZero<T>
355356
where
356357
T: Serialize + Zeroable + Clone,
@@ -363,6 +364,31 @@ where
363364
}
364365
}
365366

367+
macro_rules! nonzero_integers {
368+
( $( $T: ident, )+ ) => {
369+
$(
370+
#[cfg(feature = "unstable")]
371+
impl Serialize for ::std::num::$T {
372+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
373+
where
374+
S: Serializer,
375+
{
376+
self.clone().get().serialize(serializer)
377+
}
378+
}
379+
)+
380+
}
381+
}
382+
383+
nonzero_integers! {
384+
NonZeroU8, NonZeroI8,
385+
NonZeroU16, NonZeroI16,
386+
NonZeroU32, NonZeroI32,
387+
NonZeroU64, NonZeroI64,
388+
// FIXME: https://github.com/serde-rs/serde/issues/1136 NonZeroU128, NonZeroI128,
389+
NonZeroUsize, NonZeroIsize,
390+
}
391+
366392
impl<T> Serialize for Cell<T>
367393
where
368394
T: Serialize + Copy,

serde/src/ser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
//! - PathBuf
9595
//! - Range\<T\>
9696
//! - NonZero\<T\> (unstable)
97+
//! - num::NonZero* (unstable)
9798
//! - **Net types**:
9899
//! - IpAddr
99100
//! - Ipv4Addr

0 commit comments

Comments
 (0)