Skip to content

Commit d719020

Browse files
committed
Make Uniform constructors return a result
- This is a breaking change. - The new error type had to be made public, otherwise `Uniform` could not be extended for user-defined types by implementing `UniformSampler`. - `rand_distr` was updated accordingly. - Also forbid unsafe code for crates where none is used. Fixes rust-random#1195, rust-random#1211.
1 parent 9b7a24a commit d719020

20 files changed

+201
-168
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
88

99
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.
1010

11+
## [0.9.0] - unreleased
12+
### Distributions
13+
- `{Uniform, UniformSampler}::{new, new_inclusive}` return a `Result` (instead of potentially panicking)
14+
- `Uniform` implements `TryFrom` instead of `From` for ranges
15+
1116
## [0.8.5] - 2021-08-20
1217
### Fixes
1318
- Fix build on non-32/64-bit architectures (#1144)

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand"
3-
version = "0.8.5"
3+
version = "0.9.0"
44
authors = ["The Rand Project Developers", "The Rust Project Developers"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"

examples/monte-carlo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use rand::distributions::{Distribution, Uniform};
3030

3131
fn main() {
32-
let range = Uniform::new(-1.0f64, 1.0);
32+
let range = Uniform::new(-1.0f64, 1.0).unwrap();
3333
let mut rng = rand::thread_rng();
3434

3535
let total = 1_000_000;

examples/monty-hall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn main() {
8080
let num_simulations = 10000;
8181

8282
let mut rng = rand::thread_rng();
83-
let random_door = Uniform::new(0u32, 3);
83+
let random_door = Uniform::new(0u32, 3).unwrap();
8484

8585
let (mut switch_wins, mut switch_losses) = (0, 0);
8686
let (mut keep_wins, mut keep_losses) = (0, 0);

rand_distr/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
- Upgrade Rand
9+
710
## [0.4.3] - 2021-12-30
811
- Fix `no_std` build (#1208)
912

rand_distr/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ std_math = ["num-traits/std"]
2323
serde1 = ["serde", "rand/serde1"]
2424

2525
[dependencies]
26-
rand = { path = "..", version = "0.8.0", default-features = false }
26+
rand = { path = "..", version = "0.9.0", default-features = false }
2727
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
2828
serde = { version = "1.0.103", features = ["derive"], optional = true }
2929

3030
[dev-dependencies]
3131
rand_pcg = { version = "0.3.0", path = "../rand_pcg" }
3232
# For inline examples
33-
rand = { path = "..", version = "0.8.0", default-features = false, features = ["std_rng", "std", "small_rng"] }
33+
rand = { path = "..", version = "0.9.0", default-features = false, features = ["std_rng", "std", "small_rng"] }
3434
# Histogram implementation for testing uniformity
3535
average = { version = "0.13", features = [ "std" ] }
3636
# Special functions for testing distributions

rand_distr/src/binomial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ impl Distribution<u64> for Binomial {
163163
// return value
164164
let mut y: i64;
165165

166-
let gen_u = Uniform::new(0., p4);
167-
let gen_v = Uniform::new(0., 1.);
166+
let gen_u = Uniform::new(0., p4).unwrap();
167+
let gen_v = Uniform::new(0., 1.).unwrap();
168168

169169
loop {
170170
// Step 1: Generate `u` for selecting the region. If region 1 is

rand_distr/src/hypergeometric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl Distribution<u64> for Hypergeometric {
251251
x
252252
},
253253
RejectionAcceptance { m, a, lambda_l, lambda_r, x_l, x_r, p1, p2, p3 } => {
254-
let distr_region_select = Uniform::new(0.0, p3);
254+
let distr_region_select = Uniform::new(0.0, p3).unwrap();
255255
loop {
256256
let (y, v) = loop {
257257
let u = distr_region_select.sample(rng);

rand_distr/src/unit_ball.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct UnitBall;
3131
impl<F: Float + SampleUniform> Distribution<[F; 3]> for UnitBall {
3232
#[inline]
3333
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [F; 3] {
34-
let uniform = Uniform::new(F::from(-1.).unwrap(), F::from(1.).unwrap());
34+
let uniform = Uniform::new(F::from(-1.).unwrap(), F::from(1.).unwrap()).unwrap();
3535
let mut x1;
3636
let mut x2;
3737
let mut x3;

rand_distr/src/unit_circle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct UnitCircle;
3535
impl<F: Float + SampleUniform> Distribution<[F; 2]> for UnitCircle {
3636
#[inline]
3737
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [F; 2] {
38-
let uniform = Uniform::new(F::from(-1.).unwrap(), F::from(1.).unwrap());
38+
let uniform = Uniform::new(F::from(-1.).unwrap(), F::from(1.).unwrap()).unwrap();
3939
let mut x1;
4040
let mut x2;
4141
let mut sum;

0 commit comments

Comments
 (0)