From a843027b563339034d2e3c98ecd8f0c38cd6ebd4 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Wed, 31 Jan 2018 15:42:55 -0800 Subject: [PATCH] Re-introduce the std feature This is a port of @vks's rust-num/num#296, but without the feature- toggled changes to `Float`. --- Cargo.toml | 4 ++++ src/bounds.rs | 8 ++++---- src/cast.rs | 13 +++++++------ src/identities.rs | 4 ++-- src/int.rs | 2 +- src/lib.rs | 24 +++++++++++++++--------- src/ops/checked.rs | 2 +- src/ops/wrapping.rs | 4 ++-- src/pow.rs | 2 +- src/sign.rs | 15 +++++++-------- 10 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 06cab57..a320050 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,7 @@ version = "0.1.42" readme = "README.md" [dependencies] + +[features] +default = ["std"] +std = [] diff --git a/src/bounds.rs b/src/bounds.rs index 83fdd0f..191f3e1 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -1,7 +1,7 @@ -use std::{usize, u8, u16, u32, u64}; -use std::{isize, i8, i16, i32, i64}; -use std::{f32, f64}; -use std::num::Wrapping; +use core::{usize, u8, u16, u32, u64}; +use core::{isize, i8, i16, i32, i64}; +use core::{f32, f64}; +use core::num::Wrapping; /// Numbers which have upper and lower bounds pub trait Bounded { diff --git a/src/cast.rs b/src/cast.rs index 2d7fe19..0ef0329 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -1,8 +1,9 @@ -use std::mem::size_of; -use std::num::Wrapping; +use core::mem::size_of; +use core::num::Wrapping; use identities::Zero; use bounds::Bounded; +use float::Float; /// A generic trait for converting a value to a number. pub trait ToPrimitive { @@ -226,8 +227,8 @@ macro_rules! impl_to_primitive_float_to_float { // Make sure the value is in range for the cast. // NaN and +-inf are cast as they are. let n = $slf as f64; - let max_value: $DstT = ::std::$DstT::MAX; - if !n.is_finite() || (-max_value as f64 <= n && n <= max_value as f64) { + let max_value: $DstT = ::core::$DstT::MAX; + if !Float::is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) { Some($slf as $DstT) } else { None @@ -522,8 +523,8 @@ impl_as_primitive!(bool => u8, i8, u16, i16, u32, i32, u64, isize, usize, i64); #[test] fn to_primitive_float() { - use std::f32; - use std::f64; + use core::f32; + use core::f64; let f32_toolarge = 1e39f64; assert_eq!(f32_toolarge.to_f32(), None); diff --git a/src/identities.rs b/src/identities.rs index 79882ed..14f5ac0 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -1,5 +1,5 @@ -use std::ops::{Add, Mul}; -use std::num::Wrapping; +use core::ops::{Add, Mul}; +use core::num::Wrapping; /// Defines an additive identity element for `Self`. pub trait Zero: Sized + Add { diff --git a/src/int.rs b/src/int.rs index 4f9221f..84fedc6 100644 --- a/src/int.rs +++ b/src/int.rs @@ -1,4 +1,4 @@ -use std::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; +use core::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use {Num, NumCast}; use bounds::Bounded; diff --git a/src/lib.rs b/src/lib.rs index 47c0bed..1413cc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,10 +12,16 @@ #![doc(html_root_url = "https://docs.rs/num-traits/0.1")] -use std::ops::{Add, Sub, Mul, Div, Rem}; -use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; -use std::num::Wrapping; -use std::fmt; +#![deny(unconditional_recursion)] + +#![cfg_attr(not(feature = "std"), no_std)] +#[cfg(feature = "std")] +extern crate core; + +use core::ops::{Add, Sub, Mul, Div, Rem}; +use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; +use core::num::Wrapping; +use core::fmt; pub use bounds::Bounded; pub use float::{Float, FloatConst}; @@ -130,10 +136,10 @@ impl NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {} macro_rules! int_trait_impl { ($name:ident for $($t:ty)*) => ($( impl $name for $t { - type FromStrRadixErr = ::std::num::ParseIntError; + type FromStrRadixErr = ::core::num::ParseIntError; #[inline] fn from_str_radix(s: &str, radix: u32) - -> Result + -> Result { <$t>::from_str_radix(s, radix) } @@ -159,7 +165,7 @@ pub enum FloatErrorKind { Empty, Invalid, } -// FIXME: std::num::ParseFloatError is stable in 1.0, but opaque to us, +// FIXME: core::num::ParseFloatError is stable in 1.0, but opaque to us, // so there's not really any way for us to reuse it. #[derive(Debug)] pub struct ParseFloatError { @@ -317,8 +323,8 @@ macro_rules! float_trait_impl { }; match (is_positive, exp) { - (true, Ok(exp)) => base.powi(exp as i32), - (false, Ok(exp)) => 1.0 / base.powi(exp as i32), + (true, Ok(exp)) => Float::powi(base, exp as i32), + (false, Ok(exp)) => 1.0 / Float::powi(base, exp as i32), (_, Err(_)) => return Err(PFE { kind: Invalid }), } }, diff --git a/src/ops/checked.rs b/src/ops/checked.rs index 95214a2..020f649 100644 --- a/src/ops/checked.rs +++ b/src/ops/checked.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, Sub, Mul, Div, Shl, Shr}; +use core::ops::{Add, Sub, Mul, Div, Shl, Shr}; /// Performs addition that returns `None` instead of wrapping around on /// overflow. diff --git a/src/ops/wrapping.rs b/src/ops/wrapping.rs index f989058..24f3405 100644 --- a/src/ops/wrapping.rs +++ b/src/ops/wrapping.rs @@ -1,5 +1,5 @@ -use std::ops::{Add, Sub, Mul}; -use std::num::Wrapping; +use core::ops::{Add, Sub, Mul}; +use core::num::Wrapping; macro_rules! wrapping_impl { ($trait_name:ident, $method:ident, $t:ty) => { diff --git a/src/pow.rs b/src/pow.rs index b250ad4..212a1de 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -1,4 +1,4 @@ -use std::ops::Mul; +use core::ops::Mul; use {One, CheckedMul}; /// Raises a value to the power of exp, using exponentiation by squaring. diff --git a/src/sign.rs b/src/sign.rs index 4b43c89..bf45bd8 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -1,8 +1,8 @@ -use std::ops::Neg; -use std::{f32, f64}; -use std::num::Wrapping; +use core::ops::Neg; +use core::{f32, f64}; +use core::num::Wrapping; -use Num; +use {Num, Float}; /// Useful functions for signed numbers (i.e. numbers that can be negative). pub trait Signed: Sized + Num + Neg { @@ -104,16 +104,15 @@ macro_rules! signed_float_impl { /// Computes the absolute value. Returns `NAN` if the number is `NAN`. #[inline] fn abs(&self) -> $t { - <$t>::abs(*self) + (*self).abs() } /// The positive difference of two numbers. Returns `0.0` if the number is /// less than or equal to `other`, otherwise the difference between`self` /// and `other` is returned. #[inline] - #[allow(deprecated)] fn abs_sub(&self, other: &$t) -> $t { - <$t>::abs_sub(*self, *other) + if *self <= *other { 0. } else { *self - *other } } /// # Returns @@ -123,7 +122,7 @@ macro_rules! signed_float_impl { /// - `NAN` if the number is NaN #[inline] fn signum(&self) -> $t { - <$t>::signum(*self) + Float::signum(*self) } /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`