@@ -3,17 +3,29 @@ rust-numpy
3
3
[ ![ Build Status] ( https://travis-ci.org/rust-numpy/rust-numpy.svg?branch=master )] ( https://travis-ci.org/rust-numpy/rust-numpy )
4
4
[ ![ Build status] ( https://ci.appveyor.com/api/projects/status/bjaru43c7t1alx2x/branch/master?svg=true )] ( https://ci.appveyor.com/project/kngwyu/rust-numpy/branch/master )
5
5
[ ![ Crate] ( http://meritbadge.herokuapp.com/numpy )] ( https://crates.io/crates/numpy )
6
- [ ![ docs.rs] ( https://docs.rs/numpy/badge.svg )] ( https://docs.rs/numpy )
7
6
8
7
Rust binding of NumPy C-API
9
8
10
- Dependencies
9
+ API documentation
11
10
-------------
11
+ - [ Latest release(possibly broken)] ( https://docs.rs/numpy )
12
+ - [ Current Master] ( https://rust-numpy.github.io/rust-numpy )
12
13
13
- - [ rust-ndarray] ( https://github.com/bluss/rust-ndarray )
14
- - [ rust-cpython] ( https://github.com/dgrunwald/rust-cpython )
15
14
16
- and more (see [ Cargo.toml] ( Cargo.toml ) )
15
+ Requirements
16
+ -------------
17
+ - current nightly rust (see https://github.com/PyO3/pyo3/issues/5 for nightly features, and
18
+ https://github.com/PyO3/pyo3/blob/master/build.rs for minimum required version)
19
+ - some rust libraries
20
+ - [ rust-ndarray] ( https://github.com/bluss/rust-ndarray ) for rust-side matrix library
21
+ - [ pyo3] ( https://github.com/PyO3/pyo3 ) for cpython binding
22
+ - and more (see [ Cargo.toml] ( Cargo.toml ) )
23
+ - [ numpy] ( http://www.numpy.org/ ) installed in your python environments(e.g. via ` pip install numpy ` )
24
+
25
+ ** Note**
26
+ From 0.3, we migrated from rust-cpython to pyo3.
27
+ If you want to use rust-cpython, use version 0.2.1 from crates.io.
28
+
17
29
18
30
Example
19
31
---------
@@ -25,66 +37,71 @@ name = "rust_ext"
25
37
crate-type = ["cdylib"]
26
38
27
39
[dependencies]
28
- numpy = "* "
29
- cpython = "* "
30
- ndarray = "* "
40
+ numpy = "0.3 "
41
+ pyo3 = "^0.3.1 "
42
+ ndarray = "0.11 "
31
43
```
32
44
33
45
``` rust
34
- #[macro_use]
35
- extern crate cpython;
36
- extern crate numpy;
46
+ #![feature(use_extern_macros, specialization)]
47
+
37
48
extern crate ndarray;
49
+ extern crate numpy;
50
+ extern crate pyo3;
38
51
39
- use numpy :: * ;
40
52
use ndarray :: * ;
41
- use cpython :: {PyResult , Python , PyObject };
42
-
43
- /* Pure rust-ndarray functions */
44
-
45
- // immutable example
46
- fn axpy (a : f64 , x : ArrayViewD <f64 >, y : ArrayViewD <f64 >) -> ArrayD <f64 > {
47
- a * & x + & y
48
- }
49
-
50
- // mutable example (no return)
51
- fn mult (a : f64 , mut x : ArrayViewMutD <f64 >) {
52
- x *= a ;
53
- }
54
-
55
- /* rust-cpython wrappers (to be exposed) */
56
-
57
- // wrapper of `axpy`
58
- fn axpy_py (py : Python , a : f64 , x : PyArray , y : PyArray ) -> PyResult <PyArray > {
59
- let np = PyArrayModule :: import (py )? ;
60
- let x = x . as_array (). into_pyresult (py , " x must be f64 array" )? ;
61
- let y = y . as_array (). into_pyresult (py , " y must be f64 array" )? ;
62
- Ok (axpy (a , x , y ). into_pyarray (py , & np ))
63
- }
64
-
65
- // wrapper of `mult`
66
- fn mult_py (py : Python , a : f64 , x : PyArray ) -> PyResult <PyObject > {
67
- let x = x . as_array_mut (). into_pyresult (py , " x must be f64 array" )? ;
68
- mult (a , x );
69
- Ok (py . None ()) // Python function must returns
70
- }
53
+ use numpy :: * ;
54
+ use pyo3 :: prelude :: * ;
55
+
56
+ #[pymodinit]
57
+ fn rust_ext (py : Python , m : & PyModule ) -> PyResult <()> {
58
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59
+ // You **must** write this sentence for PyArray type checker working correctly
60
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
61
+ let _np = PyArrayModule :: import (py )? ;
62
+
63
+ // immutable example
64
+ fn axpy (a : f64 , x : ArrayViewD <f64 >, y : ArrayViewD <f64 >) -> ArrayD <f64 > {
65
+ a * & x + & y
66
+ }
67
+
68
+ // mutable example (no return)
69
+ fn mult (a : f64 , mut x : ArrayViewMutD <f64 >) {
70
+ x *= a ;
71
+ }
72
+
73
+ // wrapper of `axpy`
74
+ #[pyfn(m, " axpy" )]
75
+ fn axpy_py (py : Python , a : f64 , x : & PyArray , y : & PyArray ) -> PyResult <PyArray > {
76
+ let np = PyArrayModule :: import (py )? ;
77
+ let x = x . as_array (). into_pyresult (" x must be f64 array" )? ;
78
+ let y = y . as_array (). into_pyresult (" y must be f64 array" )? ;
79
+ Ok (axpy (a , x , y ). into_pyarray (py , & np ))
80
+ }
81
+
82
+ // wrapper of `mult`
83
+ #[pyfn(m, " mult" )]
84
+ fn mult_py (_py : Python , a : f64 , x : & PyArray ) -> PyResult <()> {
85
+ let x = x . as_array_mut (). into_pyresult (" x must be f64 array" )? ;
86
+ mult (a , x );
87
+ Ok (())
88
+ }
71
89
72
- /* Define module "_rust_ext" */
73
- py_module_initializer! (_rust_ext , init_rust_ext , PyInit__rust_ext , | py , m | {
74
- m . add (py , " __doc__" , " Rust extension for NumPy" )? ;
75
- m . add (py , " axpy" , py_fn! (py , axpy_py (a : f64 , x : PyArray , y : PyArray )))? ;
76
- m . add (py , " mult" , py_fn! (py , mult_py (a : f64 , x : PyArray )))? ;
77
90
Ok (())
78
- });
91
+ }
79
92
```
80
93
81
94
Contribution
82
95
-------------
83
96
This project is in pre-alpha version.
84
- We need your feedback. Don't hesitate to open [ issue ] ( https://github.com/termoshtt/rust-numpy/issues ) !
97
+ We need your feedback. Don't hesitate to open [ issues ] ( https://github.com/termoshtt/rust-numpy/issues ) !
85
98
86
99
Version
87
100
--------
101
+ - v0.3.0
102
+ - Breaking Change: Migrated to pyo3 from rust-cpython
103
+ - Some api addition
104
+ - [ Static type checking with PhantomData] ( https://github.com/rust-numpy/rust-numpy/pull/41 )
88
105
89
106
- v0.2.1
90
107
- NEW: trait ` IntoPyErr ` , ` IntoPyResult ` for error translation
0 commit comments