PyO3 / rust-numpy

PyO3-based Rust bindings of the NumPy C-API
BSD 2-Clause "Simplified" License
1.13k stars 111 forks source link

Bad examples? #224

Closed mikeWShef closed 2 years ago

mikeWShef commented 2 years ago

Hi I'm really interested in using this to speed up some python code, I have been looking at the examples here:

https://github.com/PyO3/rust-numpy/blob/main/examples/simple-extension/Cargo.toml and here: https://lib.rs/crates/numpy (under Write a Python module in Rust)

The bottom one has: use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD}; in lib.rs but ndarray is not a dependency in cargo.toml, I tried adding it to the dependency list but I can't get it to compile (example error below), from looking at other issues it seems like people kind of know this happens...

The other example has: [dependencies] numpy = { path = "../.." }

In cargo.toml, which also obviously isn't going to work on my machine. Is it possible to get a beginer friendly example that compiles?

Error from ndarray: error[E0277]: the trait bound <S as data_traits::DataOwned>::MaybeUninit: RawDataSubst<A> is not satisfied --> C:\Users--me--.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\impl_ops.rs:401:12 401 S: DataOwned + DataMut, ^^^^^^^^^^^^^^^^^^^ the trait RawDataSubst<A> is not implemented for <S as data_traits::DataOwned>::MaybeUninit
::: C:\Users--me--.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\data_traits.rs:518:18 518 pub unsafe trait DataOwned: Data { --------- required by a bound in this ... 521 + RawDataSubst<Self::Elem, Output=Self>; ------------------------------------- required by this bound in data_traits::DataOwned

help: consider further restricting the associated type | 406 | fn not(mut self) -> Self where ::MaybeUninit: RawDataSubst { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

adamreichold commented 2 years ago

Indeed the example in the README (availabe on crates.io) is outdated, but the example in the repository here contains a better way of doing things: This crate re-exports its dependency on ndarray, i.e. instead of

use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};

you can do

use numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};

(Will open a PR against the README...)

mikeWShef commented 2 years ago

I did try that as well, but I still get 209 errors while trying to compile ndarray:

error[E0277]: the trait bound `<S as data_traits::DataOwned>::MaybeUninit: RawDataSubst<A>` is not satisfied
   --> C:\Users\__me__\.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\impl_ops.rs:401:12
    |
401 |         S: DataOwned<Elem = A> + DataMut,
    |            ^^^^^^^^^^^^^^^^^^^ the trait `RawDataSubst<A>` is not implemented for `<S as data_traits::DataOwned>::MaybeUninit`
    | 
   ::: C:\Users\__me__\.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\data_traits.rs:518:18
    |
518 | pub unsafe trait DataOwned: Data {
    |                  --------- required by a bound in this
...
521 |         + RawDataSubst<Self::Elem, Output=Self>;
    |           ------------------------------------- required by this bound in `data_traits::DataOwned`
    |
help: consider further restricting the associated type
    |
406 |         fn not(mut self) -> Self where <S as data_traits::DataOwned>::MaybeUninit: RawDataSubst<A> {
    |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   Compiling pyo3-macros-backend v0.15.1
error[E0277]: the trait bound `<S as data_traits::DataOwned>::MaybeUninit: RawDataSubst<R>` is not satisfied
   --> C:\Users\__me__\.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\zip\mod.rs:742:26
    |
742 |                   where S: DataOwned<Elem = R>
    |                            ^^^^^^^^^^^^^^^^^^^ the trait `RawDataSubst<R>` is not implemented for `<S as data_traits::DataOwned>::MaybeUninit`
...
912 | / map_impl! {
913 | |     [true P1],
914 | |     [true P1 P2],
915 | |     [true P1 P2 P3],
...   |
918 | |     [false P1 P2 P3 P4 P5 P6],
919 | | }
    | |_- in this macro invocation
    | 
   ::: C:\Users\__me__\.cargo\registry\src\github.com-1ecc6299db9ec823\ndarray-0.15.4\src\data_traits.rs:518:18
    |
518 |   pub unsafe trait DataOwned: Data {
    |                    --------- required by a bound in this
...
521 |           + RawDataSubst<Self::Elem, Output=Self>;
    |             ------------------------------------- required by this bound in `data_traits::DataOwned`
    |
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 209 previous errors
mikeWShef commented 2 years ago

Never mind: rustup update fixed it