@@ -11,10 +11,14 @@ Dependencies
1111-------------
1212
1313- [ rust-ndarray] ( https://github.com/bluss/rust-ndarray )
14- - [ rust-cpython ] ( https://github.com/dgrunwald/rust-cpython )
14+ - [ pyo3 ] ( https://github.com/PyO3/pyo3 )
1515
1616and more (see [ Cargo.toml] ( Cargo.toml ) )
1717
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+
1822Example
1923---------
2024Please see [ example] ( example ) directory for a complete example
@@ -25,57 +29,58 @@ name = "rust_ext"
2529crate-type = ["cdylib"]
2630
2731[dependencies]
28- numpy = "* "
29- cpython = "* "
30- ndarray = "* "
32+ numpy = "0.3 "
33+ pyo3 = "^0.3.1 "
34+ ndarray = "0.11 "
3135```
3236
3337``` rust
34- #[macro_use]
35- extern crate cpython;
36- extern crate numpy;
38+ #![feature(use_extern_macros, specialization)]
39+
3740extern crate ndarray;
41+ extern crate numpy;
42+ extern crate pyo3;
3843
39- use numpy :: * ;
4044use 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+ }
7181
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 )))? ;
7782 Ok (())
78- });
83+ }
7984```
8085
8186Contribution
@@ -85,6 +90,8 @@ We need your feedback. Don't hesitate to open [issue](https://github.com/termosh
8590
8691Version
8792--------
93+ - v0.3.0
94+ - Breaking Change: Migrated to pyo3 from rust-cpython
8895
8996- v0.2.1
9097 - NEW: trait ` IntoPyErr ` , ` IntoPyResult ` for error translation
0 commit comments