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

+5-4
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

+13-1
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

+5-1
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

+3-1
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

+1
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

+2
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

+3
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

+1
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

+3-2
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

+2-1
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,

src/dimension/conversion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use num_traits::Zero;
1212
use std::ops::{Index, IndexMut};
13+
use alloc::vec::Vec;
1314

1415
use crate::{Dim, Dimension, Ix, Ix1, IxDyn, IxDynImpl};
1516

src/dimension/dim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use std::fmt;
109

10+
use std::fmt;
1111
use super::Dimension;
1212
use super::IntoDimension;
1313
use crate::itertools::zip;

src/dimension/dimension_trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use std::fmt::Debug;
1010
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
1111
use std::ops::{Index, IndexMut};
12+
use alloc::vec::Vec;
1213

1314
use super::axes_of;
1415
use super::conversion::Convert;

src/dimension/dynindeximpl.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::imp_prelude::*;
22
use std::hash::{Hash, Hasher};
33
use std::ops::{Deref, DerefMut, Index, IndexMut};
4-
4+
use alloc::vec;
5+
use alloc::boxed::Box;
6+
use alloc::vec::Vec;
57
const CAP: usize = 4;
68

79
/// T is usize or isize

src/dimension/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ mod test {
676676

677677
#[test]
678678
fn slice_indexing_uncommon_strides() {
679-
let v: Vec<_> = (0..12).collect();
679+
let v: alloc::vec::Vec<_> = (0..12).collect();
680680
let dim = (2, 3, 2).into_dimension();
681681
let strides = (1, 2, 6).into_dimension();
682682
assert!(super::can_index_slice(&v, &dim, &strides).is_ok());
@@ -784,7 +784,7 @@ mod test {
784784
}
785785

786786
quickcheck! {
787-
fn can_index_slice_not_custom_same_as_can_index_slice(data: Vec<u8>, dim: Vec<usize>) -> bool {
787+
fn can_index_slice_not_custom_same_as_can_index_slice(data: alloc::vec::Vec<u8>, dim: alloc::vec::Vec<usize>) -> bool {
788788
let dim = IxDyn(&dim);
789789
let result = can_index_slice_not_custom(data.len(), &dim);
790790
if dim.size_checked().is_none() {
@@ -871,7 +871,7 @@ mod test {
871871
let (min2, max2) = (cmp::min(first2, last2), cmp::max(first2, last2));
872872

873873
// Naively determine if the sequences intersect.
874-
let seq1: Vec<_> = (0..len1)
874+
let seq1: alloc::vec::Vec<_> = (0..len1)
875875
.map(|n| first1 + step1 * n)
876876
.collect();
877877
let intersects = (0..len2)

src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88
use super::Dimension;
9+
#[cfg(feature = "std")]
910
use std::error::Error;
1011
use std::fmt;
1112

@@ -69,6 +70,7 @@ impl PartialEq for ShapeError {
6970
}
7071
}
7172

73+
#[cfg(feature = "std")]
7274
impl Error for ShapeError {}
7375

7476
impl fmt::Display for ShapeError {

src/extension/nonnull.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ptr::NonNull;
2+
use alloc::vec::Vec;
23

34
/// Return a NonNull<T> pointer to the vector's data
45
pub(crate) fn nonnull_from_vec_data<T>(v: &mut Vec<T>) -> NonNull<T> {

src/free_functions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
// except according to those terms.
88

99
use std::mem::{forget, size_of};
10-
use std::slice;
10+
use alloc::slice;
11+
use alloc::vec;
12+
use alloc::vec::Vec;
1113

1214
use crate::imp_prelude::*;
1315
use crate::{dimension, ArcArray1, ArcArray2};

src/geomspace.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8+
#![cfg(feature = "std")]
89
use num_traits::Float;
910

1011
/// An iterator of a sequence of geometrically spaced floats.

src/impl_1d.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// except according to those terms.
88

99
//! Methods for one-dimensional arrays.
10+
use alloc::vec::Vec;
1011
use crate::imp_prelude::*;
1112

1213
/// # Methods For 1-D Arrays

src/impl_constructors.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
//!
1212
1313
#![allow(clippy::match_wild_err_arm)]
14-
15-
use num_traits::{Float, One, Zero};
14+
#[cfg(feature = "std")]
15+
use num_traits::Float;
16+
use num_traits::{One, Zero};
1617
use std::mem::MaybeUninit;
18+
use alloc::vec;
19+
use alloc::vec::Vec;
1720

1821
use crate::dimension;
1922
use crate::error::{self, ShapeError};
@@ -23,8 +26,10 @@ use crate::indexes;
2326
use crate::indices;
2427
use crate::iterators::{to_vec, to_vec_mapped};
2528
use crate::StrideShape;
29+
#[cfg(feature = "std")]
2630
use crate::{geomspace, linspace, logspace};
2731

32+
2833
/// # Constructor Methods for Owned Arrays
2934
///
3035
/// Note that the constructor methods apply to `Array` and `ArcArray`,
@@ -66,6 +71,7 @@ where
6671
/// let array = Array::linspace(0., 1., 5);
6772
/// assert!(array == arr1(&[0.0, 0.25, 0.5, 0.75, 1.0]))
6873
/// ```
74+
#[cfg(feature = "std")]
6975
pub fn linspace(start: A, end: A, n: usize) -> Self
7076
where
7177
A: Float,
@@ -84,6 +90,7 @@ where
8490
/// let array = Array::range(0., 5., 1.);
8591
/// assert!(array == arr1(&[0., 1., 2., 3., 4.]))
8692
/// ```
93+
#[cfg(feature = "std")]
8794
pub fn range(start: A, end: A, step: A) -> Self
8895
where
8996
A: Float,
@@ -112,6 +119,7 @@ where
112119
/// assert_abs_diff_eq!(array, arr1(&[-1e3, -1e2, -1e1, -1e0]));
113120
/// # }
114121
/// ```
122+
#[cfg(feature = "std")]
115123
pub fn logspace(base: A, start: A, end: A, n: usize) -> Self
116124
where
117125
A: Float,
@@ -146,6 +154,7 @@ where
146154
/// #
147155
/// # example().unwrap();
148156
/// ```
157+
#[cfg(feature = "std")]
149158
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
150159
where
151160
A: Float,

src/impl_methods.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
// except according to those terms.
88

99
use std::ptr as std_ptr;
10-
use std::slice;
11-
10+
use alloc::slice;
11+
use alloc::vec;
12+
use alloc::vec::Vec;
1213
use rawpointer::PointerExt;
1314

1415
use crate::imp_prelude::*;

src/impl_owned_array.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
use alloc::vec::Vec;
23
use crate::imp_prelude::*;
34

45
/// Methods specific to `Array0`.

src/impl_views/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use std::slice;
9+
use alloc::slice;
1010

1111
use crate::imp_prelude::*;
1212

src/iterators/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod windows;
1616
use std::iter::FromIterator;
1717
use std::marker::PhantomData;
1818
use std::ptr;
19+
use alloc::vec::Vec;
1920

2021
use crate::Ix1;
2122

@@ -1446,10 +1447,13 @@ pub unsafe trait TrustedIterator {}
14461447

14471448
use crate::indexes::IndicesIterF;
14481449
use crate::iter::IndicesIter;
1450+
#[cfg(feature = "std")]
14491451
use crate::{geomspace::Geomspace, linspace::Linspace, logspace::Logspace};
1450-
1451-
unsafe impl<F> TrustedIterator for Geomspace<F> {}
1452+
#[cfg(feature = "std")]
14521453
unsafe impl<F> TrustedIterator for Linspace<F> {}
1454+
#[cfg(feature = "std")]
1455+
unsafe impl<F> TrustedIterator for Geomspace<F> {}
1456+
#[cfg(feature = "std")]
14531457
unsafe impl<F> TrustedIterator for Logspace<F> {}
14541458
unsafe impl<'a, A, D> TrustedIterator for Iter<'a, A, D> {}
14551459
unsafe impl<'a, A, D> TrustedIterator for IterMut<'a, A, D> {}

0 commit comments

Comments
 (0)