Re-introduce the std feature
This is a port of @vks's rust-num/num#296, but without the feature- toggled changes to `Float`.
This commit is contained in:
parent
3716330128
commit
a843027b56
|
@ -12,3 +12,7 @@ version = "0.1.42"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = []
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{usize, u8, u16, u32, u64};
|
use core::{usize, u8, u16, u32, u64};
|
||||||
use std::{isize, i8, i16, i32, i64};
|
use core::{isize, i8, i16, i32, i64};
|
||||||
use std::{f32, f64};
|
use core::{f32, f64};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
/// Numbers which have upper and lower bounds
|
/// Numbers which have upper and lower bounds
|
||||||
pub trait Bounded {
|
pub trait Bounded {
|
||||||
|
|
13
src/cast.rs
13
src/cast.rs
|
@ -1,8 +1,9 @@
|
||||||
use std::mem::size_of;
|
use core::mem::size_of;
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
use identities::Zero;
|
use identities::Zero;
|
||||||
use bounds::Bounded;
|
use bounds::Bounded;
|
||||||
|
use float::Float;
|
||||||
|
|
||||||
/// A generic trait for converting a value to a number.
|
/// A generic trait for converting a value to a number.
|
||||||
pub trait ToPrimitive {
|
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.
|
// Make sure the value is in range for the cast.
|
||||||
// NaN and +-inf are cast as they are.
|
// NaN and +-inf are cast as they are.
|
||||||
let n = $slf as f64;
|
let n = $slf as f64;
|
||||||
let max_value: $DstT = ::std::$DstT::MAX;
|
let max_value: $DstT = ::core::$DstT::MAX;
|
||||||
if !n.is_finite() || (-max_value as f64 <= n && n <= max_value as f64) {
|
if !Float::is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) {
|
||||||
Some($slf as $DstT)
|
Some($slf as $DstT)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -522,8 +523,8 @@ impl_as_primitive!(bool => u8, i8, u16, i16, u32, i32, u64, isize, usize, i64);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn to_primitive_float() {
|
fn to_primitive_float() {
|
||||||
use std::f32;
|
use core::f32;
|
||||||
use std::f64;
|
use core::f64;
|
||||||
|
|
||||||
let f32_toolarge = 1e39f64;
|
let f32_toolarge = 1e39f64;
|
||||||
assert_eq!(f32_toolarge.to_f32(), None);
|
assert_eq!(f32_toolarge.to_f32(), None);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::ops::{Add, Mul};
|
use core::ops::{Add, Mul};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
/// Defines an additive identity element for `Self`.
|
/// Defines an additive identity element for `Self`.
|
||||||
pub trait Zero: Sized + Add<Self, Output = Self> {
|
pub trait Zero: Sized + Add<Self, Output = Self> {
|
||||||
|
|
|
@ -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 {Num, NumCast};
|
||||||
use bounds::Bounded;
|
use bounds::Bounded;
|
||||||
|
|
24
src/lib.rs
24
src/lib.rs
|
@ -12,10 +12,16 @@
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/num-traits/0.1")]
|
#![doc(html_root_url = "https://docs.rs/num-traits/0.1")]
|
||||||
|
|
||||||
use std::ops::{Add, Sub, Mul, Div, Rem};
|
#![deny(unconditional_recursion)]
|
||||||
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
|
||||||
use std::num::Wrapping;
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
use std::fmt;
|
#[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 bounds::Bounded;
|
||||||
pub use float::{Float, FloatConst};
|
pub use float::{Float, FloatConst};
|
||||||
|
@ -130,10 +136,10 @@ impl<T> NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {}
|
||||||
macro_rules! int_trait_impl {
|
macro_rules! int_trait_impl {
|
||||||
($name:ident for $($t:ty)*) => ($(
|
($name:ident for $($t:ty)*) => ($(
|
||||||
impl $name for $t {
|
impl $name for $t {
|
||||||
type FromStrRadixErr = ::std::num::ParseIntError;
|
type FromStrRadixErr = ::core::num::ParseIntError;
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str_radix(s: &str, radix: u32)
|
fn from_str_radix(s: &str, radix: u32)
|
||||||
-> Result<Self, ::std::num::ParseIntError>
|
-> Result<Self, ::core::num::ParseIntError>
|
||||||
{
|
{
|
||||||
<$t>::from_str_radix(s, radix)
|
<$t>::from_str_radix(s, radix)
|
||||||
}
|
}
|
||||||
|
@ -159,7 +165,7 @@ pub enum FloatErrorKind {
|
||||||
Empty,
|
Empty,
|
||||||
Invalid,
|
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.
|
// so there's not really any way for us to reuse it.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParseFloatError {
|
pub struct ParseFloatError {
|
||||||
|
@ -317,8 +323,8 @@ macro_rules! float_trait_impl {
|
||||||
};
|
};
|
||||||
|
|
||||||
match (is_positive, exp) {
|
match (is_positive, exp) {
|
||||||
(true, Ok(exp)) => base.powi(exp as i32),
|
(true, Ok(exp)) => Float::powi(base, exp as i32),
|
||||||
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
|
(false, Ok(exp)) => 1.0 / Float::powi(base, exp as i32),
|
||||||
(_, Err(_)) => return Err(PFE { kind: Invalid }),
|
(_, Err(_)) => return Err(PFE { kind: Invalid }),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -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
|
/// Performs addition that returns `None` instead of wrapping around on
|
||||||
/// overflow.
|
/// overflow.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::ops::{Add, Sub, Mul};
|
use core::ops::{Add, Sub, Mul};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
macro_rules! wrapping_impl {
|
macro_rules! wrapping_impl {
|
||||||
($trait_name:ident, $method:ident, $t:ty) => {
|
($trait_name:ident, $method:ident, $t:ty) => {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::ops::Mul;
|
use core::ops::Mul;
|
||||||
use {One, CheckedMul};
|
use {One, CheckedMul};
|
||||||
|
|
||||||
/// Raises a value to the power of exp, using exponentiation by squaring.
|
/// Raises a value to the power of exp, using exponentiation by squaring.
|
||||||
|
|
15
src/sign.rs
15
src/sign.rs
|
@ -1,8 +1,8 @@
|
||||||
use std::ops::Neg;
|
use core::ops::Neg;
|
||||||
use std::{f32, f64};
|
use core::{f32, f64};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
use Num;
|
use {Num, Float};
|
||||||
|
|
||||||
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
||||||
pub trait Signed: Sized + Num + Neg<Output = Self> {
|
pub trait Signed: Sized + Num + Neg<Output = Self> {
|
||||||
|
@ -104,16 +104,15 @@ macro_rules! signed_float_impl {
|
||||||
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
|
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn abs(&self) -> $t {
|
fn abs(&self) -> $t {
|
||||||
<$t>::abs(*self)
|
(*self).abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The positive difference of two numbers. Returns `0.0` if the number is
|
/// The positive difference of two numbers. Returns `0.0` if the number is
|
||||||
/// less than or equal to `other`, otherwise the difference between`self`
|
/// less than or equal to `other`, otherwise the difference between`self`
|
||||||
/// and `other` is returned.
|
/// and `other` is returned.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(deprecated)]
|
|
||||||
fn abs_sub(&self, other: &$t) -> $t {
|
fn abs_sub(&self, other: &$t) -> $t {
|
||||||
<$t>::abs_sub(*self, *other)
|
if *self <= *other { 0. } else { *self - *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Returns
|
/// # Returns
|
||||||
|
@ -123,7 +122,7 @@ macro_rules! signed_float_impl {
|
||||||
/// - `NAN` if the number is NaN
|
/// - `NAN` if the number is NaN
|
||||||
#[inline]
|
#[inline]
|
||||||
fn signum(&self) -> $t {
|
fn signum(&self) -> $t {
|
||||||
<$t>::signum(*self)
|
Float::signum(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
||||||
|
|
Loading…
Reference in New Issue