Skip to content

Commit 8b5d4ac

Browse files
committed
Auto merge of #296 - vks:no_std, r=cuviper
traits: Introduce std feature 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.
2 parents ef752e4 + ba73ba2 commit 8b5d4ac

File tree

12 files changed

+344
-80
lines changed

12 files changed

+344
-80
lines changed

ci/test_full.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ for package in bigint complex integer iter rational traits; do
1010
cargo test --manifest-path $package/Cargo.toml
1111
done
1212

13+
# Only num-traits supports no_std at the moment.
14+
cargo build --manifest-path traits/Cargo.toml --no-default-features
15+
cargo test --manifest-path traits/Cargo.toml --no-default-features
16+
1317
# Each isolated feature should also work everywhere.
1418
for feature in '' bigint rational complex; do
1519
cargo build --verbose --no-default-features --features="$feature"

traits/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ name = "num-traits"
1010
version = "0.1.37"
1111

1212
[dependencies]
13+
14+
[features]
15+
default = ["std"]
16+
std = []

traits/src/bounds.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{usize, u8, u16, u32, u64};
2-
use std::{isize, i8, i16, i32, i64};
3-
use std::{f32, f64};
4-
use std::num::Wrapping;
1+
use core::{usize, u8, u16, u32, u64};
2+
use core::{isize, i8, i16, i32, i64};
3+
use core::{f32, f64};
4+
use core::num::Wrapping;
55

66
/// Numbers which have upper and lower bounds
77
pub trait Bounded {

traits/src/cast.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::mem::size_of;
2-
use std::num::Wrapping;
1+
use core::mem::size_of;
2+
use core::num::Wrapping;
33

44
use identities::Zero;
55
use bounds::Bounded;
6+
use float::Float;
67

78
/// A generic trait for converting a value to a number.
89
pub trait ToPrimitive {
@@ -226,8 +227,8 @@ macro_rules! impl_to_primitive_float_to_float {
226227
// Make sure the value is in range for the cast.
227228
// NaN and +-inf are cast as they are.
228229
let n = $slf as f64;
229-
let max_value: $DstT = ::std::$DstT::MAX;
230-
if !n.is_finite() || (-max_value as f64 <= n && n <= max_value as f64) {
230+
let max_value: $DstT = ::core::$DstT::MAX;
231+
if !Float::is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) {
231232
Some($slf as $DstT)
232233
} else {
233234
None
@@ -454,8 +455,8 @@ impl<T: NumCast> NumCast for Wrapping<T> {
454455

455456
#[test]
456457
fn to_primitive_float() {
457-
use std::f32;
458-
use std::f64;
458+
use core::f32;
459+
use core::f64;
459460

460461
let f32_toolarge = 1e39f64;
461462
assert_eq!(f32_toolarge.to_f32(), None);

0 commit comments

Comments
 (0)