@@ -10,10 +10,14 @@ Dependencies
10
10
-------------
11
11
12
12
- [ rust-ndarray] ( https://github.com/bluss/rust-ndarray )
13
- - [ rust-cpython ] ( https://github.com/dgrunwald/rust-cpython )
13
+ - [ pyo3 ] ( https://github.com/PyO3/pyo3 )
14
14
15
15
and more (see [ Cargo.toml] ( Cargo.toml ) )
16
16
17
+ ** Note**
18
+ From 0.3, we migrated from rust-cpython to pyo3.
19
+ If you want rust-cpython, use version 0.2.1 from crates.io.
20
+
17
21
Example
18
22
---------
19
23
Please see [ example] ( example ) directory for a complete example
@@ -24,57 +28,58 @@ name = "rust_ext"
24
28
crate-type = ["cdylib"]
25
29
26
30
[dependencies]
27
- numpy = "* "
28
- cpython = "* "
29
- ndarray = "* "
31
+ numpy = "0.3 "
32
+ pyo3 = "^0.3.1 "
33
+ ndarray = "0.11 "
30
34
```
31
35
32
36
``` rust
33
- #[macro_use]
34
- extern crate cpython;
35
- extern crate numpy;
37
+ #![feature(use_extern_macros, specialization)]
38
+
36
39
extern crate ndarray;
40
+ extern crate numpy;
41
+ extern crate pyo3;
37
42
38
- use numpy :: * ;
39
43
use ndarray :: * ;
40
- use cpython :: {PyResult , Python , PyObject };
41
-
42
- /* Pure rust-ndarray functions */
43
-
44
- // immutable example
45
- fn axpy (a : f64 , x : ArrayViewD <f64 >, y : ArrayViewD <f64 >) -> ArrayD <f64 > {
46
- a * & x + & y
47
- }
48
-
49
- // mutable example (no return)
50
- fn mult (a : f64 , mut x : ArrayViewMutD <f64 >) {
51
- x *= a ;
52
- }
53
-
54
- /* rust-cpython wrappers (to be exposed) */
55
-
56
- // wrapper of `axpy`
57
- fn axpy_py (py : Python , a : f64 , x : PyArray , y : PyArray ) -> PyResult <PyArray > {
58
- let np = PyArrayModule :: import (py )? ;
59
- let x = x . as_array (). into_pyresult (py , " x must be f64 array" )? ;
60
- let y = y . as_array (). into_pyresult (py , " y must be f64 array" )? ;
61
- Ok (axpy (a , x , y ). into_pyarray (py , & np ))
62
- }
63
-
64
- // wrapper of `mult`
65
- fn mult_py (py : Python , a : f64 , x : PyArray ) -> PyResult <PyObject > {
66
- let x = x . as_array_mut (). into_pyresult (py , " x must be f64 array" )? ;
67
- mult (a , x );
68
- Ok (py . None ()) // Python function must returns
69
- }
44
+ use numpy :: * ;
45
+ use pyo3 :: prelude :: * ;
46
+
47
+ #[pymodinit]
48
+ fn rust_ext (py : Python , m : & PyModule ) -> PyResult <()> {
49
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50
+ // You **must** write this sentence for PyArray type checker working correctly
51
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
52
+ let _np = PyArrayModule :: import (py )? ;
53
+
54
+ // immutable example
55
+ fn axpy (a : f64 , x : ArrayViewD <f64 >, y : ArrayViewD <f64 >) -> ArrayD <f64 > {
56
+ a * & x + & y
57
+ }
58
+
59
+ // mutable example (no return)
60
+ fn mult (a : f64 , mut x : ArrayViewMutD <f64 >) {
61
+ x *= a ;
62
+ }
63
+
64
+ // wrapper of `axpy`
65
+ #[pyfn(m, " axpy" )]
66
+ fn axpy_py (py : Python , a : f64 , x : & PyArray , y : & PyArray ) -> PyResult <PyArray > {
67
+ let np = PyArrayModule :: import (py )? ;
68
+ let x = x . as_array (). into_pyresult (" x must be f64 array" )? ;
69
+ let y = y . as_array (). into_pyresult (" y must be f64 array" )? ;
70
+ Ok (axpy (a , x , y ). into_pyarray (py , & np ))
71
+ }
72
+
73
+ // wrapper of `mult`
74
+ #[pyfn(m, " mult" )]
75
+ fn mult_py (_py : Python , a : f64 , x : & PyArray ) -> PyResult <()> {
76
+ let x = x . as_array_mut (). into_pyresult (" x must be f64 array" )? ;
77
+ mult (a , x );
78
+ Ok (())
79
+ }
70
80
71
- /* Define module "_rust_ext" */
72
- py_module_initializer! (_rust_ext , init_rust_ext , PyInit__rust_ext , | py , m | {
73
- m . add (py , " __doc__" , " Rust extension for NumPy" )? ;
74
- m . add (py , " axpy" , py_fn! (py , axpy_py (a : f64 , x : PyArray , y : PyArray )))? ;
75
- m . add (py , " mult" , py_fn! (py , mult_py (a : f64 , x : PyArray )))? ;
76
81
Ok (())
77
- });
82
+ }
78
83
```
79
84
80
85
Contribution
@@ -84,6 +89,8 @@ We need your feedback. Don't hesitate to open [issue](https://github.com/termosh
84
89
85
90
Version
86
91
--------
92
+ - v0.3.0
93
+ - Breaking Change: Migrated to pyo3 from rust-cpython
87
94
88
95
- v0.2.1
89
96
- NEW: trait ` IntoPyErr ` , ` IntoPyResult ` for error translation
0 commit comments