From 6b9604c2ae830c20ad6f68f95260f848ef29c081 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 21 May 2018 18:01:42 -0700 Subject: [PATCH] Update to rand 0.5 --- .travis.yml | 4 ++-- Cargo.toml | 4 ++-- ci/rustup.sh | 4 ++-- ci/test_full.sh | 7 +++++-- src/crand.rs | 26 ++++++++++++++++++++++++++ src/lib.rs | 12 +++--------- 6 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/crand.rs diff --git a/.travis.yml b/.travis.yml index 8a4946c..9539d57 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: rust rust: - 1.15.0 - - 1.20.0 - - 1.25.0 + - 1.22.0 + - 1.26.0 - stable - beta - nightly diff --git a/Cargo.toml b/Cargo.toml index 4ef8c10..8a23217 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ readme = "README.md" build = "build.rs" [package.metadata.docs.rs] -features = ["std"] +features = ["std", "serde", "rand"] [dependencies] @@ -29,7 +29,7 @@ default-features = false [dependencies.rand] optional = true -version = "0.4" +version = "0.5" default-features = false [features] diff --git a/ci/rustup.sh b/ci/rustup.sh index e8d1ba7..c5aea79 100755 --- a/ci/rustup.sh +++ b/ci/rustup.sh @@ -1,11 +1,11 @@ #!/bin/sh # Use rustup to locally run the same suite of tests as .travis.yml. -# (You should first install/update 1.15.0, stable, beta, and nightly.) +# (You should first install/update all versions listed below.) set -ex export TRAVIS_RUST_VERSION -for TRAVIS_RUST_VERSION in 1.15.0 1.20.0 1.25.0 stable beta nightly; do +for TRAVIS_RUST_VERSION in 1.15.0 1.22.0 1.26.0 stable beta nightly; do run="rustup run $TRAVIS_RUST_VERSION" $run cargo build --verbose $run $PWD/ci/test_full.sh diff --git a/ci/test_full.sh b/ci/test_full.sh index b8c3a2b..0cf5209 100755 --- a/ci/test_full.sh +++ b/ci/test_full.sh @@ -4,8 +4,11 @@ set -ex echo Testing num-complex on rustc ${TRAVIS_RUST_VERSION} -FEATURES="std rand serde" -if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable)$ ]]; then +FEATURES="std serde" +if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.26|1.22)$ ]]; then + FEATURES="$FEATURES rand" +fi +if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.26)$ ]]; then FEATURES="$FEATURES i128" fi diff --git a/src/crand.rs b/src/crand.rs new file mode 100644 index 0000000..4fb112a --- /dev/null +++ b/src/crand.rs @@ -0,0 +1,26 @@ +//! Rand implementations for complex numbers + +use Complex; +use rand::distributions::Standard; +use rand::prelude::*; +use traits::Num; + +impl Distribution> for Standard +where + T: Num + Clone, + Standard: Distribution, +{ + fn sample(&self, rng: &mut R) -> Complex { + Complex::new(self.sample(rng), self.sample(rng)) + } +} + +#[test] +fn standard_f64() { + let mut rng = SmallRng::from_seed([42; 16]); + for _ in 0..100 { + let c: Complex = rng.gen(); + assert!(c.re >= 0.0 && c.re < 1.0); + assert!(c.im >= 0.0 && c.im < 1.0); + } +} diff --git a/src/lib.rs b/src/lib.rs index 4d068c7..8a6eb89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,9 @@ use traits::{Zero, One, Num, Inv}; use traits::float::Float; use traits::float::FloatCore; +#[cfg(feature = "rand")] +mod crand; + // FIXME #1284: handle complex NaN & infinity etc. This // probably doesn't map to C's _Complex correctly. @@ -1161,15 +1164,6 @@ impl<'de, T> serde::Deserialize<'de> for Complex where } } -#[cfg(feature = "rand")] -impl rand::Rand for Complex where - T: rand::Rand + Num + Clone -{ - fn rand(rng: &mut R) -> Self { - Self::new(rng.gen::(), rng.gen::()) - } -} - #[derive(Debug, PartialEq)] pub struct ParseComplexError {