Skip to content

Commit

Permalink
Implement From<RangeInclusive> for Uniform
Browse files Browse the repository at this point in the history
Fixes #556.
  • Loading branch information
vks authored and dhardy committed Jul 27, 2018
1 parent 149c8b3 commit a7f8f34
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -12,6 +12,7 @@ Random number generators and other randomness functionality.
"""
keywords = ["random", "rng"]
categories = ["algorithms", "no-std"]
build = "build.rs"

[badges]
travis-ci = { repository = "rust-lang-nursery/rand" }
Expand Down Expand Up @@ -54,5 +55,8 @@ fuchsia-zircon = { version = "0.3.2", optional = true }
stdweb = { version = "0.4", optional = true }
wasm-bindgen = { version = "0.2.12", optional = true }

[build-dependencies]
rustc_version = "0.2"

[package.metadata.docs.rs]
all-features = true
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -117,7 +117,7 @@ optional features are available:
- `alloc` can be used instead of `std` to provide `Vec` and `Box`.
- `i128_support` enables support for generating `u128` and `i128` values.
- `log` enables some logging via the `log` crate.
- `nightly` enables all unstable features (`i128_support`).
- `nightly` enables all unstable features (`i128_support`, `simd_support`).
- `serde1` enables serialization for some types, via Serde version 1.
- `stdweb` enables support for `OsRng` on `wasm32-unknown-unknown` via `stdweb`
combined with `cargo-web`.
Expand Down
8 changes: 8 additions & 0 deletions build.rs
@@ -0,0 +1,8 @@
extern crate rustc_version;
use rustc_version::{version, Version};

fn main() {
if version().unwrap() >= Version::parse("1.27.0").unwrap() {
println!("cargo:rustc-cfg=rust_1_27");
}
}
19 changes: 19 additions & 0 deletions src/distributions/uniform.rs
Expand Up @@ -276,6 +276,13 @@ impl<X: SampleUniform> From<::core::ops::Range<X>> for Uniform<X> {
}
}

#[cfg(rust_1_27)]
impl<X: SampleUniform> From<::core::ops::RangeInclusive<X>> for Uniform<X> {
fn from(r: ::core::ops::RangeInclusive<X>) -> Uniform<X> {
Uniform::new_inclusive(r.start(), r.end())
}
}

/// Helper trait similar to [`Borrow`] but implemented
/// only for SampleUniform and references to SampleUniform in
/// order to resolve ambiguity issues.
Expand Down Expand Up @@ -1060,4 +1067,16 @@ mod tests {
assert_eq!(r.inner.low, 2.0);
assert_eq!(r.inner.scale, 5.0);
}

#[cfg(rust_1_27)]
#[test]
fn test_uniform_from_std_range_inclusive() {
let r = Uniform::from(2u32..=6);
assert_eq!(r.inner.low, 2);
assert_eq!(r.inner.range, 5);
let r = Uniform::from(2.0f64..=7.0);
assert_eq!(r.inner.low, 2.0);
assert!(r.inner.scale > 5.0);
assert!(r.inner.scale < 5.0 + 1e-14);
}
}

0 comments on commit a7f8f34

Please sign in to comment.