From b6c4cfec37ed1e72d72946042c54d389b56039ce Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 3 Jun 2018 00:55:58 -0700 Subject: [PATCH] Support Duration in no-std mode on new compilers --- serde/build.rs | 6 ++++++ serde/src/de/impls.rs | 6 +++--- serde/src/lib.rs | 5 ++++- serde/src/ser/impls.rs | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/serde/build.rs b/serde/build.rs index ac0549f9..a187cf85 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -24,6 +24,12 @@ fn main() { println!("cargo:rustc-cfg=de_rc_dst"); } + // Duration available in core since Rust 1.25: + // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations + if minor >= 25 { + println!("cargo:rustc-cfg=core_duration"); + } + // 128-bit integers stabilized in Rust 1.26: // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html if minor >= 26 { diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 579ee594..20e17373 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -12,7 +12,7 @@ use de::{ Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess, Visitor, }; -#[cfg(any(feature = "std", feature = "alloc"))] +#[cfg(any(core_duration, feature = "std", feature = "alloc"))] use de::MapAccess; use de::from_primitive::FromPrimitive; @@ -1694,7 +1694,7 @@ forwarded_impl!((T), RwLock, RwLock::new); // secs: u64, // nanos: u32, // } -#[cfg(feature = "std")] +#[cfg(any(core_duration, feature = "std"))] impl<'de> Deserialize<'de> for Duration { fn deserialize(deserializer: D) -> Result where @@ -1742,7 +1742,7 @@ impl<'de> Deserialize<'de> for Duration { b"secs" => Ok(Field::Secs), b"nanos" => Ok(Field::Nanos), _ => { - let value = String::from_utf8_lossy(value); + let value = ::export::from_utf8_lossy(value); Err(Error::unknown_field(&value, FIELDS)) } } diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 148e1ac4..90a88744 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -211,7 +211,10 @@ mod lib { #[cfg(feature = "std")] pub use std::sync::{Mutex, RwLock}; #[cfg(feature = "std")] - pub use std::time::{Duration, SystemTime, UNIX_EPOCH}; + pub use std::time::{SystemTime, UNIX_EPOCH}; + + #[cfg(any(core_duration, feature = "std"))] + pub use self::core::time::Duration; } //////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 3a4a4efe..0b21e541 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -527,7 +527,7 @@ where //////////////////////////////////////////////////////////////////////////////// -#[cfg(feature = "std")] +#[cfg(any(core_duration, feature = "std"))] impl Serialize for Duration { fn serialize(&self, serializer: S) -> Result where