Skip to content

Commit 14c9153

Browse files
committed
Add libm feature.
Allows use of `Pow` and `Float` traits in no_std builds. Fixes #164.
1 parent d722d5f commit 14c9153

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ borsh = { version = "1.2.0", optional = true, default-features = false }
2020
bytemuck = { version = "1.12.2", optional = true, default-features = false }
2121
derive-visitor = { version = "0.4.0", optional = true }
2222
num-cmp = { version = "0.1.0", optional = true }
23-
num-traits = { version = "0.2.1", default-features = false }
23+
num-traits = { version = "0.2.9", default-features = false }
2424
proptest = { version = "1.0.0", optional = true }
2525
rand = { version = "0.8.3", optional = true, default-features = false }
2626
rkyv = { version = "0.7.41", optional = true, default-features = false, features = ["rend"] }
@@ -34,6 +34,7 @@ serde_test = "1.0"
3434
[features]
3535
default = ["std"]
3636
std = ["num-traits/std"]
37+
libm = ["num-traits/libm"]
3738
serde = ["dep:serde", "rand?/serde1"]
3839
randtest = ["rand/std", "rand/std_rng"]
3940
rkyv = ["rkyv_32"]

src/lib.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use num_traits::float::FloatCore;
2727
use num_traits::{
2828
AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero,
2929
};
30-
#[cfg(feature = "std")]
30+
#[cfg(any(feature = "std", feature = "libm"))]
3131
pub use num_traits::{Float, Pow};
3232

3333
#[cfg(feature = "rand")]
@@ -571,7 +571,7 @@ impl_ordered_float_binop! {Rem, rem, RemAssign, rem_assign}
571571

