Skip to content

Commit

Permalink
fix #283: add possibility to include num-traits/libm for sin and cos …
Browse files Browse the repository at this point in the history
…in no_std builds
  • Loading branch information
wucke13 committed Jun 8, 2022
1 parent 49fc627 commit 597a19e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ use_serde = ["serde"]
rational-support = ["num-rational"]
bigint-support = ["num-bigint", "num-rational/num-bigint-std"]
complex-support = ["num-complex"]
# enable sin/cos for no_std build via libm
libm = [ "num-traits/libm" ]

[[example]]
name = "base"
Expand Down
6 changes: 3 additions & 3 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ macro_rules! si {
($($tt:tt)*) => {};
}

/// Expands the given block of code when `uom` is compiled with the `std` feature.
/// Expands the given block of code when `uom` is compiled with either `std` or `libm` feature.
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "std")]
macro_rules! std {
#[cfg(any(feature = "std", feature = "libm"))]
macro_rules! std_or_libm {
($($tt:tt)*) => { $($tt)* };
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
//! `uom` with `no_std`. Enabled by default.
//! * `use_serde` -- Feature to enable support for serialization and deserialization of quantities
//! with the [Serde][serde] crate. Disabled by default.
//! * `libm` -- Enable use of trigonometrical functions (sin, cos, ...) on no_std targets
//!
//! [Serde][serde] support for the `big*` and `rational*` underlying storage types requires
//! manually enabling the `serde` feature for the `num-rational` and `num-bigint` crates. To do
Expand Down Expand Up @@ -277,9 +278,9 @@ pub mod lib {
// Conditionally import num sub-crate types based on feature selection.
#[doc(hidden)]
pub mod num {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
pub use num_traits::float::Float;
#[cfg(not(feature = "std"))]
#[cfg(not(any(feature = "std", feature = "libm")))]
pub use num_traits::float::FloatCore as Float;

pub use num_traits::{pow, FromPrimitive, Num, One, Saturating, Signed, ToPrimitive, Zero};
Expand Down
8 changes: 4 additions & 4 deletions src/si/angle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Angle (dimensionless quantity).

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use super::ratio::Ratio;

quantity! {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Angle<crate::si::SI<f64>, f64> {
}

/// Implementation of various stdlib trigonometric functions
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<U, V> Angle<U, V>
where
U: crate::si::Units<V> + ?Sized,
Expand Down Expand Up @@ -121,7 +121,7 @@ where
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<D, U, V> crate::si::Quantity<D, U, V>
where
D: crate::si::Dimension + ?Sized,
Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
mod trig {
storage_types! {
types: Float;
Expand Down
6 changes: 3 additions & 3 deletions src/si/ratio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Ratio (dimensionless quantity).

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use super::angle::{Angle, radian};

quantity! {
Expand Down Expand Up @@ -33,7 +33,7 @@ quantity! {
}

/// Implementation of various stdlib functions.
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<U, V> Ratio<U, V>
where
U: crate::si::Units<V> + ?Sized,
Expand Down Expand Up @@ -220,7 +220,7 @@ mod tests {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
mod float {
storage_types! {
types: Float;
Expand Down
4 changes: 2 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ macro_rules! system {
self.value.classify()
}

std! {
std_or_libm! {
autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -807,7 +807,7 @@ macro_rules! system {
self.value.is_normal()
}

std! {
std_or_libm! {
/// Takes the cubic root of a number.
///
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
Expand Down

0 comments on commit 597a19e

Please sign in to comment.