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