572572
macro_rules! impl_ordered_float_pow {
573573
($inner:ty, $rhs:ty) => {
574-
#[cfg(feature = "std")]
574+
#[cfg(any(feature = "std", feature = "libm"))]
575575
impl Pow<$rhs> for OrderedFloat<$inner> {
576576
type Output = OrderedFloat<$inner>;
577577
#[inline]
@@ -580,7 +580,7 @@ macro_rules! impl_ordered_float_pow {
580580
}
581581
}
582582

583-
#[cfg(feature = "std")]
583+
#[cfg(any(feature = "std", feature = "libm"))]
584584
impl<'a> Pow<&'a $rhs> for OrderedFloat<$inner> {
585585
type Output = OrderedFloat<$inner>;
586586
#[inline]
@@ -589,7 +589,7 @@ macro_rules! impl_ordered_float_pow {
589589
}
590590
}
591591

592-
#[cfg(feature = "std")]
592+
#[cfg(any(feature = "std", feature = "libm"))]
593593
impl<'a> Pow<$rhs> for &'a OrderedFloat<$inner> {
594594
type Output = OrderedFloat<$inner>;
595595
#[inline]
@@ -598,7 +598,7 @@ macro_rules! impl_ordered_float_pow {
598598
}
599599
}
600600

601-
#[cfg(feature = "std")]
601+
#[cfg(any(feature = "std", feature = "libm"))]
602602
impl<'a, 'b> Pow<&'a $rhs> for &'b OrderedFloat<$inner> {
603603
type Output = OrderedFloat<$inner>;
604604
#[inline]
@@ -625,7 +625,7 @@ impl_ordered_float_pow! {f64, f64}
625625

626626
macro_rules! impl_ordered_float_self_pow {
627627
($base:ty, $exp:ty) => {
628-
#[cfg(feature = "std")]
628+
#[cfg(any(feature = "std", feature = "libm"))]
629629
impl Pow<OrderedFloat<$exp>> for OrderedFloat<$base> {
630630
type Output = OrderedFloat<$base>;
631631
#[inline]
@@ -634,7 +634,7 @@ macro_rules! impl_ordered_float_self_pow {
634634
}
635635
}
636636

637-
#[cfg(feature = "std")]
637+
#[cfg(any(feature = "std", feature = "libm"))]
638638
impl<'a> Pow<&'a OrderedFloat<$exp>> for OrderedFloat<$base> {
639639
type Output = OrderedFloat<$base>;
640640
#[inline]
@@ -643,7 +643,7 @@ macro_rules! impl_ordered_float_self_pow {
643643
}
644644
}
645645

646-
#[cfg(feature = "std")]
646+
#[cfg(any(feature = "std", feature = "libm"))]
647647
impl<'a> Pow<OrderedFloat<$exp>> for &'a OrderedFloat<$base> {
648648
type Output = OrderedFloat<$base>;
649649
#[inline]
@@ -652,7 +652,7 @@ macro_rules! impl_ordered_float_self_pow {
652652
}
653653
}
654654

655-
#[cfg(feature = "std")]
655+
#[cfg(any(feature = "std", feature = "libm"))]
656656
impl<'a, 'b> Pow<&'a OrderedFloat<$exp>> for &'b OrderedFloat<$base> {
657657
type Output = OrderedFloat<$base>;
658658
#[inline]
@@ -1037,7 +1037,7 @@ impl<T: FloatCore> FloatCore for OrderedFloat<T> {
10371037
}
10381038
}
10391039

1040-
#[cfg(feature = "std")]
1040+
#[cfg(any(feature = "std", feature = "libm"))]
10411041
impl<T: Float + FloatCore> Float for OrderedFloat<T> {
10421042
fn nan() -> Self {
10431043
OrderedFloat(<T as Float>::nan())
@@ -1663,7 +1663,7 @@ impl_not_nan_binop! {Rem, rem, RemAssign, rem_assign}
16631663
// Will panic if NaN value is return from the operation
16641664
macro_rules! impl_not_nan_pow {
16651665
($inner:ty, $rhs:ty) => {
1666-
#[cfg(feature = "std")]
1666+
#[cfg(any(feature = "std", feature = "libm"))]
16671667
impl Pow<$rhs> for NotNan<$inner> {
16681668
type Output = NotNan<$inner>;
16691669
#[inline]
@@ -1672,7 +1672,7 @@ macro_rules! impl_not_nan_pow {
16721672
}
16731673
}
16741674

1675-
#[cfg(feature = "std")]
1675+
#[cfg(any(feature = "std", feature = "libm"))]
16761676
impl<'a> Pow<&'a $rhs> for NotNan<$inner> {
16771677
type Output = NotNan<$inner>;
16781678
#[inline]
@@ -1681,7 +1681,7 @@ macro_rules! impl_not_nan_pow {
16811681
}
16821682
}
16831683

1684-
#[cfg(feature = "std")]
1684+
#[cfg(any(feature = "std", feature = "libm"))]
16851685
impl<'a> Pow<$rhs> for &'a NotNan<$inner> {
16861686
type Output = NotNan<$inner>;
16871687
#[inline]
@@ -1690,7 +1690,7 @@ macro_rules! impl_not_nan_pow {
16901690
}
16911691
}
16921692

1693-
#[cfg(feature = "std")]
1693+
#[cfg(any(feature = "std", feature = "libm"))]
16941694
impl<'a, 'b> Pow<&'a $rhs> for &'b NotNan<$inner> {
16951695
type Output = NotNan<$inner>;
16961696
#[inline]
@@ -1718,7 +1718,7 @@ impl_not_nan_pow! {f64, f64}
17181718
// This also should panic on NaN
17191719
macro_rules! impl_not_nan_self_pow {
17201720
($base:ty, $exp:ty) => {
1721-
#[cfg(feature = "std")]
1721+
#[cfg(any(feature = "std", feature = "libm"))]
17221722
impl Pow<NotNan<$exp>> for NotNan<$base> {
17231723
type Output = NotNan<$base>;
17241724
#[inline]
@@ -1727,7 +1727,7 @@ macro_rules! impl_not_nan_self_pow {
17271727
}
17281728
}
17291729

1730-
#[cfg(feature = "std")]
1730+
#[cfg(any(feature = "std", feature = "libm"))]
17311731
impl<'a> Pow<&'a NotNan<$exp>> for NotNan<$base> {
17321732
type Output = NotNan<$base>;
17331733
#[inline]
@@ -1736,7 +1736,7 @@ macro_rules! impl_not_nan_self_pow {
17361736
}
17371737
}
17381738

1739-
#[cfg(feature = "std")]
1739+
#[cfg(any(feature = "std", feature = "libm"))]
17401740
impl<'a> Pow<NotNan<$exp>> for &'a NotNan<$base> {
17411741
type Output = NotNan<$base>;
17421742
#[inline]
@@ -1745,7 +1745,7 @@ macro_rules! impl_not_nan_self_pow {
17451745
}
17461746
}
17471747

1748-
#[cfg(feature = "std")]
1748+
#[cfg(any(feature = "std", feature = "libm"))]
17491749
impl<'a, 'b> Pow<&'a NotNan<$exp>> for &'b NotNan<$base> {
17501750
type Output = NotNan<$base>;
17511751
#[inline]

tests/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate ordered_float;
55

66
pub use num_traits::float::FloatCore;
77
pub use num_traits::{Bounded, FloatConst, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
8-
#[cfg(feature = "std")]
8+
#[cfg(any(feature = "std", feature = "libm"))]
99
pub use num_traits::{Float, Pow};
1010
pub use ordered_float::*;
1111

@@ -788,7 +788,7 @@ fn float_consts_equal_inner() {
788788
test_float_const_methods!(NotNan<f32>);
789789
}
790790

791-
#[cfg(feature = "std")]
791+
#[cfg(any(feature = "std", feature = "libm"))]
792792
macro_rules! test_pow_ord {
793793
($type:ident < $inner:ident >) => {
794794
assert_eq!($type::<$inner>::from(3.0).pow(2i8), OrderedFloat(9.0));
@@ -800,7 +800,7 @@ macro_rules! test_pow_ord {
800800
};
801801
}
802802

803-
#[cfg(feature = "std")]
803+
#[cfg(any(feature = "std", feature = "libm"))]
804804
macro_rules! test_pow_nn {
805805
($type:ident < $inner:ident >) => {
806806
assert_eq!(
@@ -830,7 +830,7 @@ macro_rules! test_pow_nn {
830830
};
831831
}
832832

833-
#[cfg(feature = "std")]
833+
#[cfg(any(feature = "std", feature = "libm"))]
834834
#[test]
835835
fn test_pow_works() {
836836
assert_eq!(OrderedFloat(3.0).pow(OrderedFloat(2.0)), OrderedFloat(9.0));
@@ -850,7 +850,7 @@ fn test_pow_works() {
850850
);
851851
}
852852

853-
#[cfg(feature = "std")]
853+
#[cfg(any(feature = "std", feature = "libm"))]
854854
#[test]
855855
#[should_panic]
856856
fn test_pow_fails_on_nan() {

0 commit comments

Comments
 (0)