Skip to content

Commit

Permalink
Serialize const arrays with serde_with
Browse files Browse the repository at this point in the history
  • Loading branch information
Armavica committed Mar 16, 2023
1 parent a16b932 commit e8bd2e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions rand_distr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 4 additions & 1 deletion rand_distr/src/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
///
Expand All @@ -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<F, const N: usize>
where
F: Float,
Expand All @@ -41,6 +43,7 @@ where
Open01: Distribution<F>,
{
/// Concentration parameters (alpha)
#[cfg_attr(feature = "serde_with", serde_as(as = "[_; N]"))]
alpha: [F; N],
}

Expand Down

0 comments on commit e8bd2e0

Please sign in to comment.