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 15, 2023
1 parent a16b932 commit 238d580
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions rand_distr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ std = ["alloc", "rand/std"]
alloc = ["rand/alloc"]
std_math = ["num-traits/std"]
serde1 = ["serde", "rand/serde1"]
serdewith = ["serde", "serde_with", "rand/serde"]

[dependencies]
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 = "serdewith")]
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 = "serdewith", 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 = "serdewith", serde_as(as = "[_; N]"))]
alpha: [F; N],
}

Expand Down

0 comments on commit 238d580

Please sign in to comment.