Support Duration in no-std mode on new compilers

This commit is contained in:
David Tolnay 2018-06-03 00:55:58 -07:00
parent 94853752a1
commit b6c4cfec37
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 14 additions and 5 deletions

View File

@ -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 {

View File

@ -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<T>, RwLock::new);
// secs: u64,
// nanos: u32,
// }
#[cfg(feature = "std")]
#[cfg(any(core_duration, feature = "std"))]
impl<'de> Deserialize<'de> for Duration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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))
}
}

View File

@ -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;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -527,7 +527,7 @@ where
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
#[cfg(any(core_duration, feature = "std"))]
impl Serialize for Duration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where