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 #216.
  • Loading branch information
vks committed May 31, 2017
1 parent ef752e4 commit 351dfc6
Show file tree
Hide file tree
Showing 11 changed files with 287 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
13 changes: 7 additions & 6 deletions traits/src/cast.rs
Original file line number Diff line number Diff line change
@@ -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::BasicFloat;

/// A generic trait for converting a value to a number.
pub trait ToPrimitive {
Expand Down Expand Up @@ -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 !BasicFloat::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 +455,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 351dfc6

Please sign in to comment.