From e8bd2e02797da43b91bb62c034f7bee6ff2def7f Mon Sep 17 00:00:00 2001 From: Virgile Andreani Date: Wed, 1 Mar 2023 23:04:48 -0500 Subject: [PATCH] Serialize const arrays with serde_with --- rand_distr/Cargo.toml | 1 + rand_distr/src/dirichlet.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rand_distr/Cargo.toml b/rand_distr/Cargo.toml index ff1400d6e6..49fd2e6f8a 100644 --- a/rand_distr/Cargo.toml +++ b/rand_distr/Cargo.toml @@ -27,6 +27,7 @@ serde1 = ["serde", "rand/serde1"] rand = { path = "..", version = "0.9.0", default-features = false } num-traits = { version = "0.2", default-features = false, features = ["libm"] } serde = { version = "1.0.103", features = ["derive"], optional = true } +serde_with = { version = "1.14.0", optional = true } [dev-dependencies] rand_pcg = { version = "0.4.0", path = "../rand_pcg" } diff --git a/rand_distr/src/dirichlet.rs b/rand_distr/src/dirichlet.rs index 462e9f901f..526480411d 100644 --- a/rand_distr/src/dirichlet.rs +++ b/rand_distr/src/dirichlet.rs @@ -13,6 +13,8 @@ use num_traits::Float; use crate::{Distribution, Exp1, Gamma, Open01, StandardNormal}; use rand::Rng; use core::fmt; +#[cfg(feature = "serde_with")] +use serde_with::serde_as; /// The Dirichlet distribution `Dirichlet(alpha)`. /// @@ -31,8 +33,8 @@ use core::fmt; /// println!("{:?} is from a Dirichlet([1.0, 2.0, 3.0]) distribution", samples); /// ``` #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] +#[cfg_attr(feature = "serde_with", serde_as)] #[derive(Clone, Debug, PartialEq)] -#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] pub struct Dirichlet where F: Float, @@ -41,6 +43,7 @@ where Open01: Distribution, { /// Concentration parameters (alpha) + #[cfg_attr(feature = "serde_with", serde_as(as = "[_; N]"))] alpha: [F; N], }