From 89efb71373822564fdc886329176034b2e2fdd85 Mon Sep 17 00:00:00 2001 From: wucke13 Date: Wed, 8 Jun 2022 10:12:26 +0200 Subject: [PATCH] fix #283: add possibility to include num-traits/libm for sin and cos in no_std builds --- Cargo.toml | 2 ++ src/lib.rs | 5 +++-- src/si/angle.rs | 8 ++++---- src/si/ratio.rs | 6 +++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 06647cb3..1cfea6f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index c095a00d..817e997a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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}; diff --git a/src/si/angle.rs b/src/si/angle.rs index 171a7c0d..46c7abde 100644 --- a/src/si/angle.rs +++ b/src/si/angle.rs @@ -1,6 +1,6 @@ //! Angle (dimensionless quantity). -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "libm"))] use super::ratio::Ratio; quantity! { @@ -64,7 +64,7 @@ impl Angle, f64> { } /// Implementation of various stdlib trigonometric functions -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "libm"))] impl Angle where U: crate::si::Units + ?Sized, @@ -121,7 +121,7 @@ where } } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "libm"))] impl crate::si::Quantity where D: crate::si::Dimension + ?Sized, @@ -161,7 +161,7 @@ mod tests { } } - #[cfg(feature = "std")] + #[cfg(any(feature = "std", feature = "libm"))] mod trig { storage_types! { types: Float; diff --git a/src/si/ratio.rs b/src/si/ratio.rs index bd789871..f2d05897 100644 --- a/src/si/ratio.rs +++ b/src/si/ratio.rs @@ -1,6 +1,6 @@ //! Ratio (dimensionless quantity). -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "libm"))] use super::angle::{Angle, radian}; quantity! { @@ -33,7 +33,7 @@ quantity! { } /// Implementation of various stdlib functions. -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "libm"))] impl Ratio where U: crate::si::Units + ?Sized, @@ -220,7 +220,7 @@ mod tests { } } - #[cfg(feature = "std")] + #[cfg(any(feature = "std", feature = "libm"))] mod float { storage_types! { types: Float;