Skip to content

Commit 52407e7

Browse files
authored
Merge pull request #1141 from dhardy/work
Add serde1 feature for rand_distr
2 parents 8a07c93 + af751df commit 52407e7

25 files changed

+68
-13
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
cargo test --target ${{ matrix.target }} --manifest-path rand_core/Cargo.toml --no-default-features --features=alloc,getrandom
9393
- name: Test rand_distr
9494
run: |
95-
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml
95+
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --features=serde1
9696
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --no-default-features
9797
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --no-default-features --features=std,std_math
9898
- name: Test rand_pcg
@@ -134,7 +134,7 @@ jobs:
134134
cross test --no-fail-fast --target ${{ matrix.target }} --features=serde1,log,small_rng
135135
cross test --no-fail-fast --target ${{ matrix.target }} --examples
136136
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_core/Cargo.toml
137-
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml
137+
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --features=serde1
138138
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_pcg/Cargo.toml --features=serde1
139139
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_chacha/Cargo.toml
140140
cross test --no-fail-fast --target ${{ matrix.target }} --manifest-path rand_hc/Cargo.toml

rand_distr/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Make sure all distributions and their error types implement `Error`, `Display`, `Clone`,
1313
`Copy`, `PartialEq` and `Eq` as appropriate (#1126)
1414
- Port benchmarks to use Criterion crate (#1116)
15+
- Support serde for distributions (#1141)
1516

1617
## [0.4.0] - 2020-12-18
1718
- Bump `rand` to v0.8.0

rand_distr/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ categories = ["algorithms", "no-std"]
1515
edition = "2018"
1616
include = ["src/", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"]
1717

18-
[dependencies]
19-
rand = { path = "..", version = "0.8.0", default-features = false }
20-
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
21-
2218
[features]
2319
default = ["std"]
2420
std = ["alloc", "rand/std"]
2521
alloc = ["rand/alloc"]
2622
std_math = ["num-traits/std"]
23+
serde1 = ["serde", "rand/serde1"]
24+
25+
[dependencies]
26+
rand = { path = "..", version = "0.8.0", default-features = false }
27+
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
28+
serde = { version = "1.0.103", features = ["derive"], optional = true }
2729

2830
[dev-dependencies]
2931
rand_pcg = { version = "0.3.0", path = "../rand_pcg" }

rand_distr/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ It is worth mentioning the [statrs] crate which provides similar functionality
2020
along with various support functions, including PDF and CDF computation. In
2121
contrast, this `rand_distr` crate focuses on sampling from distributions.
2222

23-
If the `std` default feature is enabled, `rand_distr` implements the `Error`
24-
trait for its error types.
25-
26-
The default `alloc` feature (which is implied by the `std` feature) is required
27-
for some distributions (in particular, `Dirichlet` and `WeightedAliasIndex`).
23+
## Portability and libm
2824

2925
The floating point functions from `num_traits` and `libm` are used to support
3026
`no_std` environments and ensure reproducibility. If the floating point
3127
functions from `std` are preferred, which may provide better accuracy and
3228
performance but may produce different random values, the `std_math` feature
3329
can be enabled.
3430

35-
Links:
31+
## Crate features
32+
33+
- `std` (enabled by default): `rand_distr` implements the `Error` trait for
34+
its error types. Implies `alloc` and `rand/std`.
35+
- `alloc` (enabled by default): required for some distributions when not using
36+
`std` (in particular, `Dirichlet` and `WeightedAliasIndex`).
37+
- `std_math`: see above on portability and libm
38+
- `serde1`: implement (de)seriaialization using `serde`
39+
40+
## Links
3641

3742
- [API documentation (master)](https://rust-random.github.io/rand/rand_distr)
3843
- [API documentation (docs.rs)](https://docs.rs/rand_distr)

rand_distr/src/binomial.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use core::cmp::Ordering;
2929
/// println!("{} is from a binomial distribution", v);
3030
/// ```
3131
#[derive(Clone, Copy, Debug)]
32+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3233
pub struct Binomial {
3334
/// Number of trials.
3435
n: u64,

rand_distr/src/cauchy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use core::fmt;
3232
/// println!("{} is from a Cauchy(2, 5) distribution", v);
3333
/// ```
3434
#[derive(Clone, Copy, Debug)]
35+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3536
pub struct Cauchy<F>
3637
where F: Float + FloatConst, Standard: Distribution<F>
3738
{

rand_distr/src/dirichlet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use alloc::{boxed::Box, vec, vec::Vec};
3333
/// ```
3434
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
3535
#[derive(Clone, Debug)]
36+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3637
pub struct Dirichlet<F>
3738
where
3839
F: Float,

rand_distr/src/exponential.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use core::fmt;
3939
/// println!("{}", val);
4040
/// ```
4141
#[derive(Clone, Copy, Debug)]
42+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4243
pub struct Exp1;
4344

4445
impl Distribution<f32> for Exp1 {
@@ -91,6 +92,7 @@ impl Distribution<f64> for Exp1 {
9192
/// println!("{} is from a Exp(2) distribution", v);
9293
/// ```
9394
#[derive(Clone, Copy, Debug)]
95+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
9496
pub struct Exp<F>
9597
where F: Float, Exp1: Distribution<F>
9698
{

rand_distr/src/gamma.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use num_traits::Float;
2121
use crate::{Distribution, Exp, Exp1, Open01};
2222
use rand::Rng;
2323
use core::fmt;
24+
#[cfg(feature = "serde1")]
25+
use serde::{Serialize, Deserialize};
2426

2527
/// The Gamma distribution `Gamma(shape, scale)` distribution.
2628
///
@@ -53,6 +55,7 @@ use core::fmt;
5355
/// (September 2000), 363-372.
5456
/// DOI:[10.1145/358407.358414](https://doi.acm.org/10.1145/358407.358414)
5557
#[derive(Clone, Copy, Debug)]
58+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
5659
pub struct Gamma<F>
5760
where
5861
F: Float,
@@ -89,6 +92,7 @@ impl fmt::Display for Error {
8992
impl std::error::Error for Error {}
9093

9194
#[derive(Clone, Copy, Debug)]
95+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
9296
enum GammaRepr<F>
9397
where
9498
F: Float,
@@ -116,6 +120,7 @@ where
116120
/// See `Gamma` for sampling from a Gamma distribution with general
117121
/// shape parameters.
118122
#[derive(Clone, Copy, Debug)]
123+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
119124
struct GammaSmallShape<F>
120125
where
121126
F: Float,
@@ -131,6 +136,7 @@ where
131136
/// See `Gamma` for sampling from a Gamma distribution with general
132137
/// shape parameters.
133138
#[derive(Clone, Copy, Debug)]
139+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
134140
struct GammaLargeShape<F>
135141
where
136142
F: Float,
@@ -275,6 +281,7 @@ where
275281
/// println!("{} is from a χ²(11) distribution", v)
276282
/// ```
277283
#[derive(Clone, Copy, Debug)]
284+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
278285
pub struct ChiSquared<F>
279286
where
280287
F: Float,
@@ -287,6 +294,7 @@ where
287294

288295
/// Error type returned from `ChiSquared::new` and `StudentT::new`.
289296
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
297+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
290298
pub enum ChiSquaredError {
291299
/// `0.5 * k <= 0` or `nan`.
292300
DoFTooSmall,
@@ -307,6 +315,7 @@ impl fmt::Display for ChiSquaredError {
307315
impl std::error::Error for ChiSquaredError {}
308316

309317
#[derive(Clone, Copy, Debug)]
318+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
310319
enum ChiSquaredRepr<F>
311320
where
312321
F: Float,
@@ -377,6 +386,7 @@ where
377386
/// println!("{} is from an F(2, 32) distribution", v)
378387
/// ```
379388
#[derive(Clone, Copy, Debug)]
389+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
380390
pub struct FisherF<F>
381391
where
382392
F: Float,
@@ -393,6 +403,7 @@ where
393403

394404
/// Error type returned from `FisherF::new`.
395405
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
406+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
396407
pub enum FisherFError {
397408
/// `m <= 0` or `nan`.
398409
MTooSmall,
@@ -462,6 +473,7 @@ where
462473
/// println!("{} is from a t(11) distribution", v)
463474
/// ```
464475
#[derive(Clone, Copy, Debug)]
476+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
465477
pub struct StudentT<F>
466478
where
467479
F: Float,
@@ -511,13 +523,15 @@ where
511523
/// Communications of the ACM 21, 317-322.
512524
/// https://doi.org/10.1145/359460.359482
513525
#[derive(Clone, Copy, Debug)]
526+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
514527
enum BetaAlgorithm<N> {
515528
BB(BB<N>),
516529
BC(BC<N>),
517530
}
518531

519532
/// Algorithm BB for `min(alpha, beta) > 1`.
520533
#[derive(Clone, Copy, Debug)]
534+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
521535
struct BB<N> {
522536
alpha: N,
523537
beta: N,
@@ -526,6 +540,7 @@ struct BB<N> {
526540

527541
/// Algorithm BC for `min(alpha, beta) <= 1`.
528542
#[derive(Clone, Copy, Debug)]
543+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
529544
struct BC<N> {
530545
alpha: N,
531546
beta: N,
@@ -546,6 +561,7 @@ struct BC<N> {
546561
/// println!("{} is from a Beta(2, 5) distribution", v);
547562
/// ```
548563
#[derive(Clone, Copy, Debug)]
564+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
549565
pub struct Beta<F>
550566
where
551567
F: Float,
@@ -557,6 +573,7 @@ where
557573

558574
/// Error type returned from `Beta::new`.
559575
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
576+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
560577
pub enum BetaError {
561578
/// `alpha <= 0` or `nan`.
562579
AlphaTooSmall,

rand_distr/src/geometric.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use core::fmt;
2626
/// println!("{} is from a Geometric(0.25) distribution", v);
2727
/// ```
2828
#[derive(Copy, Clone, Debug)]
29+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2930
pub struct Geometric
3031
{
3132
p: f64,
@@ -151,6 +152,7 @@ impl Distribution<u64> for Geometric
151152
/// println!("{} is from a Geometric(0.5) distribution", v);
152153
/// ```
153154
#[derive(Copy, Clone, Debug)]
155+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
154156
pub struct StandardGeometric;
155157

156158
impl Distribution<u64> for StandardGeometric {
@@ -231,4 +233,4 @@ mod test {
231233
results.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / results.len() as f64;
232234
assert!((variance - expected_variance).abs() < expected_variance / 10.0);
233235
}
234-
}
236+
}

rand_distr/src/hypergeometric.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rand::distributions::uniform::Uniform;
66
use core::fmt;
77

88
#[derive(Clone, Copy, Debug)]
9+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
910
enum SamplingMethod {
1011
InverseTransform{ initial_p: f64, initial_x: i64 },
1112
RejectionAcceptance{
@@ -43,6 +44,7 @@ enum SamplingMethod {
4344
/// println!("{} is from a hypergeometric distribution", v);
4445
/// ```
4546
#[derive(Copy, Clone, Debug)]
47+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4648
pub struct Hypergeometric {
4749
n1: u64,
4850
n2: u64,

rand_distr/src/inverse_gaussian.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl std::error::Error for Error {}
2727

2828
/// The [inverse Gaussian distribution](https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution)
2929
#[derive(Debug, Clone, Copy)]
30+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3031
pub struct InverseGaussian<F>
3132
where
3233
F: Float,

rand_distr/src/normal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use core::fmt;
3737
/// println!("{}", val);
3838
/// ```
3939
#[derive(Clone, Copy, Debug)]
40+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4041
pub struct StandardNormal;
4142

4243
impl Distribution<f32> for StandardNormal {
@@ -112,6 +113,7 @@ impl Distribution<f64> for StandardNormal {
112113
///
113114
/// [`StandardNormal`]: crate::StandardNormal
114115
#[derive(Clone, Copy, Debug)]
116+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
115117
pub struct Normal<F>
116118
where F: Float, StandardNormal: Distribution<F>
117119
{
@@ -226,6 +228,7 @@ where F: Float, StandardNormal: Distribution<F>
226228
/// println!("{} is from an ln N(2, 9) distribution", v)
227229
/// ```
228230
#[derive(Clone, Copy, Debug)]
231+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
229232
pub struct LogNormal<F>
230233
where F: Float, StandardNormal: Distribution<F>
231234
{

rand_distr/src/normal_inverse_gaussian.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl std::error::Error for Error {}
2727

2828
/// The [normal-inverse Gaussian distribution](https://en.wikipedia.org/wiki/Normal-inverse_Gaussian_distribution)
2929
#[derive(Debug, Clone, Copy)]
30+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3031
pub struct NormalInverseGaussian<F>
3132
where
3233
F: Float,

rand_distr/src/pareto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use core::fmt;
2424
/// println!("{}", val);
2525
/// ```
2626
#[derive(Clone, Copy, Debug)]
27+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2728
pub struct Pareto<F>
2829
where F: Float, OpenClosed01: Distribution<F>
2930
{

rand_distr/src/pert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use core::fmt;
3131
///
3232
/// [`Triangular`]: crate::Triangular
3333
#[derive(Clone, Copy, Debug)]
34+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3435
pub struct Pert<F>
3536
where
3637
F: Float,

rand_distr/src/poisson.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use core::fmt;
2929
/// println!("{} is from a Poisson(2) distribution", v);
3030
/// ```
3131
#[derive(Clone, Copy, Debug)]
32+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3233
pub struct Poisson<F>
3334
where F: Float + FloatConst, Standard: Distribution<F>
3435
{

rand_distr/src/triangular.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use core::fmt;
3232
///
3333
/// [`Pert`]: crate::Pert
3434
#[derive(Clone, Copy, Debug)]
35+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3536
pub struct Triangular<F>
3637
where F: Float, Standard: Distribution<F>
3738
{

rand_distr/src/unit_ball.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rand::Rng;
2525
/// println!("{:?} is from the unit ball.", v)
2626
/// ```
2727
#[derive(Clone, Copy, Debug)]
28+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2829
pub struct UnitBall;
2930

3031
impl<F: Float + SampleUniform> Distribution<[F; 3]> for UnitBall {

rand_distr/src/unit_circle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rand::Rng;
2929
/// NBS Appl. Math. Ser., No. 12. Washington, DC: U.S. Government Printing
3030
/// Office, pp. 36-38.
3131
#[derive(Clone, Copy, Debug)]
32+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3233
pub struct UnitCircle;
3334

3435
impl<F: Float + SampleUniform> Distribution<[F; 2]> for UnitCircle {

rand_distr/src/unit_disc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rand::Rng;
2424
/// println!("{:?} is from the unit Disc.", v)
2525
/// ```
2626
#[derive(Clone, Copy, Debug)]
27+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2728
pub struct UnitDisc;
2829

2930
impl<F: Float + SampleUniform> Distribution<[F; 2]> for UnitDisc {

rand_distr/src/unit_sphere.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rand::Rng;
2828
/// Sphere.*](https://doi.org/10.1214/aoms/1177692644)
2929
/// Ann. Math. Statist. 43, no. 2, 645--646.
3030
#[derive(Clone, Copy, Debug)]
31+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
3132
pub struct UnitSphere;
3233

3334
impl<F: Float + SampleUniform> Distribution<[F; 3]> for UnitSphere {

rand_distr/src/weibull.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use core::fmt;
2424
/// println!("{}", val);
2525
/// ```
2626
#[derive(Clone, Copy, Debug)]
27+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2728
pub struct Weibull<F>
2829
where F: Float, OpenClosed01: Distribution<F>
2930
{

0 commit comments

Comments
 (0)