Skip to content

Commit 56f7625

Browse files
authored
Merge pull request #919 from kennytm/impl-error-for-distr-error
impl std::error::Error for rand_distr::*Error.
2 parents 5643fd5 + 413fc4b commit 56f7625

File tree

15 files changed

+192
-3
lines changed

15 files changed

+192
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ 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+
## Unreleased
12+
### Additions
13+
- Implement `std::error::Error` for `BernoulliError` (#919)
14+
1115
## [0.7.2] - 2019-09-16
1216
### Fixes
1317
- Fix dependency on `rand_core` 0.5.1 (#890)

rand_distr/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ 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+
- All error types now implement `std::error::Error` (#919)
9+
- Re-exported `rand::distributions::BernoulliError` (#919)
10+
711
## [0.2.2] - 2019-09-10
812
- Fix version requirement on rand lib (#847)
913
- Clippy fixes & suppression (#840)

rand_distr/src/binomial.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use rand::Rng;
1313
use crate::{Distribution, Uniform};
14+
use std::{error, fmt};
1415

1516
/// The binomial distribution `Binomial(n, p)`.
1617
///
@@ -43,6 +44,17 @@ pub enum Error {
4344
ProbabilityTooLarge,
4445
}
4546

47+
impl fmt::Display for Error {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
f.write_str(match self {
50+
Error::ProbabilityTooSmall => "p < 0 or is NaN in binomial distribution",
51+
Error::ProbabilityTooLarge => "p > 1 in binomial distribution",
52+
})
53+
}
54+
}
55+
56+
impl error::Error for Error {}
57+
4658
impl Binomial {
4759
/// Construct a new `Binomial` with the given shape parameters `n` (number
4860
/// of trials) and `p` (probability of success).

rand_distr/src/cauchy.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use rand::Rng;
1313
use crate::{Distribution, Standard};
1414
use crate::utils::Float;
15+
use std::{error, fmt};
1516

1617
/// The Cauchy distribution `Cauchy(median, scale)`.
1718
///
@@ -43,6 +44,16 @@ pub enum Error {
4344
ScaleTooSmall,
4445
}
4546

47+
impl fmt::Display for Error {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
f.write_str(match self {
50+
Error::ScaleTooSmall => "scale is not positive in Cauchy distribution",
51+
})
52+
}
53+
}
54+
55+
impl error::Error for Error {}
56+
4657
impl<N: Float> Cauchy<N>
4758
where Standard: Distribution<N>
4859
{

rand_distr/src/dirichlet.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use rand::Rng;
1313
use crate::{Distribution, Gamma, StandardNormal, Exp1, Open01};
1414
use crate::utils::Float;
15+
use std::{error, fmt};
1516

16-
/// The dirichelet distribution `Dirichlet(alpha)`.
17+
/// The Dirichlet distribution `Dirichlet(alpha)`.
1718
///
1819
/// The Dirichlet distribution is a family of continuous multivariate
1920
/// probability distributions parameterized by a vector alpha of positive reals.
@@ -46,6 +47,19 @@ pub enum Error {
4647
SizeTooSmall,
4748
}
4849

50+
impl fmt::Display for Error {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
f.write_str(match self {
53+
Error::AlphaTooShort | Error::SizeTooSmall => {
54+
"less than 2 dimensions in Dirichlet distribution"
55+
}
56+
Error::AlphaTooSmall => "alpha is not positive in Dirichlet distribution",
57+
})
58+
}
59+
}
60+
61+
impl error::Error for Error {}
62+
4963
impl<N: Float> Dirichlet<N>
5064
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
5165
{

rand_distr/src/exponential.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use rand::Rng;
1313
use crate::{ziggurat_tables, Distribution};
1414
use crate::utils::{ziggurat, Float};
15+
use std::{error, fmt};
1516

1617
/// Samples floating-point numbers according to the exponential distribution,
1718
/// with rate parameter `λ = 1`. This is equivalent to `Exp::new(1.0)` or
@@ -97,6 +98,16 @@ pub enum Error {
9798
LambdaTooSmall,
9899
}
99100

101+
impl fmt::Display for Error {
102+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103+
f.write_str(match self {
104+
Error::LambdaTooSmall => "lambda is not positive in exponential distribution",
105+
})
106+
}
107+
}
108+
109+
impl error::Error for Error {}
110+
100111
impl<N: Float> Exp<N>
101112
where Exp1: Distribution<N>
102113
{

rand_distr/src/gamma.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rand::Rng;
1616
use crate::normal::StandardNormal;
1717
use crate::{Distribution, Exp1, Exp, Open01};
1818
use crate::utils::Float;
19+
use std::{error, fmt};
1920

2021
/// The Gamma distribution `Gamma(shape, scale)` distribution.
2122
///
@@ -63,6 +64,18 @@ pub enum Error {
6364
ScaleTooLarge,
6465
}
6566

67+
impl fmt::Display for Error {
68+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69+
f.write_str(match self {
70+
Error::ShapeTooSmall => "shape is not positive in gamma distribution",
71+
Error::ScaleTooSmall => "scale is not positive in gamma distribution",
72+
Error::ScaleTooLarge => "scale is infinity in gamma distribution",
73+
})
74+
}
75+
}
76+
77+
impl error::Error for Error {}
78+
6679
#[derive(Clone, Copy, Debug)]
6780
enum GammaRepr<N> {
6881
Large(GammaLargeShape<N>),
@@ -224,6 +237,18 @@ pub enum ChiSquaredError {
224237
DoFTooSmall,
225238
}
226239

240+
impl fmt::Display for ChiSquaredError {
241+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
242+
f.write_str(match self {
243+
ChiSquaredError::DoFTooSmall => {
244+
"degrees-of-freedom k is not positive in chi-squared distribution"
245+
}
246+
})
247+
}
248+
}
249+
250+
impl error::Error for ChiSquaredError {}
251+
227252
#[derive(Clone, Copy, Debug)]
228253
enum ChiSquaredRepr<N> {
229254
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
@@ -298,6 +323,17 @@ pub enum FisherFError {
298323
NTooSmall,
299324
}
300325

326+
impl fmt::Display for FisherFError {
327+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328+
f.write_str(match self {
329+
FisherFError::MTooSmall => "m is not positive in Fisher F distribution",
330+
FisherFError::NTooSmall => "n is not positive in Fisher F distribution",
331+
})
332+
}
333+
}
334+
335+
impl error::Error for FisherFError {}
336+
301337
impl<N: Float> FisherF<N>
302338
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
303339
{
@@ -390,6 +426,17 @@ pub enum BetaError {
390426
BetaTooSmall,
391427
}
392428

429+
impl fmt::Display for BetaError {
430+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
431+
f.write_str(match self {
432+
BetaError::AlphaTooSmall => "alpha is not positive in beta distribution",
433+
BetaError::BetaTooSmall => "beta is not positive in beta distribution",
434+
})
435+
}
436+
}
437+
438+
impl error::Error for BetaError {}
439+
393440
impl<N: Float> Beta<N>
394441
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
395442
{

rand_distr/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
//! - [`UnitDisc`] distribution
6565
6666
pub use rand::distributions::{Distribution, DistIter, Standard,
67-
Alphanumeric, Uniform, OpenClosed01, Open01, Bernoulli, uniform, weighted};
67+
Alphanumeric, Uniform, OpenClosed01, Open01, Bernoulli, BernoulliError,
68+
uniform, weighted};
6869

6970
pub use self::unit_sphere::UnitSphere;
7071
pub use self::unit_ball::UnitBall;

rand_distr/src/normal.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use rand::Rng;
1313
use crate::{ziggurat_tables, Distribution, Open01};
1414
use crate::utils::{ziggurat, Float};
15+
use std::{error, fmt};
1516

1617
/// Samples floating-point numbers according to the normal distribution
1718
/// `N(0, 1)` (a.k.a. a standard normal, or Gaussian). This is equivalent to
@@ -114,6 +115,16 @@ pub enum Error {
114115
StdDevTooSmall,
115116
}
116117

118+
impl fmt::Display for Error {
119+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120+
f.write_str(match self {
121+
Error::StdDevTooSmall => "std_dev < 0 or is NaN in normal distribution",
122+
})
123+
}
124+
}
125+
126+
impl error::Error for Error {}
127+
117128
impl<N: Float> Normal<N>
118129
where StandardNormal: Distribution<N>
119130
{

rand_distr/src/pareto.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use rand::Rng;
1212
use crate::{Distribution, OpenClosed01};
1313
use crate::utils::Float;
14+
use std::{error, fmt};
1415

1516
/// Samples floating-point numbers according to the Pareto distribution
1617
///
@@ -37,6 +38,17 @@ pub enum Error {
3738
ShapeTooSmall,
3839
}
3940

41+
impl fmt::Display for Error {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
f.write_str(match self {
44+
Error::ScaleTooSmall => "scale is not positive in Pareto distribution",
45+
Error::ShapeTooSmall => "shape is not positive in Pareto distribution",
46+
})
47+
}
48+
}
49+
50+
impl error::Error for Error {}
51+
4052
impl<N: Float> Pareto<N>
4153
where OpenClosed01: Distribution<N>
4254
{

rand_distr/src/pert.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use rand::Rng;
1111
use crate::{Distribution, Beta, StandardNormal, Exp1, Open01};
1212
use crate::utils::Float;
13+
use std::{error, fmt};
1314

1415
/// The PERT distribution.
1516
///
@@ -47,6 +48,18 @@ pub enum PertError {
4748
ShapeTooSmall,
4849
}
4950

51+
impl fmt::Display for PertError {
52+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53+
f.write_str(match self {
54+
PertError::RangeTooSmall => "requirement min < max is not met in PERT distribution",
55+
PertError::ModeRange => "mode is outside [min, max] in PERT distribution",
56+
PertError::ShapeTooSmall => "shape < 0 or is NaN in PERT distribution",
57+
})
58+
}
59+
}
60+
61+
impl error::Error for PertError {}
62+
5063
impl<N: Float> Pert<N>
5164
where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distribution<N>
5265
{

rand_distr/src/poisson.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use rand::Rng;
1313
use crate::{Distribution, Cauchy, Standard};
1414
use crate::utils::Float;
15+
use std::{error, fmt};
1516

1617
/// The Poisson distribution `Poisson(lambda)`.
1718
///
@@ -44,6 +45,16 @@ pub enum Error {
4445
ShapeTooSmall,
4546
}
4647

48+
impl fmt::Display for Error {
49+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50+
f.write_str(match self {
51+
Error::ShapeTooSmall => "lambda is not positive in Poisson distribution",
52+
})
53+
}
54+
}
55+
56+
impl error::Error for Error {}
57+
4758
impl<N: Float> Poisson<N>
4859
where Standard: Distribution<N>
4960
{

rand_distr/src/triangular.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use rand::Rng;
1111
use crate::{Distribution, Standard};
1212
use crate::utils::Float;
13+
use std::{error, fmt};
1314

1415
/// The triangular distribution.
1516
///
@@ -46,6 +47,19 @@ pub enum TriangularError {
4647
ModeRange,
4748
}
4849

50+
impl fmt::Display for TriangularError {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
f.write_str(match self {
53+
TriangularError::RangeTooSmall => {
54+
"requirement min <= max is not met in triangular distribution"
55+
}
56+
TriangularError::ModeRange => "mode is outside [min, max] in triangular distribution",
57+
})
58+
}
59+
}
60+
61+
impl error::Error for TriangularError {}
62+
4963
impl<N: Float> Triangular<N>
5064
where Standard: Distribution<N>
5165
{

rand_distr/src/weibull.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use rand::Rng;
1212
use crate::{Distribution, OpenClosed01};
1313
use crate::utils::Float;
14+
use std::{error, fmt};
1415

1516
/// Samples floating-point numbers according to the Weibull distribution
1617
///
@@ -37,6 +38,17 @@ pub enum Error {
3738
ShapeTooSmall,
3839
}
3940

41+
impl fmt::Display for Error {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
f.write_str(match self {
44+
Error::ScaleTooSmall => "scale is not positive in Weibull distribution",
45+
Error::ShapeTooSmall => "shape is not positive in Weibull distribution",
46+
})
47+
}
48+
}
49+
50+
impl error::Error for Error {}
51+
4052
impl<N: Float> Weibull<N>
4153
where OpenClosed01: Distribution<N>
4254
{

0 commit comments

Comments
 (0)