Skip to content

Commit

Permalink
Implement From<RangeInclusive> for Uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Jul 23, 2018
1 parent 686ba14 commit 35bf020
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
i128_support = [] # enables i128 and u128 support
simd_support = [] # enables SIMD support
serde1 = ["serde", "serde_derive", "rand_core/serde1"] # enables serialization for PRNGs
rust_1_27 = [] # enables RangeInclusive support for Rust >= 1.27

[workspace]
members = ["rand_core", "rand_isaac"]
Expand Down
19 changes: 19 additions & 0 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ impl<X: SampleUniform> From<::core::ops::Range<X>> for Uniform<X> {
}
}

#[cfg(feature = "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(feature = "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 + 1e15);
}
}

0 comments on commit 35bf020

Please sign in to comment.