Skip to content

Commit

Permalink
doc: add doc(cfg(feature = "...'))
Browse files Browse the repository at this point in the history
This will add a note "Available on crate feature <...> only."
to feature-gated structs, impls etc.
  • Loading branch information
FreezyLemon authored and YeungOnion committed Sep 17, 2024
1 parent 8e78b2c commit 3c3cb4d
Show file tree
Hide file tree
Showing 34 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl std::fmt::Display for Bernoulli {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Bernoulli {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.gen_bool(self.p()) as u8 as f64
Expand Down
1 change: 1 addition & 0 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl std::fmt::Display for Beta {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Beta {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
// Generated by sampling two gamma distributions and normalizing.
Expand Down
1 change: 1 addition & 0 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl std::fmt::Display for Binomial {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Binomial {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
(0..self.n).fold(0.0, |acc, _| {
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl std::fmt::Display for Categorical {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Categorical {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
sample_unchecked(rng, &self.cdf)
Expand Down Expand Up @@ -323,6 +324,7 @@ impl Discrete<u64, f64> 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<R: ::rand::Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> f64 {
let draw = rng.gen::<f64>() * cdf.last().unwrap();
cdf.iter()
Expand Down
1 change: 1 addition & 0 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl std::fmt::Display for Cauchy {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Cauchy {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
self.location + self.scale * (f64::consts::PI * (r.gen::<f64>() - 0.5)).tan()
Expand Down
1 change: 1 addition & 0 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl std::fmt::Display for Chi {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Chi {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
(0..self.freedom as i64)
Expand Down
1 change: 1 addition & 0 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl std::fmt::Display for ChiSquared {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for ChiSquared {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
::rand::distributions::Distribution::sample(&self.g, r)
Expand Down
1 change: 1 addition & 0 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl std::fmt::Display for Dirac {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Dirac {
fn sample<R: ::rand::Rng + ?Sized>(&self, _: &mut R) -> f64 {
self.0
Expand Down
1 change: 1 addition & 0 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ where
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<D> ::rand::distributions::Distribution<OVector<f64, D>> for Dirichlet<D>
where
D: Dim,
Expand Down
1 change: 1 addition & 0 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl std::fmt::Display for DiscreteUniform {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for DiscreteUniform {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.gen_range(self.min..=self.max) as f64
Expand Down
1 change: 1 addition & 0 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl std::fmt::Display for Empirical {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Empirical {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
use crate::distribution::Uniform;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl std::fmt::Display for Erlang {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Erlang {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
::rand::distributions::Distribution::sample(&self.g, rng)
Expand Down
1 change: 1 addition & 0 deletions src/distribution/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl std::fmt::Display for Exp {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Exp {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
use crate::distribution::ziggurat;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/fisher_snedecor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl std::fmt::Display for FisherSnedecor {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for FisherSnedecor {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
(super::gamma::sample_unchecked(rng, self.freedom_1 / 2.0, 0.5) * self.freedom_2)
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl std::fmt::Display for Gamma {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Gamma {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
sample_unchecked(rng, self.shape, self.rate)
Expand Down Expand Up @@ -401,6 +402,7 @@ impl Continuous<f64, f64> 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<R: ::rand::Rng + ?Sized>(rng: &mut R, shape: f64, rate: f64) -> f64 {
let mut a = shape;
let mut afix = 1.0;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/geometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl std::fmt::Display for Geometric {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Geometric {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
use ::rand::distributions::OpenClosed01;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/hypergeometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl std::fmt::Display for Hypergeometric {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Hypergeometric {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let mut population = self.population as f64;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/inverse_gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl std::fmt::Display for InverseGamma {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for InverseGamma {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
1.0 / super::gamma::sample_unchecked(r, self.shape, self.rate)
Expand Down
1 change: 1 addition & 0 deletions src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl std::fmt::Display for Laplace {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Laplace {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.gen_range(-0.5..0.5);
Expand Down
1 change: 1 addition & 0 deletions src/distribution/log_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl std::fmt::Display for LogNormal {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for LogNormal {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
super::normal::sample_unchecked(rng, self.location, self.scale).exp()
Expand Down
6 changes: 6 additions & 0 deletions src/distribution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/distribution/multinomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ where
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<D> ::rand::distributions::Distribution<OVector<f64, D>> for Multinomial<D>
where
D: Dim,
Expand Down
1 change: 1 addition & 0 deletions src/distribution/multivariate_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ where
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<D> ::rand::distributions::Distribution<OVector<f64, D>> for MultivariateNormal<D>
where
D: Dim,
Expand Down
1 change: 1 addition & 0 deletions src/distribution/multivariate_students_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ where
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<D> ::rand::distributions::Distribution<OVector<f64, D>> for MultivariateStudent<D>
where
D: Dim,
Expand Down
1 change: 1 addition & 0 deletions src/distribution/negative_binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl std::fmt::Display for NegativeBinomial {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<u64> for NegativeBinomial {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> u64 {
use crate::distribution::{gamma, poisson};
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl std::fmt::Display for Normal {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Normal {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
sample_unchecked(rng, self.mean, self.std_dev)
Expand Down Expand Up @@ -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<R: ::rand::Rng + ?Sized>(rng: &mut R, mean: f64, std_dev: f64) -> f64 {
use crate::distribution::ziggurat;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/pareto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl std::fmt::Display for Pareto {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Pareto {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
use rand::distributions::OpenClosed01;
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl std::fmt::Display for Poisson {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Poisson {
/// Generates one sample from the Poisson distribution either by
/// Knuth's method if lambda < 30.0 or Rejection method PA by
Expand Down Expand Up @@ -284,6 +285,7 @@ impl Discrete<u64, f64> 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<R: ::rand::Rng + ?Sized>(rng: &mut R, lambda: f64) -> f64 {
if lambda < 30.0 {
let limit = (-lambda).exp();
Expand Down
1 change: 1 addition & 0 deletions src/distribution/students_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl std::fmt::Display for StudentsT {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for StudentsT {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
// based on method 2, section 5 in chapter 9 of L. Devroye's
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl std::fmt::Display for Triangular {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Triangular {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
sample_unchecked(rng, self.min, self.max, self.mode)
Expand Down Expand Up @@ -383,6 +384,7 @@ impl Continuous<f64, f64> for Triangular {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, min: f64, max: f64, mode: f64) -> f64 {
let f: f64 = rng.gen();
if f < (mode - min) / (max - min) {
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl std::fmt::Display for Uniform {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Uniform {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let d = rand::distributions::Uniform::new_inclusive(self.min, self.max);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/distribution/weibull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl std::fmt::Display for Weibull {
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Weibull {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.gen();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3c3cb4d

Please sign in to comment.