Skip to content

Commit

Permalink
Add optional support for Serde
Browse files Browse the repository at this point in the history
  • Loading branch information
Kromey committed May 7, 2021
1 parent 122c62e commit d924d47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ exclude = ["/.github/*"]
rand = "0.8.3"
rand_xoshiro = "0.6.0"
rand_distr = "0.4.0"
serde = { version = "1.0", package = "serde", features = ["derive"], optional = true }
serde_arrays = { version = "0.1.0", optional = true }

[features]
default = [] # Provide an "empty" default feature for CI
single_precision = []
small_rng = []
derive_serde = ["serde", "serde_arrays"]

[dev-dependencies]
serde_json = "1.0"
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
//! [const generics]: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#const-generics-mvp
//! [small_rng]: https://docs.rs/rand/0.8.3/rand/rngs/struct.SmallRng.html
#[cfg(feature = "derive_serde")]
use serde::{Deserialize, Serialize};
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -181,8 +183,10 @@ type Float = f32;
/// whether or not they were built with the same parameters, but rather on whether or not they will
/// produce the same results once the distribution is generated.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
pub struct Poisson<const N: usize> {
/// Dimensions of the box
#[cfg_attr(feature = "derive_serde", serde(with = "serde_arrays"))]
dimensions: [Float; N],
/// Radius around each point that must remain empty
radius: Float,
Expand Down
16 changes: 16 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![cfg(feature = "derive_serde")]

use fast_poisson::Poisson2D;
use serde_json;

#[test]
fn serialize_and_deserialize() {
// Be sure to set a seed so we can assert equality
let mut poisson = Poisson2D::new();
poisson.with_seed(1337);

let json = serde_json::to_string(&poisson).unwrap();
let decoded = serde_json::from_str(&json).unwrap();

assert_eq!(poisson, decoded);
}

0 comments on commit d924d47

Please sign in to comment.