@@ -10,10 +10,14 @@ Dependencies
1010-------------
1111
1212- [ rust-ndarray] ( https://github.com/bluss/rust-ndarray )
13- - [ rust-cpython ] ( https://github.com/dgrunwald/rust-cpython )
13+ - [ pyo3 ] ( https://github.com/PyO3/pyo3 )
1414
1515and more (see [ Cargo.toml] ( Cargo.toml ) )
1616
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+
1721Example
1822---------
1923Please see [ example] ( example ) directory for a complete example
@@ -24,57 +28,58 @@ name = "rust_ext"
2428crate-type = ["cdylib"]
2529
2630[dependencies]
27- numpy = "* "
28- cpython = "* "
29- ndarray = "* "
31+ numpy = "0.3 "
32+ pyo3 = "^0.3.1 "
33+ ndarray = "0.11 "
3034```
3135
3236``` rust
33- #[macro_use]
34- extern crate cpython;
35- extern crate numpy;
37+ #![feature(use_extern_macros, specialization)]
38+
3639extern crate ndarray;
40+ extern crate numpy;
41+ extern crate pyo3;
3742
38- use numpy :: * ;
3943use 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+ }
7080
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 )))? ;
7681 Ok (())
77- });
82+ }
7883```
7984
8085Contribution
@@ -84,6 +89,8 @@ We need your feedback. Don't hesitate to open [issue](https://github.com/termosh
8489
8590Version
8691--------
92+ - v0.3.0
93+ - Breaking Change: Migrated to pyo3 from rust-cpython
8794
8895- v0.2.1
8996 - NEW: trait ` IntoPyErr ` , ` IntoPyResult ` for error translation
0 commit comments