More idiomatic serde support

* Follow the suggestions from the Serde docs.
* Rename the feature from `serde` to `serde1`.
* Fix a doctest.
* Mention the feature in the README.
This commit is contained in:
Vinzent Steinberg 2018-07-11 12:54:37 +02:00
parent 34d33ef21a
commit a7dde93df8
12 changed files with 32 additions and 19 deletions

View File

@ -10,6 +10,9 @@ readme = "README.md"
repository = "https://github.com/vks/average"
version = "0.8.0"
[features]
serde1 = ["serde", "serde_derive"]
[[bench]]
harness = false
name = "mean"
@ -26,16 +29,13 @@ name = "kurtosis"
num-traits = "0.2"
num-integer = "0.1"
float-ord = "0.2"
serde = { version = "1", optional = true }
serde_derive = { version = "1", optional = true }
[dependencies.conv]
default-features = false
version = "0.3"
[dependencies.serde]
features = ["derive"]
optional = true
version = "1"
[dev-dependencies]
bencher = "0.1"
rand = "0.5"

View File

@ -15,6 +15,7 @@ easily parallelized by using `merge`.
[Latest Version]: https://img.shields.io/crates/v/average.svg
[crates.io]: https://crates.io/crates/average
## Implemented statistics
* Mean and its error.
@ -24,6 +25,14 @@ easily parallelized by using `merge`.
* Quantile.
* Histogram.
## Crate features
The following optional feature is available:
* `serde1` enables serialization, via Serde version 1.
## Related Projects
* [`quantiles`](https://crates.io/crates/quantiles):

View File

@ -89,9 +89,10 @@
extern crate conv;
extern crate float_ord;
#[cfg(feature = "serde")]
#[macro_use]
#[cfg(feature = "serde1")]
extern crate serde;
#[cfg(feature = "serde1")]
#[macro_use] extern crate serde_derive;
extern crate num_traits;
extern crate num_integer;

View File

@ -24,7 +24,7 @@ fn max(a: f64, b: f64) -> f64 {
/// println!("The minimum is {}.", a.min());
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Min {
x: f64,
}
@ -104,7 +104,7 @@ impl Merge for Min {
/// assert_eq!(a.max(), 5.);
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Max {
x: f64,
}

View File

@ -3,7 +3,7 @@
///
/// This can be used to estimate the standard error of the mean.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Kurtosis {
/// Estimator of mean, variance and skewness.
avg: Skewness,

View File

@ -10,7 +10,7 @@
/// println!("The mean is {}.", a.mean());
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Mean {
/// Mean value.
avg: f64,

View File

@ -32,6 +32,10 @@ pub type MeanWithError = Variance;
/// # extern crate conv;
/// # extern crate num_integer;
/// # extern crate num_traits;
/// #[cfg(feature = "serde1")]
/// extern crate serde;
/// #[cfg(feature = "serde1")]
/// #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate average;
/// # fn main() {
/// define_moments!(Moments4, 4);
@ -65,7 +69,7 @@ macro_rules! define_moments {
/// Estimate the first N moments of a sequence of numbers a sequence of numbers
/// ("population").
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct $name {
/// Number of samples.
///
@ -126,13 +130,12 @@ macro_rules! define_moments {
#[inline]
pub fn standardized_moment(&self, p: usize) -> f64 {
match p {
0 => n,
0 => f64::approx_from(self.n).unwrap(),
1 => 0.,
2 => 1.,
_ => {
let variance = self.central_moment(2);
assert_ne!(variance, 0.);
let n = f64::approx_from(self.n).unwrap();
self.central_moment(p) / pow(variance.sqrt(), p)
},
}

View File

@ -3,7 +3,7 @@
///
/// This can be used to estimate the standard error of the mean.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Skewness {
/// Estimator of mean and variance.
avg: MeanWithError,

View File

@ -13,7 +13,7 @@
/// println!("The mean is {} ± {}.", a.mean(), a.error());
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Variance {
/// Estimator of average.
avg: Mean,

View File

@ -15,7 +15,7 @@ use super::Estimate;
// This uses the P² algorithm introduced here:
// http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Quantile {
/// Marker heights.
q: [f64; 5],

View File

@ -17,7 +17,7 @@ use super::{MeanWithError, Estimate, Merge};
/// println!("The weighted mean is {}.", a.mean());
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct WeightedMean {
/// Sum of the weights.
weight_sum: f64,

View File

@ -59,7 +59,7 @@ fn simple_serde() {
let a: Moments4 = (1..6).map(f64::from).collect();
let b = serde_json::to_string(&a).unwrap();
assert_eq!(&b, "{\"n\":5,\"avg\":3.0,\"m\":[10.0,1.7763568394002506e-15,34.00000000000001]}");
let mut c: Moments = serde_json::from_str(&b).unwrap();
let mut c: Moments4 = serde_json::from_str(&b).unwrap();
assert_eq!(c.len(), 5);
assert_eq!(c.mean(), 3.0);
assert_eq!(c.central_moment(0), 1.0);