Skip to content

Commit 1eaae98

Browse files
committed
Merge #14
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>`
2 parents 0443a5b + 1a2449c commit 1eaae98

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ features = ["std"]
2323
optional = true
2424
version = "1.0"
2525

26+
[dependencies.rand]
27+
optional = true
28+
version = "0.4"
29+
2630
[features]
2731
default = []
2832
unstable = []

ci/test_full.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ cargo build --no-default-features
1313
cargo test --no-default-features
1414

1515
# Each isolated feature should also work everywhere.
16-
for feature in serde; do
16+
for feature in rand serde; do
1717
cargo build --verbose --no-default-features --features="$feature"
1818
cargo test --verbose --no-default-features --features="$feature"
1919
done
20+
21+
cargo build --all-features
22+
cargo test --all-features

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ extern crate num_traits as traits;
2121
#[cfg(feature = "serde")]
2222
extern crate serde;
2323

24+
#[cfg(feature = "rand")]
25+
extern crate rand;
26+
2427
use std::error::Error;
2528
use std::fmt;
2629
#[cfg(test)]
@@ -1117,6 +1120,15 @@ impl<'de, T> serde::Deserialize<'de> for Complex<T> where
11171120
}
11181121
}
11191122

1123+
#[cfg(feature = "rand")]
1124+
impl<T> rand::Rand for Complex<T> where
1125+
T: rand::Rand + Num + Clone
1126+
{
1127+
fn rand<R:rand::Rng>(rng: &mut R) -> Self {
1128+
Self::new(rng.gen::<T>(), rng.gen::<T>())
1129+
}
1130+
}
1131+
11201132
#[derive(Debug, PartialEq)]
11211133
pub struct ParseComplexError<E>
11221134
{

0 commit comments

Comments
 (0)