From b9f0c54a2c8dee333d0218e87601c7a8b37f8bff Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 8 Jul 2021 15:00:32 +0100 Subject: [PATCH] rand_distr: derive serde support (easy cases) --- rand_distr/src/binomial.rs | 1 + rand_distr/src/cauchy.rs | 1 + rand_distr/src/dirichlet.rs | 1 + rand_distr/src/exponential.rs | 2 ++ rand_distr/src/gamma.rs | 17 +++++++++++++++++ rand_distr/src/geometric.rs | 4 +++- rand_distr/src/hypergeometric.rs | 2 ++ rand_distr/src/inverse_gaussian.rs | 1 + rand_distr/src/normal.rs | 3 +++ rand_distr/src/normal_inverse_gaussian.rs | 1 + rand_distr/src/pareto.rs | 1 + rand_distr/src/pert.rs | 1 + rand_distr/src/poisson.rs | 1 + rand_distr/src/triangular.rs | 1 + rand_distr/src/unit_ball.rs | 1 + rand_distr/src/unit_circle.rs | 1 + rand_distr/src/unit_disc.rs | 1 + rand_distr/src/unit_sphere.rs | 1 + rand_distr/src/weibull.rs | 1 + 19 files changed, 41 insertions(+), 1 deletion(-) diff --git a/rand_distr/src/binomial.rs b/rand_distr/src/binomial.rs index f4fbbfab..f3c45953 100644 --- a/rand_distr/src/binomial.rs +++ b/rand_distr/src/binomial.rs @@ -29,6 +29,7 @@ use core::cmp::Ordering; /// println!("{} is from a binomial distribution", v); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Binomial { /// Number of trials. n: u64, diff --git a/rand_distr/src/cauchy.rs b/rand_distr/src/cauchy.rs index 66dc09b3..49b121c4 100644 --- a/rand_distr/src/cauchy.rs +++ b/rand_distr/src/cauchy.rs @@ -32,6 +32,7 @@ use core::fmt; /// println!("{} is from a Cauchy(2, 5) distribution", v); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Cauchy where F: Float + FloatConst, Standard: Distribution { diff --git a/rand_distr/src/dirichlet.rs b/rand_distr/src/dirichlet.rs index 6286e935..0ffbc40a 100644 --- a/rand_distr/src/dirichlet.rs +++ b/rand_distr/src/dirichlet.rs @@ -33,6 +33,7 @@ use alloc::{boxed::Box, vec, vec::Vec}; /// ``` #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] #[derive(Clone, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Dirichlet where F: Float, diff --git a/rand_distr/src/exponential.rs b/rand_distr/src/exponential.rs index e389319d..4e33c3ca 100644 --- a/rand_distr/src/exponential.rs +++ b/rand_distr/src/exponential.rs @@ -39,6 +39,7 @@ use core::fmt; /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Exp1; impl Distribution for Exp1 { @@ -91,6 +92,7 @@ impl Distribution for Exp1 { /// println!("{} is from a Exp(2) distribution", v); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Exp where F: Float, Exp1: Distribution { diff --git a/rand_distr/src/gamma.rs b/rand_distr/src/gamma.rs index 45181b92..87faf11c 100644 --- a/rand_distr/src/gamma.rs +++ b/rand_distr/src/gamma.rs @@ -21,6 +21,8 @@ use num_traits::Float; use crate::{Distribution, Exp, Exp1, Open01}; use rand::Rng; use core::fmt; +#[cfg(feature = "serde1")] +use serde::{Serialize, Deserialize}; /// The Gamma distribution `Gamma(shape, scale)` distribution. /// @@ -53,6 +55,7 @@ use core::fmt; /// (September 2000), 363-372. /// DOI:[10.1145/358407.358414](https://doi.acm.org/10.1145/358407.358414) #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Gamma where F: Float, @@ -89,6 +92,7 @@ impl fmt::Display for Error { impl std::error::Error for Error {} #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] enum GammaRepr where F: Float, @@ -116,6 +120,7 @@ where /// See `Gamma` for sampling from a Gamma distribution with general /// shape parameters. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] struct GammaSmallShape where F: Float, @@ -131,6 +136,7 @@ where /// See `Gamma` for sampling from a Gamma distribution with general /// shape parameters. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] struct GammaLargeShape where F: Float, @@ -275,6 +281,7 @@ where /// println!("{} is from a χ²(11) distribution", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct ChiSquared where F: Float, @@ -287,6 +294,7 @@ where /// Error type returned from `ChiSquared::new` and `StudentT::new`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub enum ChiSquaredError { /// `0.5 * k <= 0` or `nan`. DoFTooSmall, @@ -307,6 +315,7 @@ impl fmt::Display for ChiSquaredError { impl std::error::Error for ChiSquaredError {} #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] enum ChiSquaredRepr where F: Float, @@ -377,6 +386,7 @@ where /// println!("{} is from an F(2, 32) distribution", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct FisherF where F: Float, @@ -393,6 +403,7 @@ where /// Error type returned from `FisherF::new`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub enum FisherFError { /// `m <= 0` or `nan`. MTooSmall, @@ -462,6 +473,7 @@ where /// println!("{} is from a t(11) distribution", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct StudentT where F: Float, @@ -511,6 +523,7 @@ where /// Communications of the ACM 21, 317-322. /// https://doi.org/10.1145/359460.359482 #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] enum BetaAlgorithm { BB(BB), BC(BC), @@ -518,6 +531,7 @@ enum BetaAlgorithm { /// Algorithm BB for `min(alpha, beta) > 1`. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] struct BB { alpha: N, beta: N, @@ -526,6 +540,7 @@ struct BB { /// Algorithm BC for `min(alpha, beta) <= 1`. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] struct BC { alpha: N, beta: N, @@ -546,6 +561,7 @@ struct BC { /// println!("{} is from a Beta(2, 5) distribution", v); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Beta where F: Float, @@ -557,6 +573,7 @@ where /// Error type returned from `Beta::new`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub enum BetaError { /// `alpha <= 0` or `nan`. AlphaTooSmall, diff --git a/rand_distr/src/geometric.rs b/rand_distr/src/geometric.rs index 8cb10576..31bf98c8 100644 --- a/rand_distr/src/geometric.rs +++ b/rand_distr/src/geometric.rs @@ -26,6 +26,7 @@ use core::fmt; /// println!("{} is from a Geometric(0.25) distribution", v); /// ``` #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Geometric { p: f64, @@ -151,6 +152,7 @@ impl Distribution for Geometric /// println!("{} is from a Geometric(0.5) distribution", v); /// ``` #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct StandardGeometric; impl Distribution for StandardGeometric { @@ -231,4 +233,4 @@ mod test { results.iter().map(|x| (x - mean) * (x - mean)).sum::() / results.len() as f64; assert!((variance - expected_variance).abs() < expected_variance / 10.0); } -} \ No newline at end of file +} diff --git a/rand_distr/src/hypergeometric.rs b/rand_distr/src/hypergeometric.rs index 507d7bb0..801dff4e 100644 --- a/rand_distr/src/hypergeometric.rs +++ b/rand_distr/src/hypergeometric.rs @@ -6,6 +6,7 @@ use rand::distributions::uniform::Uniform; use core::fmt; #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] enum SamplingMethod { InverseTransform{ initial_p: f64, initial_x: i64 }, RejectionAcceptance{ @@ -43,6 +44,7 @@ enum SamplingMethod { /// println!("{} is from a hypergeometric distribution", v); /// ``` #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Hypergeometric { n1: u64, n2: u64, diff --git a/rand_distr/src/inverse_gaussian.rs b/rand_distr/src/inverse_gaussian.rs index becb02b6..58986a76 100644 --- a/rand_distr/src/inverse_gaussian.rs +++ b/rand_distr/src/inverse_gaussian.rs @@ -27,6 +27,7 @@ impl std::error::Error for Error {} /// The [inverse Gaussian distribution](https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution) #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct InverseGaussian where F: Float, diff --git a/rand_distr/src/normal.rs b/rand_distr/src/normal.rs index c3e99010..7078a894 100644 --- a/rand_distr/src/normal.rs +++ b/rand_distr/src/normal.rs @@ -37,6 +37,7 @@ use core::fmt; /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct StandardNormal; impl Distribution for StandardNormal { @@ -112,6 +113,7 @@ impl Distribution for StandardNormal { /// /// [`StandardNormal`]: crate::StandardNormal #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Normal where F: Float, StandardNormal: Distribution { @@ -226,6 +228,7 @@ where F: Float, StandardNormal: Distribution /// println!("{} is from an ln N(2, 9) distribution", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct LogNormal where F: Float, StandardNormal: Distribution { diff --git a/rand_distr/src/normal_inverse_gaussian.rs b/rand_distr/src/normal_inverse_gaussian.rs index d8e44587..c4d693d0 100644 --- a/rand_distr/src/normal_inverse_gaussian.rs +++ b/rand_distr/src/normal_inverse_gaussian.rs @@ -27,6 +27,7 @@ impl std::error::Error for Error {} /// The [normal-inverse Gaussian distribution](https://en.wikipedia.org/wiki/Normal-inverse_Gaussian_distribution) #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct NormalInverseGaussian where F: Float, diff --git a/rand_distr/src/pareto.rs b/rand_distr/src/pareto.rs index 53e2987f..cd61894c 100644 --- a/rand_distr/src/pareto.rs +++ b/rand_distr/src/pareto.rs @@ -24,6 +24,7 @@ use core::fmt; /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Pareto where F: Float, OpenClosed01: Distribution { diff --git a/rand_distr/src/pert.rs b/rand_distr/src/pert.rs index e53ea0b8..4ead1fb8 100644 --- a/rand_distr/src/pert.rs +++ b/rand_distr/src/pert.rs @@ -31,6 +31,7 @@ use core::fmt; /// /// [`Triangular`]: crate::Triangular #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Pert where F: Float, diff --git a/rand_distr/src/poisson.rs b/rand_distr/src/poisson.rs index dc21ef3e..abc00289 100644 --- a/rand_distr/src/poisson.rs +++ b/rand_distr/src/poisson.rs @@ -29,6 +29,7 @@ use core::fmt; /// println!("{} is from a Poisson(2) distribution", v); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Poisson where F: Float + FloatConst, Standard: Distribution { diff --git a/rand_distr/src/triangular.rs b/rand_distr/src/triangular.rs index 97693b32..ba6d3644 100644 --- a/rand_distr/src/triangular.rs +++ b/rand_distr/src/triangular.rs @@ -32,6 +32,7 @@ use core::fmt; /// /// [`Pert`]: crate::Pert #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Triangular where F: Float, Standard: Distribution { diff --git a/rand_distr/src/unit_ball.rs b/rand_distr/src/unit_ball.rs index e5585a1e..8a4b4fbf 100644 --- a/rand_distr/src/unit_ball.rs +++ b/rand_distr/src/unit_ball.rs @@ -25,6 +25,7 @@ use rand::Rng; /// println!("{:?} is from the unit ball.", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct UnitBall; impl Distribution<[F; 3]> for UnitBall { diff --git a/rand_distr/src/unit_circle.rs b/rand_distr/src/unit_circle.rs index 29e5c9a5..24a06f3f 100644 --- a/rand_distr/src/unit_circle.rs +++ b/rand_distr/src/unit_circle.rs @@ -29,6 +29,7 @@ use rand::Rng; /// NBS Appl. Math. Ser., No. 12. Washington, DC: U.S. Government Printing /// Office, pp. 36-38. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct UnitCircle; impl Distribution<[F; 2]> for UnitCircle { diff --git a/rand_distr/src/unit_disc.rs b/rand_distr/src/unit_disc.rs index ced548b4..937c1d01 100644 --- a/rand_distr/src/unit_disc.rs +++ b/rand_distr/src/unit_disc.rs @@ -24,6 +24,7 @@ use rand::Rng; /// println!("{:?} is from the unit Disc.", v) /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct UnitDisc; impl Distribution<[F; 2]> for UnitDisc { diff --git a/rand_distr/src/unit_sphere.rs b/rand_distr/src/unit_sphere.rs index b167a5d5..2b299239 100644 --- a/rand_distr/src/unit_sphere.rs +++ b/rand_distr/src/unit_sphere.rs @@ -28,6 +28,7 @@ use rand::Rng; /// Sphere.*](https://doi.org/10.1214/aoms/1177692644) /// Ann. Math. Statist. 43, no. 2, 645--646. #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct UnitSphere; impl Distribution<[F; 3]> for UnitSphere { diff --git a/rand_distr/src/weibull.rs b/rand_distr/src/weibull.rs index aa9bdc44..b390ad3f 100644 --- a/rand_distr/src/weibull.rs +++ b/rand_distr/src/weibull.rs @@ -24,6 +24,7 @@ use core::fmt; /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Weibull where F: Float, OpenClosed01: Distribution {