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

Hide non-essential dependencies behind features #70

Merged
merged 7 commits into from
Oct 23, 2024
Merged
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
19 changes: 16 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Build and test Rust crate

on:
push:
Expand All @@ -12,10 +12,23 @@ env:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
features: ["", "ndarray", "approx", "ndarray approx", "python", "python_numpy", "num-dual"]

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --release
run: cargo build --release --features "${{ matrix.features }}"
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --release
run: cargo test --release --features approx
test_ndarray:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests (ndarray)
run: cargo test --release --features "ndarray approx"
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ exclude = ["/.github/*", "*.ipynb", "/docs"]
rustdoc-args = ["--html-in-header", "./src/docs-header.html"]

[dependencies]
ndarray = { version = "0.16", features = ["approx"] }
approx = "0.5"
num-traits = "0.2"
typenum = "1.17"
ndarray = { version = "0.16", optional = true}
approx = { version = "0.5", optional = true }
pyo3 = { version = "0.22", optional = true }
numpy = { version = "0.22", optional = true }
num-dual = { version = "0.10", optional = true }

[features]
default = []
python = ["pyo3", "numpy"]
python = ["pyo3"]
python_numpy = ["python", "numpy", "ndarray"]
approx = ["dep:approx", "ndarray?/approx"]
2 changes: 1 addition & 1 deletion example/extend_quantity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.0", features = ["extension-module", "abi3-py39"] }
quantity = { version = "*", path = "../../", features = ["python"] }
quantity = { version = "*", path = "../../", features = ["python_numpy"] }
ndarray = "0.16"
6 changes: 5 additions & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
#[cfg(feature = "ndarray")]
use ndarray::{Array, Dimension};
use std::collections::HashMap;
use std::fmt;
Expand Down Expand Up @@ -35,7 +36,7 @@ impl<
}
}

#[cfg(feature = "python")]
#[cfg(feature = "pyo3")]
pub(crate) trait PrintUnit {
const UNIT: &'static str;
}
Expand Down Expand Up @@ -64,6 +65,7 @@ macro_rules! impl_fmt {
}
}

