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

Implement From<RangeInclusive> for Uniform #566

Merged
merged 2 commits into from Jul 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -53,5 +54,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
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -117,8 +117,9 @@ 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.
- `simd_support` enables uniform sampling of SIMD types (integers and floats).
- `stdweb` enables support for `OsRng` on `wasm32-unknown-unknown` via `stdweb`
combined with `cargo-web`.
- `wasm-bindgen` enables support for `OsRng` on `wasm32-unknown-unknown` via
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);
}
}