Skip to content

Commit 7dc11ea

Browse files
committed
Remove std dependency for ToPrimitive
1 parent 5dc4c3b commit 7dc11ea

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

src/lib.rs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@ use core::cmp;
2828
use core::fmt;
2929
use core::fmt::{Binary, Display, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex};
3030
use core::hash::{Hash, Hasher};
31-
#[cfg(feature = "std")]
32-
use core::ops::ShlAssign;
33-
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
31+
use core::ops::{Add, Div, Mul, Neg, Rem, ShlAssign, Sub};
3432
use core::str::FromStr;
3533
#[cfg(feature = "std")]
3634
use std::error::Error;
3735

38-
#[cfg(all(feature = "bigint", feature = "std"))]
39-
use num_bigint::ToBigInt;
4036
#[cfg(feature = "bigint")]
41-
use num_bigint::{BigInt, BigUint, Sign};
37+
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
4238

4339
use num_integer::Integer;
4440
use num_traits::float::FloatCore;
@@ -1365,7 +1361,7 @@ where
13651361
Some(Ratio::new(n1, d1))
13661362
}
13671363

1368-
#[cfg(not(all(feature = "bigint", feature = "std")))]
1364+
#[cfg(not(feature = "bigint"))]
13691365
macro_rules! to_primitive_small {
13701366
($($type_name:ty)*) => ($(
13711367
impl ToPrimitive for Ratio<$type_name> {
@@ -1392,13 +1388,13 @@ macro_rules! to_primitive_small {
13921388
)*)
13931389
}
13941390

1395-
#[cfg(not(all(feature = "bigint", feature = "std")))]
1391+
#[cfg(not(feature = "bigint"))]
13961392
to_primitive_small!(u8 i8 u16 i16 u32 i32);
13971393

1398-
#[cfg(all(target_pointer_width = "32", not(all(feature = "bigint", feature = "std"))))]
1394+
#[cfg(all(target_pointer_width = "32", not(feature = "bigint")))]
13991395
to_primitive_small!(usize isize);
14001396

1401-
#[cfg(all(feature = "std", not(feature = "bigint")))]
1397+
#[cfg(not(feature = "bigint"))]
14021398
macro_rules! to_primitive_64 {
14031399
($($type_name:ty)*) => ($(
14041400
impl ToPrimitive for Ratio<$type_name> {
@@ -1428,13 +1424,13 @@ macro_rules! to_primitive_64 {
14281424
)*)
14291425
}
14301426

1431-
#[cfg(all(feature = "std", not(feature = "bigint")))]
1427+
#[cfg(not(feature = "bigint"))]
14321428
to_primitive_64!(u64 i64);
14331429

1434-
#[cfg(all(target_pointer_width = "64", feature = "std", not(feature = "bigint")))]
1430+
#[cfg(all(target_pointer_width = "64", not(feature = "bigint")))]
14351431
to_primitive_64!(usize isize);
14361432

1437-
#[cfg(all(feature = "bigint", feature = "std"))]
1433+
#[cfg(feature = "bigint")]
14381434
impl<T: Clone + Integer + ToPrimitive + ToBigInt> ToPrimitive for Ratio<T> {
14391435
fn to_i64(&self) -> Option<i64> {
14401436
self.to_integer().to_i64()
@@ -1454,8 +1450,10 @@ impl<T: Clone + Integer + ToPrimitive + ToBigInt> ToPrimitive for Ratio<T> {
14541450

14551451
fn to_f64(&self) -> Option<f64> {
14561452
match (self.numer.to_i64(), self.denom.to_i64()) {
1457-
(Some(numer), Some(denom)) =>
1458-
Some(ratio_to_f64(<i128 as From<_>>::from(numer), <i128 as From<_>>::from(denom))),
1453+
(Some(numer), Some(denom)) => Some(ratio_to_f64(
1454+
<i128 as From<_>>::from(numer),
1455+
<i128 as From<_>>::from(denom),
1456+
)),
14591457
_ => {
14601458
let numer: BigInt = self.numer.to_bigint()?;
14611459
let denom: BigInt = self.denom.to_bigint()?;
@@ -1465,19 +1463,17 @@ impl<T: Clone + Integer + ToPrimitive + ToBigInt> ToPrimitive for Ratio<T> {
14651463
}
14661464
}
14671465

1468-
#[cfg(feature = "std")]
14691466
trait Bits {
14701467
fn bits(&self) -> usize;
14711468
}
14721469

1473-
#[cfg(all(feature = "bigint", feature = "std"))]
1470+
#[cfg(feature = "bigint")]
14741471
impl Bits for BigInt {
14751472
fn bits(&self) -> usize {
14761473
self.bits()
14771474
}
14781475
}
14791476

1480-
#[cfg(feature = "std")]
14811477
impl Bits for i128 {
14821478
fn bits(&self) -> usize {
14831479
(128 - self.wrapping_abs().leading_zeros()) as usize
@@ -1488,19 +1484,18 @@ impl Bits for i128 {
14881484
///
14891485
/// In addition to stated trait bounds, `T` must be able to hold numbers 56 bits larger than
14901486
/// the largest of `numer` and `denom`. This is automatically true if `T` is `BigInt`.
1491-
#[cfg(feature = "std")]
14921487
fn ratio_to_f64<T: Bits + Clone + Integer + Signed + ShlAssign<usize> + ToPrimitive>(
14931488
numer: T,
14941489
denom: T,
14951490
) -> f64 {
14961491
assert_eq!(
1497-
std::f64::RADIX,
1492+
core::f64::RADIX,
14981493
2,
14991494
"only floating point implementations with radix 2 are supported"
15001495
);
15011496

15021497
// Inclusive upper and lower bounds to the range of exactly-representable ints in an f64.
1503-
const MAX_EXACT_INT: i64 = 1i64 << std::f64::MANTISSA_DIGITS;
1498+
const MAX_EXACT_INT: i64 = 1i64 << core::f64::MANTISSA_DIGITS;
15041499
const MIN_EXACT_INT: i64 = -MAX_EXACT_INT;
15051500

15061501
let flo_sign = numer.signum().to_f64().unwrap() * denom.signum().to_f64().unwrap();
@@ -1531,11 +1526,11 @@ fn ratio_to_f64<T: Bits + Clone + Integer + Signed + ShlAssign<usize> + ToPrimit
15311526

15321527
// Filter out overflows and underflows. After this step, the signed difference fits in an
15331528
// isize.
1534-
if is_diff_positive && absolute_diff > std::f64::MAX_EXP as usize {
1535-
return std::f64::INFINITY * flo_sign;
1529+
if is_diff_positive && absolute_diff > core::f64::MAX_EXP as usize {
1530+
return core::f64::INFINITY * flo_sign;
15361531
}
15371532
if !is_diff_positive
1538-
&& absolute_diff > -std::f64::MIN_EXP as usize + std::f64::MANTISSA_DIGITS as usize + 1
1533+
&& absolute_diff > -core::f64::MIN_EXP as usize + core::f64::MANTISSA_DIGITS as usize + 1
15391534
{
15401535
return 0.0 * flo_sign;
15411536
}
@@ -1548,7 +1543,7 @@ fn ratio_to_f64<T: Bits + Clone + Integer + Signed + ShlAssign<usize> + ToPrimit
15481543
// Shift is chosen so that the quotient will have 55 or 56 bits. The exception is if the
15491544
// quotient is going to be subnormal, in which case it may have fewer bits.
15501545
let shift: isize =
1551-
std::cmp::max(diff, std::f64::MIN_EXP as isize) - std::f64::MANTISSA_DIGITS as isize - 2;
1546+
diff.max(core::f64::MIN_EXP as isize) - core::f64::MANTISSA_DIGITS as isize - 2;
15521547
if shift >= 0 {
15531548
denom <<= shift as usize
15541549
} else {
@@ -1561,8 +1556,8 @@ fn ratio_to_f64<T: Bits + Clone + Integer + Signed + ShlAssign<usize> + ToPrimit
15611556
let mut quotient = quotient.to_u64().unwrap();
15621557
let n_rounding_bits = {
15631558
let quotient_bits = 64 - quotient.leading_zeros() as isize;
1564-
let subnormal_bits = std::f64::MIN_EXP as isize - shift;
1565-
std::cmp::max(quotient_bits, subnormal_bits) - std::f64::MANTISSA_DIGITS as isize
1559+
let subnormal_bits = core::f64::MIN_EXP as isize - shift;
1560+
quotient_bits.max(subnormal_bits) - core::f64::MANTISSA_DIGITS as isize
15661561
} as usize;
15671562
debug_assert!(n_rounding_bits == 2 || n_rounding_bits == 3);
15681563
let rounding_bit_mask = (1u64 << n_rounding_bits) - 1;
@@ -1580,7 +1575,7 @@ fn ratio_to_f64<T: Bits + Clone + Integer + Signed + ShlAssign<usize> + ToPrimit
15801575
// The quotient is guaranteed to be exactly representable as it's now 53 bits + 2 or 3
15811576
// trailing zeros, so there is no risk of a rounding error here.
15821577
let q_float = quotient as f64;
1583-
q_float * f64::exp2(shift as f64) * flo_sign
1578+
q_float * 2f64.powi(shift as i32) * flo_sign
15841579
}
15851580

15861581
#[cfg(test)]
@@ -1595,7 +1590,7 @@ fn hash<T: Hash>(x: &T) -> u64 {
15951590

15961591
#[cfg(test)]
15971592
mod test {
1598-
#[cfg(all(feature = "bigint", feature = "std"))]
1593+
#[cfg(all(feature = "bigint"))]
15991594
use super::BigInt;
16001595
#[cfg(feature = "bigint")]
16011596
use super::BigRational;
@@ -1606,7 +1601,6 @@ mod test {
16061601
use core::isize;
16071602
use core::str::FromStr;
16081603
use num_integer::Integer;
1609-
#[cfg(feature = "std")]
16101604
use num_traits::ToPrimitive;
16111605
use num_traits::{FromPrimitive, One, Pow, Signed, Zero};
16121606

@@ -2869,15 +2863,14 @@ mod test {
28692863
}
28702864

28712865
#[test]
2872-
#[cfg(feature = "std")]
28732866
fn test_ratio_to_i64() {
28742867
assert_eq!(5, Rational64::new(70, 14).to_u64().unwrap());
28752868
assert_eq!(-3, Rational64::new(-31, 8).to_i64().unwrap());
28762869
assert_eq!(None, Rational64::new(-31, 8).to_u64());
28772870
}
28782871

28792872
#[test]
2880-
#[cfg(all(feature = "bigint", feature = "std"))]
2873+
#[cfg(feature = "bigint")]
28812874
fn test_ratio_to_i128() {
28822875
assert_eq!(
28832876
1i128 << 70,
@@ -2888,7 +2881,7 @@ mod test {
28882881
}
28892882

28902883
#[test]
2891-
#[cfg(all(feature = "bigint", feature = "std"))]
2884+
#[cfg(feature = "bigint")]
28922885
fn test_big_ratio_to_f64() {
28932886
assert_eq!(
28942887
BigRational::new(
@@ -2919,7 +2912,6 @@ mod test {
29192912
}
29202913

29212914
#[test]
2922-
#[cfg(all(feature = "std"))]
29232915
fn test_ratio_to_f64() {
29242916
assert_eq!(0.5f64, Ratio::<u8>::new(1, 2).to_f64().unwrap());
29252917
assert_eq!(0.5f64, Rational64::new(1, 2).to_f64().unwrap());

0 commit comments

Comments
 (0)