Skip to content

Commit

Permalink
Refactor: only import first module under std/core
Browse files Browse the repository at this point in the history
And let the lib user choose between using the full path or not
  • Loading branch information
bigherc18 committed Feb 25, 2023
1 parent b1e19ab commit d614df0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
37 changes: 25 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ include!("./without_std.rs");
#[cfg(all(not(feature = "std"), feature = "alloc"))]
include!("./with_alloc.rs");

use crate::cmp::Ordering;
use crate::convert::TryFrom;
use crate::default::Default;
use crate::hash::{Hash, Hasher};
use crate::num::{ParseFloatError, ParseIntError};
use crate::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
use crate::iter::Sum;
use crate::str::FromStr;
use crate::string::{String, ToString};
use crate::f64::consts::PI;
use crate::f32::consts::PI as PI_f32;
use crate::f64::DIGITS;
use crate::f32::DIGITS as DIGITS_f32;
use crate::i8::{MAX, MIN};

use num_bigint::{BigInt, ParseBigIntError, Sign, ToBigInt};
use num_integer::Integer as IntegerTrait;
pub use num_traits::{FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
Expand Down Expand Up @@ -701,6 +716,7 @@ impl fmt::Display for ParseBigDecimalError {
}
}

// TODO: should we implement Error for no_std environments ?
#[cfg(feature = "std")]
impl std::error::Error for ParseBigDecimalError {
fn description(&self) -> &str {
Expand Down Expand Up @@ -1025,7 +1041,7 @@ impl Sub<BigDecimal> for BigDecimal {
#[inline]
fn sub(self, rhs: BigDecimal) -> BigDecimal {
let mut lhs = self;
let scale = core::cmp::max(lhs.scale, rhs.scale);
let scale = crate::cmp::max(lhs.scale, rhs.scale);

match lhs.scale.cmp(&rhs.scale) {
Ordering::Equal => {
Expand All @@ -1044,7 +1060,7 @@ impl<'a> Sub<&'a BigDecimal> for BigDecimal {
#[inline]
fn sub(self, rhs: &BigDecimal) -> BigDecimal {
let mut lhs = self;
let scale = core::cmp::max(lhs.scale, rhs.scale);
let scale = crate::cmp::max(lhs.scale, rhs.scale);

match lhs.scale.cmp(&rhs.scale) {
Ordering::Equal => {
Expand Down Expand Up @@ -1420,7 +1436,7 @@ impl Rem<BigDecimal> for BigDecimal {

#[inline]
fn rem(self, other: BigDecimal) -> BigDecimal {
let scale = core::cmp::max(self.scale, other.scale);
let scale = crate::cmp::max(self.scale, other.scale);

let num = self.take_and_scale(scale).int_val;
let den = other.take_and_scale(scale).int_val;
Expand All @@ -1434,7 +1450,7 @@ impl<'a> Rem<&'a BigDecimal> for BigDecimal {

#[inline]
fn rem(self, other: &BigDecimal) -> BigDecimal {
let scale = core::cmp::max(self.scale, other.scale);
let scale = crate::cmp::max(self.scale, other.scale);
let num = self.take_and_scale(scale).int_val;
let den = &other.int_val;

Expand All @@ -1451,7 +1467,7 @@ impl<'a> Rem<BigDecimal> for &'a BigDecimal {

#[inline]
fn rem(self, other: BigDecimal) -> BigDecimal {
let scale = core::cmp::max(self.scale, other.scale);
let scale = crate::cmp::max(self.scale, other.scale);
let num = &self.int_val;
let den = other.take_and_scale(scale).int_val;

Expand All @@ -1471,7 +1487,7 @@ impl<'a, 'b> Rem<&'b BigDecimal> for &'a BigDecimal {

#[inline]
fn rem(self, other: &BigDecimal) -> BigDecimal {
let scale = core::cmp::max(self.scale, other.scale);
let scale = crate::cmp::max(self.scale, other.scale);
let num = &self.int_val;
let den = &other.int_val;

Expand Down Expand Up @@ -1992,13 +2008,10 @@ mod bigdecimal_serde {
#[rustfmt::skip]
#[cfg(test)]
mod bigdecimal_tests {

use crate::BigDecimal;
use crate::{BigDecimal, FromStr, TryFrom};
use num_traits::{ToPrimitive, FromPrimitive, Signed, Zero, One};
use num_bigint;

use crate::{FromStr, TryFrom};

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use crate::{vec, format, ToString};

Expand Down Expand Up @@ -2135,8 +2148,8 @@ mod bigdecimal_tests {

#[test]
fn test_nan_float() {
assert!(BigDecimal::try_from(core::f32::NAN).is_err());
assert!(BigDecimal::try_from(core::f64::NAN).is_err());
assert!(BigDecimal::try_from(crate::f32::NAN).is_err());
assert!(BigDecimal::try_from(crate::f64::NAN).is_err());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/with_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
extern crate alloc;

use alloc::{format, string::String, string::ToString, vec};
use alloc::{format, string, vec};
19 changes: 2 additions & 17 deletions src/with_std.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::default::Default;
use std::error::Error;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::num::{ParseFloatError, ParseIntError};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
use std::iter::Sum;
use std::str::{self, FromStr};
use std::collections::hash_map::DefaultHasher;

use std::f64::consts::PI;
use std::f32::consts::PI as PI_f32;
use std::f64::DIGITS;
use std::f32::DIGITS as DIGITS_f32;
use std::{cmp, convert, default, fmt, hash, num, ops, iter, str, string, i8, f32, f64};

use std::i8::{MAX, MIN};
use std::collections::hash_map::DefaultHasher;
20 changes: 1 addition & 19 deletions src/without_std.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
use core::cmp::Ordering;
use core::convert::TryFrom;
use core::default::Default;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::num::{ParseFloatError, ParseIntError};
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
use core::iter::Sum;
use core::str::{self, FromStr};
use core::{cmp, convert, default, fmt, hash, num, ops, iter, str, i8, f32, f64};

#[cfg(test)]
use siphasher::sip::SipHasher as DefaultHasher;

// TODO: should we replace Error for no_std environments
// use core::error::Error;

use num_traits::float::FloatCore;

use core::f64::consts::PI;
use core::f32::consts::PI as PI_f32;
use core::f64::DIGITS;
use core::f32::DIGITS as DIGITS_f32;

use core::i8::{MAX, MIN};

0 comments on commit d614df0

Please sign in to comment.