Skip to content

No_std support for ndarray #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ bench = false
test = true

[dependencies]
num-integer = "0.1.39"
num-integer = { version = "0.1.39", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-complex = { version = "0.3", default-features = false }

rayon = { version = "1.0.3", optional = true }

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

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

matrixmultiply = { version = "0.2.0" }
matrixmultiply = { version = "0.2.0", default-features = false}
serde = { version = "1.0", optional = true }
rawpointer = { version = "0.2" }

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

[features]
default = ["std"]

# Enable blas usage
# See README for more instructions
blas = ["cblas-sys", "blas-src"]
Expand All @@ -66,7 +67,7 @@ test = ["test-blas-openblas-sys"]
# This feature is used for docs
docs = ["approx", "serde", "rayon"]

std = ["num-traits/std"]
std = ["num-traits/std", "matrixmultiply/std"]

[profile.release]
[profile.bench]
Expand Down
14 changes: 13 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ Crate Feature Flags
The following crate feature flags are available. They are configured in
your `Cargo.toml`.

- ``std``

- Rust Standard Library

- This crate can be used without the standard library by disabling the
default `std` feature. To do so, use this in your `Cargo.toml`:

[dependencies]
ndarray = { version = "0.x.y", default-features = false }

- The `geomspace` `linspace` `logspace` `range` `std` `var` `var_axis` and `std_axis`
methods are only available when `std` is enabled.

- ``serde``

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


6 changes: 5 additions & 1 deletion examples/convo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused)]
extern crate ndarray;
extern crate num_traits;

#[cfg(feature = "std")]
use num_traits::Float;

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

#[inline(never)]
#[cfg(feature = "std")]
fn conv_3x3<F>(a: &ArrayView2<'_, F>, out: &mut ArrayViewMut2<'_, F>, kernel: &Kernel3x3<F>)
where
F: Float,
Expand Down Expand Up @@ -41,6 +42,7 @@ where
}
}

#[cfg(feature = "std")]
fn main() {
let n = 16;
let mut a = Array::zeros((n, n));
Expand All @@ -61,3 +63,5 @@ fn main() {
}
println!("{:2}", res);
}
#[cfg(not(feature = "std"))]
fn main() {}
4 changes: 3 additions & 1 deletion examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ where
}
}
}

