Skip to content

Commit d27b1a9

Browse files
authored
No_std support for ndarray (#864)
This pr enables ndarray to be used in the no_std environment, and maintains most of the functions. Fixes #708. The `geomspace` `linspace` `logspace` `range` `var_axis` and `std_axis` methods are only available when `std` is enabled at current. And 'blas''rayon''serde' features are not available without std too.
1 parent 9758af7 commit d27b1a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+142
-29
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ bench = false
2828
test = true
2929

3030
[dependencies]
31-
num-integer = "0.1.39"
31+
num-integer = { version = "0.1.39", default-features = false }
3232
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 }
3636

37-
approx = { version = "0.4", optional = true }
37+
approx = { version = "0.4", optional = true , default-features = false }
3838

3939
# Use via the `blas` crate feature!
4040
cblas-sys = { version = "0.1.4", optional = true, default-features = false }
4141
blas-src = { version = "0.6.1", optional = true, default-features = false }
4242

43-
matrixmultiply = { version = "0.2.0" }
43+
matrixmultiply = { version = "0.2.0", default-features = false}
4444
serde = { version = "1.0", optional = true }
4545
rawpointer = { version = "0.2" }
4646

@@ -52,6 +52,7 @@ itertools = { version = "0.9.0", default-features = false, features = ["use_std"
5252

5353
[features]
5454
default = ["std"]
55+
5556
# Enable blas usage
5657
# See README for more instructions
5758
blas = ["cblas-sys", "blas-src"]
@@ -66,7 +67,7 @@ test = ["test-blas-openblas-sys"]
6667
# This feature is used for docs
6768
docs = ["approx", "serde", "rayon"]
6869

69-
std = ["num-traits/std"]
70+
std = ["num-traits/std", "matrixmultiply/std"]
7071

7172
[profile.release]
7273
[profile.bench]

README.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ Crate Feature Flags
4848
The following crate feature flags are available. They are configured in
4949
your `Cargo.toml`.
5050

51+
- ``std``
52+
53+
- Rust Standard Library
54+
55+
- This crate can be used without the standard library by disabling the
56+
default `std` feature. To do so, use this in your `Cargo.toml`:
57+
58+
[dependencies]
59+
ndarray = { version = "0.x.y", default-features = false }
60+
61+
- The `geomspace` `linspace` `logspace` `range` `std` `var` `var_axis` and `std_axis`
62+
methods are only available when `std` is enabled.
63+
5164
- ``serde``
5265

5366
- Optional, compatible with Rust stable
@@ -112,4 +125,3 @@ http://opensource.org/licenses/MIT, at your
112125
option. This file may not be copied, modified, or distributed
113126
except according to those terms.
114127

115-

examples/convo.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused)]
22
extern crate ndarray;
3-
extern crate num_traits;
43

4+
#[cfg(feature = "std")]
55
use num_traits::Float;
66

77
use ndarray::prelude::*;
@@ -13,6 +13,7 @@ const SHARPEN: [[f32; 3]; 3] = [[0., -1., 0.], [-1., 5., -1.], [0., -1., 0.]];
1313
type Kernel3x3<A> = [[A; 3]; 3];
1414

1515
#[inline(never)]
16+
#[cfg(feature = "std")]
1617
fn conv_3x3<F>(a: &ArrayView2<'_, F>, out: &mut ArrayViewMut2<'_, F>, kernel: &Kernel3x3<F>)
1718
where
1819
F: Float,
@@ -41,6 +42,7 @@ where
4142
}
4243
}
4344

45+
#[cfg(feature = "std")]
4446
fn main() {
4547
let n = 16;
4648
let mut a = Array::zeros((n, n));
@@ -61,3 +63,5 @@ fn main() {
6163
}
6264
println!("{:2}", res);
6365
}
66+
#[cfg(not(feature = "std"))]
67+
fn main() {}

examples/sort-axis.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ where
129129
}
130130
}
131131
}
132-
132+
#[cfg(feature = "std")]
133133
fn main() {
134134
let a = Array::linspace(0., 63., 64).into_shape((8, 8)).unwrap();
135135
let strings = a.map(|x| x.to_string());
@@ -143,3 +143,5 @@ fn main() {
143143
let c = strings.permute_axis(Axis(1), &perm);
144144
println!("{:?}", c);
145145
}
146+
#[cfg(not(feature = "std"))]
147+
fn main() {}

src/array_approx.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ where
8181
#[cfg(test)]
8282
mod tests {
8383
use crate::prelude::*;
84+
use alloc::vec;
8485
use approx::{
8586
assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne,
8687
assert_ulps_eq, assert_ulps_ne,

src/array_serde.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
1111

1212
use std::fmt;
1313
use std::marker::PhantomData;
14+
use alloc::format;
15+
use alloc::vec::Vec;
1416

1517
use crate::imp_prelude::*;
1618

src/arrayformat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use super::{ArrayBase, ArrayView, Axis, Data, Dimension, NdProducer};
99
use crate::aliases::{Ix1, IxDyn};
1010
use std::fmt;
11+
use alloc::format;
12+
use alloc::string::String;
13+
use alloc::vec::Vec;
1114

1215
/// Default threshold, below this element count, we don't ellipsize
1316
const ARRAY_MANY_ELEMENT_LIMIT: usize = 500;

src/arraytraits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::iter::FromIterator;
1212
use std::iter::IntoIterator;
1313
use std::mem;
1414
use std::ops::{Index, IndexMut};
15+
use alloc::vec::Vec;
1516

1617
use crate::imp_prelude::*;
1718
use crate::iter::{Iter, IterMut};

src/data_repr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
21
use std::mem;
32
use std::mem::ManuallyDrop;
43
use std::ptr::NonNull;
5-
use std::slice;
4+
use alloc::slice;
5+
use alloc::borrow::ToOwned;
6+
use alloc::vec::Vec;
67
use crate::extension::nonnull;
78

89
/// Array's representation.

src/data_traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use rawpointer::PointerExt;
1212
use std::mem::{self, size_of};
1313
use std::ptr::NonNull;
14-
use std::sync::Arc;
14+
use alloc::sync::Arc;
15+
use alloc::vec::Vec;
1516

1617
use crate::{
1718
ArrayBase, CowRepr, Dimension, OwnedArcRepr, OwnedRepr, RawViewRepr, ViewRepr,

0 commit comments

Comments
 (0)