From 3c3cb4df58a380c80053a2b020f246b9e3c89999 Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Mon, 16 Sep 2024 11:29:17 +0200 Subject: [PATCH] doc: add doc(cfg(feature = "...')) This will add a note "Available on crate feature <...> only." to feature-gated structs, impls etc. --- Cargo.toml | 4 ++++ src/distribution/bernoulli.rs | 1 + src/distribution/beta.rs | 1 + src/distribution/binomial.rs | 1 + src/distribution/categorical.rs | 2 ++ src/distribution/cauchy.rs | 1 + src/distribution/chi.rs | 1 + src/distribution/chi_squared.rs | 1 + src/distribution/dirac.rs | 1 + src/distribution/dirichlet.rs | 1 + src/distribution/discrete_uniform.rs | 1 + src/distribution/empirical.rs | 1 + src/distribution/erlang.rs | 1 + src/distribution/exponential.rs | 1 + src/distribution/fisher_snedecor.rs | 1 + src/distribution/gamma.rs | 2 ++ src/distribution/geometric.rs | 1 + src/distribution/hypergeometric.rs | 1 + src/distribution/inverse_gamma.rs | 1 + src/distribution/laplace.rs | 1 + src/distribution/log_normal.rs | 1 + src/distribution/mod.rs | 6 ++++++ src/distribution/multinomial.rs | 1 + src/distribution/multivariate_normal.rs | 1 + src/distribution/multivariate_students_t.rs | 1 + src/distribution/negative_binomial.rs | 1 + src/distribution/normal.rs | 2 ++ src/distribution/pareto.rs | 1 + src/distribution/poisson.rs | 2 ++ src/distribution/students_t.rs | 1 + src/distribution/triangular.rs | 2 ++ src/distribution/uniform.rs | 2 ++ src/distribution/weibull.rs | 1 + src/lib.rs | 1 + 34 files changed, 48 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 8625bd99..7c27e034 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,3 +56,7 @@ features = ["macros"] level = "warn" # Set by cargo-llvm-cov when running on nightly check-cfg = ['cfg(coverage_nightly)'] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/src/distribution/bernoulli.rs b/src/distribution/bernoulli.rs index 28f9d104..521fa7ba 100644 --- a/src/distribution/bernoulli.rs +++ b/src/distribution/bernoulli.rs @@ -85,6 +85,7 @@ impl std::fmt::Display for Bernoulli { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Bernoulli { fn sample(&self, rng: &mut R) -> f64 { rng.gen_bool(self.p()) as u8 as f64 diff --git a/src/distribution/beta.rs b/src/distribution/beta.rs index 2741889e..5ea768de 100644 --- a/src/distribution/beta.rs +++ b/src/distribution/beta.rs @@ -121,6 +121,7 @@ impl std::fmt::Display for Beta { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Beta { fn sample(&self, rng: &mut R) -> f64 { // Generated by sampling two gamma distributions and normalizing. diff --git a/src/distribution/binomial.rs b/src/distribution/binomial.rs index c24bf7b5..dca3754c 100644 --- a/src/distribution/binomial.rs +++ b/src/distribution/binomial.rs @@ -110,6 +110,7 @@ impl std::fmt::Display for Binomial { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Binomial { fn sample(&self, rng: &mut R) -> f64 { (0..self.n).fold(0.0, |acc, _| { diff --git a/src/distribution/categorical.rs b/src/distribution/categorical.rs index 7d3a7c1c..b63b1312 100644 --- a/src/distribution/categorical.rs +++ b/src/distribution/categorical.rs @@ -124,6 +124,7 @@ impl std::fmt::Display for Categorical { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Categorical { fn sample(&self, rng: &mut R) -> f64 { sample_unchecked(rng, &self.cdf) @@ -323,6 +324,7 @@ impl Discrete for Categorical { /// Draws a sample from the categorical distribution described by `cdf` /// without doing any bounds checking #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] pub fn sample_unchecked(rng: &mut R, cdf: &[f64]) -> f64 { let draw = rng.gen::() * cdf.last().unwrap(); cdf.iter() diff --git a/src/distribution/cauchy.rs b/src/distribution/cauchy.rs index c9fc4ae2..7353f02d 100644 --- a/src/distribution/cauchy.rs +++ b/src/distribution/cauchy.rs @@ -111,6 +111,7 @@ impl std::fmt::Display for Cauchy { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Cauchy { fn sample(&self, r: &mut R) -> f64 { self.location + self.scale * (f64::consts::PI * (r.gen::() - 0.5)).tan() diff --git a/src/distribution/chi.rs b/src/distribution/chi.rs index 079aca91..8eb50603 100644 --- a/src/distribution/chi.rs +++ b/src/distribution/chi.rs @@ -94,6 +94,7 @@ impl std::fmt::Display for Chi { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Chi { fn sample(&self, rng: &mut R) -> f64 { (0..self.freedom as i64) diff --git a/src/distribution/chi_squared.rs b/src/distribution/chi_squared.rs index f61d6e19..bd31bcf8 100644 --- a/src/distribution/chi_squared.rs +++ b/src/distribution/chi_squared.rs @@ -101,6 +101,7 @@ impl std::fmt::Display for ChiSquared { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for ChiSquared { fn sample(&self, r: &mut R) -> f64 { ::rand::distributions::Distribution::sample(&self.g, r) diff --git a/src/distribution/dirac.rs b/src/distribution/dirac.rs index ec833d93..8bd782f6 100644 --- a/src/distribution/dirac.rs +++ b/src/distribution/dirac.rs @@ -69,6 +69,7 @@ impl std::fmt::Display for Dirac { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Dirac { fn sample(&self, _: &mut R) -> f64 { self.0 diff --git a/src/distribution/dirichlet.rs b/src/distribution/dirichlet.rs index 7c7c9913..fffab765 100644 --- a/src/distribution/dirichlet.rs +++ b/src/distribution/dirichlet.rs @@ -192,6 +192,7 @@ where } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution> for Dirichlet where D: Dim, diff --git a/src/distribution/discrete_uniform.rs b/src/distribution/discrete_uniform.rs index 85b26090..55ec1b2e 100644 --- a/src/distribution/discrete_uniform.rs +++ b/src/distribution/discrete_uniform.rs @@ -75,6 +75,7 @@ impl std::fmt::Display for DiscreteUniform { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for DiscreteUniform { fn sample(&self, rng: &mut R) -> f64 { rng.gen_range(self.min..=self.max) as f64 diff --git a/src/distribution/empirical.rs b/src/distribution/empirical.rs index b27803d2..b2ab498d 100644 --- a/src/distribution/empirical.rs +++ b/src/distribution/empirical.rs @@ -175,6 +175,7 @@ impl std::fmt::Display for Empirical { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Empirical { fn sample(&self, rng: &mut R) -> f64 { use crate::distribution::Uniform; diff --git a/src/distribution/erlang.rs b/src/distribution/erlang.rs index 2ad017f3..89e5e4ec 100644 --- a/src/distribution/erlang.rs +++ b/src/distribution/erlang.rs @@ -83,6 +83,7 @@ impl std::fmt::Display for Erlang { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Erlang { fn sample(&self, rng: &mut R) -> f64 { ::rand::distributions::Distribution::sample(&self.g, rng) diff --git a/src/distribution/exponential.rs b/src/distribution/exponential.rs index 9c6c21fc..556812a5 100644 --- a/src/distribution/exponential.rs +++ b/src/distribution/exponential.rs @@ -91,6 +91,7 @@ impl std::fmt::Display for Exp { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Exp { fn sample(&self, r: &mut R) -> f64 { use crate::distribution::ziggurat; diff --git a/src/distribution/fisher_snedecor.rs b/src/distribution/fisher_snedecor.rs index 3208f98f..4e204225 100644 --- a/src/distribution/fisher_snedecor.rs +++ b/src/distribution/fisher_snedecor.rs @@ -124,6 +124,7 @@ impl std::fmt::Display for FisherSnedecor { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for FisherSnedecor { fn sample(&self, rng: &mut R) -> f64 { (super::gamma::sample_unchecked(rng, self.freedom_1 / 2.0, 0.5) * self.freedom_2) diff --git a/src/distribution/gamma.rs b/src/distribution/gamma.rs index 89341439..a403355d 100644 --- a/src/distribution/gamma.rs +++ b/src/distribution/gamma.rs @@ -122,6 +122,7 @@ impl std::fmt::Display for Gamma { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Gamma { fn sample(&self, rng: &mut R) -> f64 { sample_unchecked(rng, self.shape, self.rate) @@ -401,6 +402,7 @@ impl Continuous for Gamma { /// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, /// Pages 363-372 #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] pub fn sample_unchecked(rng: &mut R, shape: f64, rate: f64) -> f64 { let mut a = shape; let mut afix = 1.0; diff --git a/src/distribution/geometric.rs b/src/distribution/geometric.rs index 82af5eef..81e7439d 100644 --- a/src/distribution/geometric.rs +++ b/src/distribution/geometric.rs @@ -91,6 +91,7 @@ impl std::fmt::Display for Geometric { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Geometric { fn sample(&self, r: &mut R) -> f64 { use ::rand::distributions::OpenClosed01; diff --git a/src/distribution/hypergeometric.rs b/src/distribution/hypergeometric.rs index ac39917d..089f351b 100644 --- a/src/distribution/hypergeometric.rs +++ b/src/distribution/hypergeometric.rs @@ -146,6 +146,7 @@ impl std::fmt::Display for Hypergeometric { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Hypergeometric { fn sample(&self, rng: &mut R) -> f64 { let mut population = self.population as f64; diff --git a/src/distribution/inverse_gamma.rs b/src/distribution/inverse_gamma.rs index db101fd0..c34f99fa 100644 --- a/src/distribution/inverse_gamma.rs +++ b/src/distribution/inverse_gamma.rs @@ -119,6 +119,7 @@ impl std::fmt::Display for InverseGamma { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for InverseGamma { fn sample(&self, r: &mut R) -> f64 { 1.0 / super::gamma::sample_unchecked(r, self.shape, self.rate) diff --git a/src/distribution/laplace.rs b/src/distribution/laplace.rs index 168859ea..f0d5b8ea 100644 --- a/src/distribution/laplace.rs +++ b/src/distribution/laplace.rs @@ -111,6 +111,7 @@ impl std::fmt::Display for Laplace { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Laplace { fn sample(&self, rng: &mut R) -> f64 { let x: f64 = rng.gen_range(-0.5..0.5); diff --git a/src/distribution/log_normal.rs b/src/distribution/log_normal.rs index 2cf9d7cb..4ddbb43e 100644 --- a/src/distribution/log_normal.rs +++ b/src/distribution/log_normal.rs @@ -88,6 +88,7 @@ impl std::fmt::Display for LogNormal { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for LogNormal { fn sample(&self, rng: &mut R) -> f64 { super::normal::sample_unchecked(rng, self.location, self.scale).exp() diff --git a/src/distribution/mod.rs b/src/distribution/mod.rs index b0b50f67..5aa5ff5d 100644 --- a/src/distribution/mod.rs +++ b/src/distribution/mod.rs @@ -51,6 +51,7 @@ mod chi; mod chi_squared; mod dirac; #[cfg(feature = "nalgebra")] +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))] mod dirichlet; mod discrete_uniform; mod empirical; @@ -67,10 +68,13 @@ mod inverse_gamma; mod laplace; mod log_normal; #[cfg(feature = "nalgebra")] +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))] mod multinomial; #[cfg(feature = "nalgebra")] +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))] mod multivariate_normal; #[cfg(feature = "nalgebra")] +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))] mod multivariate_students_t; mod negative_binomial; mod normal; @@ -81,8 +85,10 @@ mod triangular; mod uniform; mod weibull; #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] mod ziggurat; #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] mod ziggurat_tables; /// The `ContinuousCDF` trait is used to specify an interface for univariate diff --git a/src/distribution/multinomial.rs b/src/distribution/multinomial.rs index d6304214..dd36f704 100644 --- a/src/distribution/multinomial.rs +++ b/src/distribution/multinomial.rs @@ -160,6 +160,7 @@ where } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution> for Multinomial where D: Dim, diff --git a/src/distribution/multivariate_normal.rs b/src/distribution/multivariate_normal.rs index 33df4969..6eb3b777 100644 --- a/src/distribution/multivariate_normal.rs +++ b/src/distribution/multivariate_normal.rs @@ -239,6 +239,7 @@ where } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution> for MultivariateNormal where D: Dim, diff --git a/src/distribution/multivariate_students_t.rs b/src/distribution/multivariate_students_t.rs index 05532431..919e3ebf 100644 --- a/src/distribution/multivariate_students_t.rs +++ b/src/distribution/multivariate_students_t.rs @@ -197,6 +197,7 @@ where } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution> for MultivariateStudent where D: Dim, diff --git a/src/distribution/negative_binomial.rs b/src/distribution/negative_binomial.rs index 29e22eee..27b90aa4 100644 --- a/src/distribution/negative_binomial.rs +++ b/src/distribution/negative_binomial.rs @@ -136,6 +136,7 @@ impl std::fmt::Display for NegativeBinomial { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for NegativeBinomial { fn sample(&self, r: &mut R) -> u64 { use crate::distribution::{gamma, poisson}; diff --git a/src/distribution/normal.rs b/src/distribution/normal.rs index a264af50..653b1cc8 100644 --- a/src/distribution/normal.rs +++ b/src/distribution/normal.rs @@ -106,6 +106,7 @@ impl std::fmt::Display for Normal { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Normal { fn sample(&self, rng: &mut R) -> f64 { sample_unchecked(rng, self.mean, self.std_dev) @@ -348,6 +349,7 @@ pub fn ln_pdf_unchecked(x: f64, mean: f64, std_dev: f64) -> f64 { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] /// draws a sample from a normal distribution using the Box-Muller algorithm pub fn sample_unchecked(rng: &mut R, mean: f64, std_dev: f64) -> f64 { use crate::distribution::ziggurat; diff --git a/src/distribution/pareto.rs b/src/distribution/pareto.rs index 1de73d84..d3dd9681 100644 --- a/src/distribution/pareto.rs +++ b/src/distribution/pareto.rs @@ -113,6 +113,7 @@ impl std::fmt::Display for Pareto { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Pareto { fn sample(&self, rng: &mut R) -> f64 { use rand::distributions::OpenClosed01; diff --git a/src/distribution/poisson.rs b/src/distribution/poisson.rs index b3f1ebab..45958176 100644 --- a/src/distribution/poisson.rs +++ b/src/distribution/poisson.rs @@ -90,6 +90,7 @@ impl std::fmt::Display for Poisson { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Poisson { /// Generates one sample from the Poisson distribution either by /// Knuth's method if lambda < 30.0 or Rejection method PA by @@ -284,6 +285,7 @@ impl Discrete for Poisson { /// Series C (Applied Statistics) Vol. 28 No. 1. (1979) pp. 29 - 35 /// otherwise #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] pub fn sample_unchecked(rng: &mut R, lambda: f64) -> f64 { if lambda < 30.0 { let limit = (-lambda).exp(); diff --git a/src/distribution/students_t.rs b/src/distribution/students_t.rs index 117fbc87..485092b5 100644 --- a/src/distribution/students_t.rs +++ b/src/distribution/students_t.rs @@ -143,6 +143,7 @@ impl std::fmt::Display for StudentsT { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for StudentsT { fn sample(&self, r: &mut R) -> f64 { // based on method 2, section 5 in chapter 9 of L. Devroye's diff --git a/src/distribution/triangular.rs b/src/distribution/triangular.rs index 1a83be2c..6be868bb 100644 --- a/src/distribution/triangular.rs +++ b/src/distribution/triangular.rs @@ -112,6 +112,7 @@ impl std::fmt::Display for Triangular { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Triangular { fn sample(&self, rng: &mut R) -> f64 { sample_unchecked(rng, self.min, self.max, self.mode) @@ -383,6 +384,7 @@ impl Continuous for Triangular { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] fn sample_unchecked(rng: &mut R, min: f64, max: f64, mode: f64) -> f64 { let f: f64 = rng.gen(); if f < (mode - min) / (max - min) { diff --git a/src/distribution/uniform.rs b/src/distribution/uniform.rs index 388a6d1d..96a2f49c 100644 --- a/src/distribution/uniform.rs +++ b/src/distribution/uniform.rs @@ -120,6 +120,7 @@ impl std::fmt::Display for Uniform { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Uniform { fn sample(&self, rng: &mut R) -> f64 { let d = rand::distributions::Uniform::new_inclusive(self.min, self.max); @@ -495,6 +496,7 @@ mod tests { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] #[test] fn test_samples_in_range() { use rand::rngs::StdRng; diff --git a/src/distribution/weibull.rs b/src/distribution/weibull.rs index aacf7bb6..6447aaa1 100644 --- a/src/distribution/weibull.rs +++ b/src/distribution/weibull.rs @@ -121,6 +121,7 @@ impl std::fmt::Display for Weibull { } #[cfg(feature = "rand")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Weibull { fn sample(&self, rng: &mut R) -> f64 { let x: f64 = rng.gen(); diff --git a/src/lib.rs b/src/lib.rs index 95d04473..d4d80ebc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ #![allow(clippy::many_single_char_names)] #![forbid(unsafe_code)] #![cfg_attr(coverage_nightly, feature(coverage_attribute))] +#![cfg_attr(docsrs, feature(doc_cfg))] #[macro_use] extern crate approx;