From 9ae55fccde48a6836c24e20e6165de7828e88e15 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Wed, 21 Nov 2018 14:05:47 +0100 Subject: [PATCH] Support Duration with no_std for Rust >= 1.25 Fixes #641. --- build.rs | 3 +++ src/distributions/uniform.rs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index 584a38e62f0..e44b6436263 100644 --- a/build.rs +++ b/build.rs @@ -2,6 +2,9 @@ extern crate rustc_version; use rustc_version::{version, Version}; fn main() { + if version().unwrap() >= Version::parse("1.25.0").unwrap() { + println!("cargo:rustc-cfg=rust_1_25"); + } if version().unwrap() >= Version::parse("1.26.0").unwrap() { println!("cargo:rustc-cfg=rust_1_26"); } diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 2b712bb3d08..5fb89e34053 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -111,6 +111,8 @@ #[cfg(feature = "std")] use std::time::Duration; +#[cfg(all(not(feature = "std"), rust_1_25))] +use core::time::Duration; use Rng; use distributions::Distribution; @@ -833,14 +835,14 @@ uniform_float_impl! { f64x8, u64x8, f64, u64, 64 - 52 } /// /// [`UniformSampler`]: trait.UniformSampler.html /// [`Uniform`]: struct.Uniform.html -#[cfg(feature = "std")] +#[cfg(any(feature = "std", rust_1_25))] #[derive(Clone, Copy, Debug)] pub struct UniformDuration { mode: UniformDurationMode, offset: u32, } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", rust_1_25))] #[derive(Debug, Copy, Clone)] enum UniformDurationMode { Small { @@ -857,12 +859,12 @@ enum UniformDurationMode { } } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", rust_1_25))] impl SampleUniform for Duration { type Sampler = UniformDuration; } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", rust_1_25))] impl UniformSampler for UniformDuration { type X = Duration; @@ -1206,9 +1208,12 @@ mod tests { #[test] - #[cfg(feature = "std")] + #[cfg(any(feature = "std", rust_1_25))] fn test_durations() { + #[cfg(feature = "std")] use std::time::Duration; + #[cfg(all(not(feature = "std"), rust_1_25))] + use core::time::Duration; let mut rng = ::test::rng(253);