Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to rand 0.5 #28

Merged
merged 1 commit into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
build = "build.rs"

[package.metadata.docs.rs]
features = ["std"]
features = ["std", "serde", "rand"]

[dependencies]

Expand All @@ -29,7 +29,7 @@ default-features = false

[dependencies.rand]
optional = true
version = "0.4"
version = "0.5"
default-features = false

[features]
Expand Down
4 changes: 2 additions & 2 deletions ci/rustup.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 5 additions & 2 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions src/crand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Rand implementations for complex numbers

use Complex;
use rand::distributions::Standard;
use rand::prelude::*;
use traits::Num;

impl<T> Distribution<Complex<T>> for Standard
where
T: Num + Clone,
Standard: Distribution<T>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Complex<T> {
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<f64> = rng.gen();
assert!(c.re >= 0.0 && c.re < 1.0);
assert!(c.im >= 0.0 && c.im < 1.0);
}
}
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -1161,15 +1164,6 @@ impl<'de, T> serde::Deserialize<'de> for Complex<T> where
}
}

#[cfg(feature = "rand")]
impl<T> rand::Rand for Complex<T> where
T: rand::Rand + Num + Clone
{
fn rand<R:rand::Rng>(rng: &mut R) -> Self {
Self::new(rng.gen::<T>(), rng.gen::<T>())
}
}

#[derive(Debug, PartialEq)]
pub struct ParseComplexError<E>
{
Expand Down