#[cfg(feature = "ndarray")]
impl<D: Dimension> fmt::Display
for Quantity<Array<f64, D>, SIUnit<$t, $l, $m, $i, $theta, $n, Z0>>
{
Expand Down Expand Up @@ -179,6 +181,7 @@ static PREFIX_SYMBOLS: LazyLock<HashMap<i8, &'static str>> = LazyLock::new(|| {
#[cfg(test)]
mod tests {
use crate::*;
#[cfg(feature = "ndarray")]
use ndarray::arr1;

#[test]
Expand All @@ -193,6 +196,7 @@ mod tests {
}

#[test]
#[cfg(feature = "ndarray")]
fn test_fmt_arr() {
assert_eq!(
format!("{}", arr1(&[273.15, 323.15]) * KELVIN),
Expand Down
19 changes: 8 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@
//! ```
//!
//! Calculate the pressure distribution in the atmosphere using the barometric formula.
//! Array operations require the `ndarray` feature.
//! ```
//! # #[cfg(feature = "ndarray")]
//! # {
//! # use quantity::*;
//! # use typenum::P2;
//! let z = Length::linspace(1.0 * METER, 70.0 * KILO * METER, 10);
Expand All @@ -133,15 +136,17 @@
//! // z = 54.44467 km p = 140.51557 Pa
//! // z = 62.22233 km p = 54.98750 Pa
//! // z = 70.00000 km p = 21.51808 Pa
//! # }
//! ```

#![warn(clippy::all)]
#[cfg(feature = "ndarray")]
use ndarray::{Array, ArrayBase, Data, Dimension};
use num_traits::Zero;
use std::marker::PhantomData;
use std::ops::{Div, Mul};
use typenum::{ATerm, Diff, Integer, Negate, Quot, Sum, TArr, N1, N2, P1, P3, Z0};

#[cfg(feature = "ndarray")]
mod array;
mod fmt;
mod ops;
Expand Down Expand Up @@ -399,6 +404,7 @@ impl Mul<CELSIUS> for f64 {
}
}

#[cfg(feature = "ndarray")]
impl<S: Data<Elem = f64>, D: Dimension> Mul<CELSIUS> for ArrayBase<S, D> {
type Output = Temperature<Array<f64, D>>;
#[allow(clippy::suspicious_arithmetic_impl)]
Expand All @@ -415,6 +421,7 @@ impl Div<CELSIUS> for Temperature<f64> {
}
}

#[cfg(feature = "ndarray")]
impl<D: Dimension> Div<CELSIUS> for Temperature<Array<f64, D>> {
type Output = Array<f64, D>;
#[allow(clippy::suspicious_arithmetic_impl)]
Expand Down Expand Up @@ -454,16 +461,6 @@ impl<T, U> Quantity<T, U> {
}
}

impl<U> Zero for Quantity<f64, U> {
fn zero() -> Self {
Quantity(0.0, PhantomData)
}

fn is_zero(&self) -> bool {
self.0.is_zero()
}
}

#[cfg(feature = "num-dual")]
mod num_dual {
use super::Quantity;
Expand Down
12 changes: 8 additions & 4 deletions src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::Quantity;
#[cfg(feature = "approx")]
use approx::{AbsDiffEq, RelativeEq};
#[cfg(feature = "ndarray")]
use ndarray::{Array, ArrayBase, Data, DataMut, DataOwned, Dimension};
use std::marker::PhantomData;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
Expand Down Expand Up @@ -77,13 +79,15 @@ where
}
}

#[cfg(feature = "ndarray")]
impl<U, S: Data<Elem = f64>, D: Dimension> Mul<Quantity<f64, U>> for &ArrayBase<S, D> {
type Output = Quantity<Array<f64, D>, U>;
fn mul(self, other: Quantity<f64, U>) -> Self::Output {
Quantity(self * other.0, PhantomData)
}
}

#[cfg(feature = "ndarray")]
impl<U, S: DataOwned<Elem = f64> + DataMut, D: Dimension> Mul<Quantity<f64, U>>
for ArrayBase<S, D>
{
Expand Down Expand Up @@ -175,13 +179,15 @@ where
}
}

#[cfg(feature = "ndarray")]
impl<U: Neg, S: Data<Elem = f64>, D: Dimension> Div<Quantity<f64, U>> for &ArrayBase<S, D> {
type Output = Quantity<Array<f64, D>, Negate<U>>;
fn div(self, other: Quantity<f64, U>) -> Self::Output {
Quantity(self / other.0, PhantomData)
}
}

#[cfg(feature = "ndarray")]
impl<U: Neg, S: DataOwned<Elem = f64> + DataMut, D: Dimension> Div<Quantity<f64, U>>
for ArrayBase<S, D>
{
Expand Down Expand Up @@ -335,7 +341,6 @@ impl<U> Quantity<f64, U> {
/// # Example
/// ```
/// # use quantity::METER;
/// # use ndarray::arr1;
/// # use approx::assert_relative_eq;
/// # use typenum::P2;
/// let x = 3.0 * METER;
Expand All @@ -353,7 +358,6 @@ impl<U> Quantity<f64, U> {
/// # Example
/// ```
/// # use quantity::METER;
/// # use ndarray::arr1;
/// # use approx::assert_relative_eq;
/// let x = 9.0 * METER * METER;
/// assert_relative_eq!(x.sqrt(), 3.0 * METER);
Expand All @@ -370,7 +374,6 @@ impl<U> Quantity<f64, U> {
/// # Example
/// ```
/// # use quantity::METER;
/// # use ndarray::arr1;
/// # use approx::assert_relative_eq;
/// let x = 27.0 * METER * METER * METER;
/// assert_relative_eq!(x.cbrt(), 3.0 * METER);
Expand All @@ -387,7 +390,6 @@ impl<U> Quantity<f64, U> {
/// # Example
/// ```
/// # use quantity::METER;
/// # use ndarray::arr1;
/// # use approx::assert_relative_eq;
/// # use typenum::P4;
/// let x = 81.0 * METER * METER * METER * METER;
Expand Down Expand Up @@ -479,6 +481,7 @@ impl<T: PartialOrd, U> PartialOrd for Quantity<T, U> {
}
}

#[cfg(feature = "approx")]
impl<T: AbsDiffEq, U> AbsDiffEq for Quantity<T, U> {
type Epsilon = T::Epsilon;

Expand All @@ -491,6 +494,7 @@ impl<T: AbsDiffEq, U> AbsDiffEq for Quantity<T, U> {
}
}

#[cfg(feature = "approx")]
impl<T: RelativeEq, U> RelativeEq for Quantity<T, U> {
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
Expand Down
7 changes: 5 additions & 2 deletions src/python.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::fmt::PrintUnit;

use super::{Quantity, SIUnit};
use crate::fmt::PrintUnit;
#[cfg(feature = "ndarray")]
use ndarray::{Array, Dimension};
#[cfg(feature = "ndarray")]
use numpy::{IntoPyArray, PyReadonlyArray};
use pyo3::{exceptions::PyValueError, prelude::*};
use std::{marker::PhantomData, sync::LazyLock};
Expand All @@ -26,6 +27,7 @@ impl<T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer,
}
}

#[cfg(feature = "ndarray")]
impl<
T: Integer,
L: Integer,
Expand Down Expand Up @@ -74,6 +76,7 @@ where
}
}

#[cfg(feature = "ndarray")]
impl<
'py,
T: Integer,
Expand Down
Loading