Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #![no_std] support #314

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ jobs:
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable

- name: Test no_std
run: cargo test --no-default-features -F rand

- name: Test default features
run: cargo test

Expand Down
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ path = "src/lib.rs"
[[bench]]
name = "order_statistics"
harness = false
required-features = ["rand"]
required-features = ["rand", "std"]

[features]
default = ["nalgebra", "rand"]
nalgebra = ["dep:nalgebra"]
default = ["std", "nalgebra", "rand"]
std = ["nalgebra?/std", "rand?/std"]
# at the moment, all nalgebra features needs std
nalgebra = ["dep:nalgebra", "std"]
rand = ["dep:rand", "nalgebra?/rand"]

[dependencies]
Expand All @@ -36,12 +38,12 @@ num-traits = "0.2.14"
[dependencies.rand]
version = "0.8"
optional = true
default-features = false

[dependencies.nalgebra]
version = "0.33"
optional = true
default-features = false
features = ["std"]

[dev-dependencies]
criterion = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
}
}

impl std::fmt::Display for Bernoulli {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Bernoulli {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 82 in src/distribution/bernoulli.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/bernoulli.rs#L82

Added line #L82 was not covered by tests
write!(f, "Bernoulli({})", self.p())
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
ShapeBInvalid,
}

impl std::fmt::Display for BetaError {
impl core::fmt::Display for BetaError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
BetaError::ShapeAInvalid => write!(f, "Shape A is NaN, infinite, zero or negative"),
BetaError::ShapeBInvalid => write!(f, "Shape B is NaN, infinite, zero or negative"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for BetaError {}

impl Beta {
Expand Down Expand Up @@ -106,8 +107,8 @@
}
}

impl std::fmt::Display for Beta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Beta {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 111 in src/distribution/beta.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/beta.rs#L111

Added line #L111 was not covered by tests
write!(f, "Beta(a={}, b={})", self.shape_a, self.shape_b)
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::function::{beta, factorial};
use crate::statistics::*;
use std::f64;
use core::f64;

/// Implements the
/// [Binomial](https://en.wikipedia.org/wiki/Binomial_distribution)
Expand Down Expand Up @@ -32,15 +32,16 @@
ProbabilityInvalid,
}

impl std::fmt::Display for BinomialError {
impl core::fmt::Display for BinomialError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
BinomialError::ProbabilityInvalid => write!(f, "Probability is NaN or not in [0, 1]"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for BinomialError {}

impl Binomial {
Expand Down Expand Up @@ -103,8 +104,8 @@
}
}

impl std::fmt::Display for Binomial {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Binomial {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 108 in src/distribution/binomial.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/binomial.rs#L108

Added line #L108 was not covered by tests
write!(f, "Bin({},{})", self.p, self.n)
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::statistics::*;
use std::f64;
use core::f64;

/// Implements the
/// [Categorical](https://en.wikipedia.org/wiki/Categorical_distribution)
Expand Down Expand Up @@ -39,9 +39,9 @@
ProbMassHasInvalidElements,
}

impl std::fmt::Display for CategoricalError {
impl core::fmt::Display for CategoricalError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
CategoricalError::ProbMassEmpty => write!(f, "Probability mass is empty"),
CategoricalError::ProbMassSumZero => write!(f, "Probabilities sum up to zero"),
Expand All @@ -53,6 +53,7 @@
}
}

#[cfg(feature = "std")]
impl std::error::Error for CategoricalError {}

impl Categorical {
Expand Down Expand Up @@ -117,8 +118,8 @@
}
}

impl std::fmt::Display for Categorical {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Categorical {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 122 in src/distribution/categorical.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/categorical.rs#L122

Added line #L122 was not covered by tests
write!(f, "Cat({:#?})", self.norm_pmf)
}
}
Expand Down Expand Up @@ -371,7 +372,7 @@
// return 0. Otherwise val returns the index of the first element larger than
// it within the search array.
fn binary_index(search: &[f64], val: f64) -> usize {
use std::cmp;
use core::cmp;

let mut low = 0_isize;
let mut high = search.len() as isize - 1;
Expand Down
11 changes: 6 additions & 5 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::statistics::*;
use std::f64;
use core::f64;

/// Implements the [Cauchy](https://en.wikipedia.org/wiki/Cauchy_distribution)
/// distribution, also known as the Lorentz distribution.
Expand Down Expand Up @@ -32,16 +32,17 @@
ScaleInvalid,
}

impl std::fmt::Display for CauchyError {
impl core::fmt::Display for CauchyError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
CauchyError::LocationInvalid => write!(f, "Location is NaN"),
CauchyError::ScaleInvalid => write!(f, "Scale is NaN, zero or less than zero"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for CauchyError {}

impl Cauchy {
Expand Down Expand Up @@ -104,8 +105,8 @@
}
}

impl std::fmt::Display for Cauchy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Cauchy {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 109 in src/distribution/cauchy.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/cauchy.rs#L109

Added line #L109 was not covered by tests
write!(f, "Cauchy({}, {})", self.location, self.scale)
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::gamma;
use crate::statistics::*;
use std::f64;
use std::num::NonZeroU64;
use core::f64;
use core::num::NonZeroU64;

/// Implements the [Chi](https://en.wikipedia.org/wiki/Chi_distribution)
/// distribution
Expand Down Expand Up @@ -31,9 +31,9 @@
FreedomInvalid,
}

impl std::fmt::Display for ChiError {
impl core::fmt::Display for ChiError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
ChiError::FreedomInvalid => {
write!(f, "Degrees of freedom are zero")
Expand All @@ -42,6 +42,7 @@
}
}

#[cfg(feature = "std")]
impl std::error::Error for ChiError {}

impl Chi {
Expand Down Expand Up @@ -86,8 +87,8 @@
}
}

impl std::fmt::Display for Chi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Chi {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 91 in src/distribution/chi.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/chi.rs#L91

Added line #L91 was not covered by tests
write!(f, "χ_{}", self.freedom)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::distribution::{Continuous, ContinuousCDF, Gamma, GammaError};
use crate::statistics::*;
use std::f64;
use core::f64;

/// Implements the
/// [Chi-squared](https://en.wikipedia.org/wiki/Chi-squared_distribution)
Expand Down Expand Up @@ -94,8 +94,8 @@
}
}

impl std::fmt::Display for ChiSquared {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for ChiSquared {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 98 in src/distribution/chi_squared.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/chi_squared.rs#L98

Added line #L98 was not covered by tests
write!(f, "χ^2_{}", self.freedom)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
ValueInvalid,
}

impl std::fmt::Display for DiracError {
impl core::fmt::Display for DiracError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
DiracError::ValueInvalid => write!(f, "Value v is NaN"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DiracError {}

impl Dirac {
Expand Down Expand Up @@ -62,8 +63,8 @@
}
}

impl std::fmt::Display for Dirac {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Dirac {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 67 in src/distribution/dirac.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/dirac.rs#L67

Added line #L67 was not covered by tests
write!(f, "δ_{}", self.0)
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::function::gamma;
use crate::prec;
use crate::statistics::*;
use core::f64;
use nalgebra::{Dim, Dyn, OMatrix, OVector};
use std::f64;

/// Implements the
/// [Dirichlet](https://en.wikipedia.org/wiki/Dirichlet_distribution)
Expand Down Expand Up @@ -41,9 +41,9 @@
AlphaHasInvalidElements,
}

impl std::fmt::Display for DirichletError {
impl core::fmt::Display for DirichletError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
DirichletError::AlphaTooShort => write!(f, "Alpha contains less than two elements"),
DirichletError::AlphaHasInvalidElements => write!(
Expand All @@ -54,6 +54,7 @@
}
}

#[cfg(feature = "std")]
impl std::error::Error for DirichletError {}

impl Dirichlet<Dyn> {
Expand Down Expand Up @@ -181,12 +182,12 @@
}
}

impl<D> std::fmt::Display for Dirichlet<D>
impl<D> core::fmt::Display for Dirichlet<D>
where
D: Dim,
nalgebra::DefaultAllocator: nalgebra::allocator::Allocator<D>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 190 in src/distribution/dirichlet.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/dirichlet.rs#L190

Added line #L190 was not covered by tests
write!(f, "Dir({}, {})", self.alpha.len(), &self.alpha)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
MinMaxInvalid,
}

impl std::fmt::Display for DiscreteUniformError {
impl core::fmt::Display for DiscreteUniformError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
DiscreteUniformError::MinMaxInvalid => write!(f, "Maximum is less than minimum"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DiscreteUniformError {}

impl DiscreteUniform {
Expand Down Expand Up @@ -68,8 +69,8 @@
}
}

impl std::fmt::Display for DiscreteUniform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for DiscreteUniform {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 73 in src/distribution/discrete_uniform.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/discrete_uniform.rs#L73

Added line #L73 was not covered by tests
write!(f, "Uni([{}, {}])", self.min, self.max)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ impl Empirical {
}
}

impl std::fmt::Display for Empirical {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Empirical {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut enumerated_values = self
.data
.iter()
.flat_map(|(x, &count)| std::iter::repeat(x.get()).take(count as usize));
.flat_map(|(x, &count)| core::iter::repeat(x.get()).take(count as usize));

if let Some(x) = enumerated_values.next() {
write!(f, "Empirical([{x:.3e}")?;
Expand Down
Loading
Loading