Skip to content

Commit

Permalink
traits: Introduce std feature
Browse files Browse the repository at this point in the history
This makes it possible to build `traits` without `std`. For this a new
trait `BasicFloat` was introduced, implementing some basic functionality
that works with `core`. Most notably this is lacking functions like
`cos`, `sin`, etc.

`Float` is not available without `std`.

Refs rust-num#216.
  • Loading branch information
vks committed May 31, 2017
1 parent ef752e4 commit cdc71ba
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 69 deletions.
4 changes: 4 additions & 0 deletions traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ name = "num-traits"
version = "0.1.37"

[dependencies]

[features]
default = ["std"]
std = []
8 changes: 4 additions & 4 deletions traits/src/bounds.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
32 changes: 26 additions & 6 deletions traits/src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::mem::size_of;
use std::num::Wrapping;
use core::mem::size_of;
use core::num::Wrapping;

use identities::Zero;
use bounds::Bounded;
Expand Down Expand Up @@ -218,6 +218,26 @@ impl_to_primitive_uint!(u16);
impl_to_primitive_uint!(u32);
impl_to_primitive_uint!(u64);

// Those have to be reimplemented, because `core::num::Float` is not stable.

/// Returns `true` if the number is NaN.
#[inline]
fn is_nan(x: f64) -> bool {
x != x
}

/// Returns `true` if the number is infinite.
#[inline]
fn is_infinite(x: f64) -> bool {
x == ::core::f64::INFINITY || x == ::core::f64::NEG_INFINITY
}

/// Returns `true` if the number is neither infinite or NaN.
#[inline]
fn is_finite(x: f64) -> bool {
!(is_nan(x) || is_infinite(x))
}

macro_rules! impl_to_primitive_float_to_float {
($SrcT:ident, $DstT:ident, $slf:expr) => (
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Expand All @@ -226,8 +246,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 !is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) {
Some($slf as $DstT)
} else {
None
Expand Down Expand Up @@ -454,8 +474,8 @@ impl<T: NumCast> NumCast for Wrapping<T> {

#[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);
Expand Down
Loading

0 comments on commit cdc71ba

Please sign in to comment.