Skip to content

Commit

Permalink
Merge #14
Browse files Browse the repository at this point in the history
14: Implementing rand::Rand r=cuviper a=shingtaklam1324

Implements #12 .

Right now it is implemented with calling `rng.gen::<T>()` for both the real and imaginary parts. I'm not 100% sure on the mathematical randomness of the distribution if the complex number is used as a vector and the angle is wanted.

As well as that, for the dependency on `rand`, I just pulled the newest version on crates.io, but it may be better to use another version. I don't have many projects that use this so I'm not sure how big of an issue this will be.

Changes:

- Optional `rand` feature and dependency on `rand`
- `impl<T> rand::Rand for Complex<T>`
  • Loading branch information
bors[bot] committed Mar 6, 2018
2 parents 0443a5b + 1a2449c commit 1eaae98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ features = ["std"]
optional = true
version = "1.0"

[dependencies.rand]
optional = true
version = "0.4"

[features]
default = []
unstable = []
5 changes: 4 additions & 1 deletion ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ cargo build --no-default-features
cargo test --no-default-features

# Each isolated feature should also work everywhere.
for feature in serde; do
for feature in rand serde; do
cargo build --verbose --no-default-features --features="$feature"
cargo test --verbose --no-default-features --features="$feature"
done

cargo build --all-features
cargo test --all-features
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ extern crate num_traits as traits;
#[cfg(feature = "serde")]
extern crate serde;

#[cfg(feature = "rand")]
extern crate rand;

use std::error::Error;
use std::fmt;
#[cfg(test)]
Expand Down Expand Up @@ -1117,6 +1120,15 @@ 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

0 comments on commit 1eaae98

Please sign in to comment.