Skip to content

Commit fa35a35

Browse files
authored
Merge pull request #861 from xd009642/float_core
Place NdFloat and num-traits/std behind a new "std" feature
2 parents cb2dedb + 1b4a1ec commit fa35a35

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test = true
2929

3030
[dependencies]
3131
num-integer = "0.1.39"
32-
num-traits = "0.2"
32+
num-traits = { version = "0.2", default-features = false }
3333
num-complex = { version = "0.3", default-features = false }
3434

3535
rayon = { version = "1.0.3", optional = true }
@@ -51,6 +51,7 @@ approx = "0.4"
5151
itertools = { version = "0.9.0", default-features = false, features = ["use_std"] }
5252

5353
[features]
54+
default = ["std"]
5455
# Enable blas usage
5556
# See README for more instructions
5657
blas = ["cblas-sys", "blas-src"]
@@ -65,6 +66,8 @@ test = ["test-blas-openblas-sys"]
6566
# This feature is used for docs
6667
docs = ["approx", "serde", "rayon"]
6768

69+
std = ["num-traits/std"]
70+
6871
[profile.release]
6972
[profile.bench]
7073
debug = true

src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#![allow(
1111
clippy::many_single_char_names,
1212
clippy::deref_addrof,
13-
clippy::unreadable_literal,
13+
clippy::unreadable_literal
1414
)]
1515

1616
//! The `ndarray` crate provides an *n*-dimensional container for general elements
@@ -106,7 +106,7 @@
106106
//!
107107
//! If you are looking to generate random arrays instead, check out [`ndarray-rand`](https://crates.io/crates/ndarray-rand).
108108
//!
109-
//! For conversion between `ndarray`, [`nalgebra`](https://crates.io/crates/nalgebra) and
109+
//! For conversion between `ndarray`, [`nalgebra`](https://crates.io/crates/nalgebra) and
110110
//! [`image`](https://crates.io/crates/image) check out [`nshare`](https://crates.io/crates/nshare).
111111
112112
#[cfg(feature = "blas")]
@@ -133,12 +133,14 @@ use crate::iterators::Baseiter;
133133
use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut, Lanes, LanesMut};
134134

135135
pub use crate::arraytraits::AsArray;
136-
pub use crate::linalg_traits::{LinalgScalar, NdFloat};
136+
#[cfg(feature = "std")]
137+
pub use crate::linalg_traits::NdFloat;
138+
pub use crate::linalg_traits::LinalgScalar;
137139

138140
pub use crate::stacking::{concatenate, stack, stack_new_axis};
139141

140142
pub use crate::impl_views::IndexLonger;
141-
pub use crate::shape_builder::{Shape, StrideShape, ShapeBuilder};
143+
pub use crate::shape_builder::{Shape, ShapeBuilder, StrideShape};
142144

143145
#[macro_use]
144146
mod macro_utils;
@@ -147,16 +149,16 @@ mod private;
147149
mod aliases;
148150
#[macro_use]
149151
mod itertools;
152+
mod argument_traits;
150153
#[cfg(feature = "approx")]
151154
mod array_approx;
152155
#[cfg(feature = "serde")]
153156
mod array_serde;
154157
mod arrayformat;
155158
mod arraytraits;
156-
mod argument_traits;
157159
pub use crate::argument_traits::AssignElem;
158-
mod data_traits;
159160
mod data_repr;
161+
mod data_traits;
160162

161163
pub use crate::aliases::*;
162164

@@ -1599,4 +1601,3 @@ mod impl_cow;
15991601
pub(crate) fn is_aligned<T>(ptr: *const T) -> bool {
16001602
(ptr as usize) % ::std::mem::align_of::<T>() == 0
16011603
}
1602-

src/linalg_traits.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88
use crate::ScalarOperand;
9-
use num_traits::{Float, One, Zero};
9+
#[cfg(feature = "std")]
10+
use num_traits::Float;
11+
use num_traits::{One, Zero};
1012
use std::fmt;
1113
use std::ops::{Add, Div, Mul, Sub};
1214
use std::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
@@ -47,6 +49,7 @@ impl<T> LinalgScalar for T where
4749
/// operations (`ScalarOperand`).
4850
///
4951
/// This trait can only be implemented by `f32` and `f64`.
52+
#[cfg(feature = "std")]
5053
pub trait NdFloat:
5154
Float
5255
+ AddAssign
@@ -65,5 +68,8 @@ pub trait NdFloat:
6568
{
6669
}
6770

71+
#[cfg(feature = "std")]
6872
impl NdFloat for f32 {}
73+
#[cfg(feature = "std")]
6974
impl NdFloat for f64 {}
75+

src/prelude.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ pub use crate::{array, azip, s};
5151
pub use crate::ShapeBuilder;
5252

5353
#[doc(no_inline)]
54-
pub use crate::{AsArray, NdFloat};
54+
pub use crate::AsArray;
55+
56+
#[doc(no_inline)]
57+
#[cfg(feature = "std")]
58+
pub use crate::NdFloat;

tests/oper.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use ndarray::prelude::*;
1010
use ndarray::{rcarr1, rcarr2};
1111
use ndarray::{Data, LinalgScalar};
1212
use ndarray::{Ix, Ixs};
13+
use num_traits::Zero;
1314
use std::iter::FromIterator;
1415

1516
use approx::assert_abs_diff_eq;
@@ -33,10 +34,9 @@ fn test_oper(op: &str, a: &[f32], b: &[f32], c: &[f32]) {
3334
test_oper_arr::<f32, _>(op, aa.clone(), bb.clone(), cc.clone());
3435
}
3536

36-
fn test_oper_arr<A, D>(op: &str, mut aa: ArcArray<A, D>, bb: ArcArray<A, D>, cc: ArcArray<A, D>)
37+
38+
fn test_oper_arr<A, D>(op: &str, mut aa: ArcArray<f32, D>, bb: ArcArray<f32, D>, cc: ArcArray<f32, D>)
3739
where
38-
A: NdFloat,
39-
for<'a> &'a A: Neg<Output = A>,
4040
D: Dimension,
4141
{
4242
match op {
@@ -147,17 +147,16 @@ fn scalar_operations() {
147147
}
148148
}
149149

150-
fn reference_dot<'a, A, V1, V2>(a: V1, b: V2) -> A
150+
fn reference_dot<'a, V1, V2>(a: V1, b: V2) -> f32
151151
where
152-
A: NdFloat,
153-
V1: AsArray<'a, A>,
154-
V2: AsArray<'a, A>,
152+
V1: AsArray<'a, f32>,
153+
V2: AsArray<'a, f32>,
155154
{
156155
let a = a.into();
157156
let b = b.into();
158157
a.iter()
159158
.zip(b.iter())
160-
.fold(A::zero(), |acc, (&x, &y)| acc + x * y)
159+
.fold(f32::zero(), |acc, (&x, &y)| acc + x * y)
161160
}
162161

163162
#[test]

0 commit comments

Comments
 (0)