#[cfg(feature = "std")]
fn main() {
let a = Array::linspace(0., 63., 64).into_shape((8, 8)).unwrap();
let strings = a.map(|x| x.to_string());
Expand All @@ -143,3 +143,5 @@ fn main() {
let c = strings.permute_axis(Axis(1), &perm);
println!("{:?}", c);
}
#[cfg(not(feature = "std"))]
fn main() {}
1 change: 1 addition & 0 deletions src/array_approx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ where
#[cfg(test)]
mod tests {
use crate::prelude::*;
use alloc::vec;
use approx::{
assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne,
assert_ulps_eq, assert_ulps_ne,
Expand Down
2 changes: 2 additions & 0 deletions src/array_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};

use std::fmt;
use std::marker::PhantomData;
use alloc::format;
use alloc::vec::Vec;

use crate::imp_prelude::*;

Expand Down
3 changes: 3 additions & 0 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use super::{ArrayBase, ArrayView, Axis, Data, Dimension, NdProducer};
use crate::aliases::{Ix1, IxDyn};
use std::fmt;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

/// Default threshold, below this element count, we don't ellipsize
const ARRAY_MANY_ELEMENT_LIMIT: usize = 500;
Expand Down
1 change: 1 addition & 0 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::iter::FromIterator;
use std::iter::IntoIterator;
use std::mem;
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

use crate::imp_prelude::*;
use crate::iter::{Iter, IterMut};
Expand Down
5 changes: 3 additions & 2 deletions src/data_repr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

use std::mem;
use std::mem::ManuallyDrop;
use std::ptr::NonNull;
use std::slice;
use alloc::slice;
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use crate::extension::nonnull;

/// Array's representation.
Expand Down
3 changes: 2 additions & 1 deletion src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use rawpointer::PointerExt;
use std::mem::{self, size_of};
use std::ptr::NonNull;
use std::sync::Arc;
use alloc::sync::Arc;
use alloc::vec::Vec;

use crate::{
ArrayBase, CowRepr, Dimension, OwnedArcRepr, OwnedRepr, RawViewRepr, ViewRepr,
Expand Down
1 change: 1 addition & 0 deletions src/dimension/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use num_traits::Zero;
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

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

Expand Down
2 changes: 1 addition & 1 deletion src/dimension/dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;

use std::fmt;
use super::Dimension;
use super::IntoDimension;
use crate::itertools::zip;
Expand Down
1 change: 1 addition & 0 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

use super::axes_of;
use super::conversion::Convert;
Expand Down
4 changes: 3 additions & 1 deletion src/dimension/dynindeximpl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::imp_prelude::*;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut, Index, IndexMut};

use alloc::vec;
use alloc::boxed::Box;
use alloc::vec::Vec;
const CAP: usize = 4;

/// T is usize or isize
Expand Down
6 changes: 3 additions & 3 deletions src/dimension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ mod test {

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

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

// Naively determine if the sequences intersect.
let seq1: Vec<_> = (0..len1)
let seq1: alloc::vec::Vec<_> = (0..len1)
.map(|n| first1 + step1 * n)
.collect();
let intersects = (0..len2)
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::Dimension;
#[cfg(feature = "std")]
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -69,6 +70,7 @@ impl PartialEq for ShapeError {
}
}

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

impl fmt::Display for ShapeError {
Expand Down
1 change: 1 addition & 0 deletions src/extension/nonnull.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ptr::NonNull;
use alloc::vec::Vec;

/// Return a NonNull<T> pointer to the vector's data
pub(crate) fn nonnull_from_vec_data<T>(v: &mut Vec<T>) -> NonNull<T> {
Expand Down
4 changes: 3 additions & 1 deletion src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// except according to those terms.

use std::mem::{forget, size_of};
use std::slice;
use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;

use crate::imp_prelude::*;
use crate::{dimension, ArcArray1, ArcArray2};
Expand Down
1 change: 1 addition & 0 deletions src/geomspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "std")]
use num_traits::Float;

/// An iterator of a sequence of geometrically spaced floats.
Expand Down
1 change: 1 addition & 0 deletions src/impl_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// except according to those terms.

//! Methods for one-dimensional arrays.
use alloc::vec::Vec;
use crate::imp_prelude::*;

/// # Methods For 1-D Arrays
Expand Down
13 changes: 11 additions & 2 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
//!

#![allow(clippy::match_wild_err_arm)]

use num_traits::{Float, One, Zero};
#[cfg(feature = "std")]
use num_traits::Float;
use num_traits::{One, Zero};
use std::mem::MaybeUninit;
use alloc::vec;
use alloc::vec::Vec;

use crate::dimension;
use crate::error::{self, ShapeError};
Expand All @@ -23,8 +26,10 @@ use crate::indexes;
use crate::indices;
use crate::iterators::{to_vec, to_vec_mapped};
use crate::StrideShape;
#[cfg(feature = "std")]
use crate::{geomspace, linspace, logspace};


/// # Constructor Methods for Owned Arrays
///
/// Note that the constructor methods apply to `Array` and `ArcArray`,
Expand Down Expand Up @@ -66,6 +71,7 @@ where
/// let array = Array::linspace(0., 1., 5);
/// assert!(array == arr1(&[0.0, 0.25, 0.5, 0.75, 1.0]))
/// ```
#[cfg(feature = "std")]
pub fn linspace(start: A, end: A, n: usize) -> Self
where
A: Float,
Expand All @@ -84,6 +90,7 @@ where
/// let array = Array::range(0., 5., 1.);
/// assert!(array == arr1(&[0., 1., 2., 3., 4.]))
/// ```
#[cfg(feature = "std")]
pub fn range(start: A, end: A, step: A) -> Self
where
A: Float,
Expand Down Expand Up @@ -112,6 +119,7 @@ where
/// assert_abs_diff_eq!(array, arr1(&[-1e3, -1e2, -1e1, -1e0]));
/// # }
/// ```
#[cfg(feature = "std")]
pub fn logspace(base: A, start: A, end: A, n: usize) -> Self
where
A: Float,
Expand Down Expand Up @@ -146,6 +154,7 @@ where
/// #
/// # example().unwrap();
/// ```
#[cfg(feature = "std")]
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
where
A: Float,
Expand Down
5 changes: 3 additions & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
// except according to those terms.

use std::ptr as std_ptr;
use std::slice;

use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;
use rawpointer::PointerExt;

use crate::imp_prelude::*;
Expand Down
1 change: 1 addition & 0 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

use alloc::vec::Vec;
use crate::imp_prelude::*;

/// Methods specific to `Array0`.
Expand Down
2 changes: 1 addition & 1 deletion src/impl_views/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::slice;
use alloc::slice;

use crate::imp_prelude::*;

Expand Down
8 changes: 6 additions & 2 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod windows;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ptr;
use alloc::vec::Vec;

use crate::Ix1;

Expand Down Expand Up @@ -1446,10 +1447,13 @@ pub unsafe trait TrustedIterator {}

use crate::indexes::IndicesIterF;
use crate::iter::IndicesIter;
#[cfg(feature = "std")]
use crate::{geomspace::Geomspace, linspace::Linspace, logspace::Logspace};

unsafe impl<F> TrustedIterator for Geomspace<F> {}
#[cfg(feature = "std")]
unsafe impl<F> TrustedIterator for Linspace<F> {}
#[cfg(feature = "std")]
unsafe impl<F> TrustedIterator for Geomspace<F> {}
#[cfg(feature = "std")]
unsafe impl<F> TrustedIterator for Logspace<F> {}
unsafe impl<'a, A, D> TrustedIterator for Iter<'a, A, D> {}
unsafe impl<'a, A, D> TrustedIterator for IterMut<'a, A, D> {}
Expand Down
Loading