Skip to content

Commit

Permalink
Serialize const arrays with serde_arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Armavica committed Mar 2, 2023
1 parent a16b932 commit a215a76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rand_distr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ default = ["std"]
std = ["alloc", "rand/std"]
alloc = ["rand/alloc"]
std_math = ["num-traits/std"]
serde1 = ["serde", "rand/serde1"]
serde1 = ["serde", "serde_arrays", "rand/serde1"]

[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_arrays = { version = "0.1.0", optional = true }

[dev-dependencies]
rand_pcg = { version = "0.4.0", path = "../rand_pcg" }
Expand Down
7 changes: 6 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 = "serde1")]
use serde::{Serialize, Deserialize};

/// The Dirichlet distribution `Dirichlet(alpha)`.
///
Expand All @@ -32,7 +34,9 @@ use core::fmt;
/// ```
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", serde(bound(serialize = "F: Serialize")))]
#[cfg_attr(feature = "serde1", serde(bound(deserialize = "F: Deserialize<'de>")))]
pub struct Dirichlet<F, const N: usize>
where
F: Float,
Expand All @@ -41,6 +45,7 @@ where
Open01: Distribution<F>,
{
/// Concentration parameters (alpha)
#[cfg_attr(feature = "serde1", serde(with = "serde_arrays"))]
alpha: [F; N],
}

Expand Down

0 comments on commit a215a76

Please sign in to comment.