From d9abfb5064cef7715eca26773574a69fcf2f4365 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Thu, 19 Jul 2018 11:21:37 +0900 Subject: [PATCH] Bump version to 0.3 --- Cargo.toml | 2 +- README.md | 95 +++++++++++++++++++++++++++++------------------------- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 11bbac89e..20ce21a49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "numpy" -version = "0.2.1" +version = "0.3.0" authors = ["Toshiki Teramura "] description = "Rust binding of NumPy C-API" diff --git a/README.md b/README.md index 74fe770a4..7abf94940 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,14 @@ Dependencies ------------- - [rust-ndarray](https://github.com/bluss/rust-ndarray) -- [rust-cpython](https://github.com/dgrunwald/rust-cpython) +- [pyo3](https://github.com/PyO3/pyo3) and more (see [Cargo.toml](Cargo.toml)) +**Note** +From 0.3, we migrated from rust-cpython to pyo3. +If you want rust-cpython, use version 0.2.1 from crates.io. + Example --------- Please see [example](example) directory for a complete example @@ -24,57 +28,58 @@ name = "rust_ext" crate-type = ["cdylib"] [dependencies] -numpy = "*" -cpython = "*" -ndarray = "*" +numpy = "0.3" +pyo3 = "^0.3.1" +ndarray = "0.11" ``` ```rust -#[macro_use] -extern crate cpython; -extern crate numpy; +#![feature(use_extern_macros, specialization)] + extern crate ndarray; +extern crate numpy; +extern crate pyo3; -use numpy::*; use ndarray::*; -use cpython::{PyResult, Python, PyObject}; - -/* Pure rust-ndarray functions */ - -// immutable example -fn axpy(a: f64, x: ArrayViewD, y: ArrayViewD) -> ArrayD { - a * &x + &y -} - -// mutable example (no return) -fn mult(a: f64, mut x: ArrayViewMutD) { - x *= a; -} - -/* rust-cpython wrappers (to be exposed) */ - -// wrapper of `axpy` -fn axpy_py(py: Python, a: f64, x: PyArray, y: PyArray) -> PyResult { - let np = PyArrayModule::import(py)?; - let x = x.as_array().into_pyresult(py, "x must be f64 array")?; - let y = y.as_array().into_pyresult(py, "y must be f64 array")?; - Ok(axpy(a, x, y).into_pyarray(py, &np)) -} - -// wrapper of `mult` -fn mult_py(py: Python, a: f64, x: PyArray) -> PyResult { - let x = x.as_array_mut().into_pyresult(py, "x must be f64 array")?; - mult(a, x); - Ok(py.None()) // Python function must returns -} +use numpy::*; +use pyo3::prelude::*; + +#[pymodinit] +fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> { + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + // You **must** write this sentence for PyArray type checker working correctly + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + let _np = PyArrayModule::import(py)?; + + // immutable example + fn axpy(a: f64, x: ArrayViewD, y: ArrayViewD) -> ArrayD { + a * &x + &y + } + + // mutable example (no return) + fn mult(a: f64, mut x: ArrayViewMutD) { + x *= a; + } + + // wrapper of `axpy` + #[pyfn(m, "axpy")] + fn axpy_py(py: Python, a: f64, x: &PyArray, y: &PyArray) -> PyResult { + let np = PyArrayModule::import(py)?; + let x = x.as_array().into_pyresult("x must be f64 array")?; + let y = y.as_array().into_pyresult("y must be f64 array")?; + Ok(axpy(a, x, y).into_pyarray(py, &np)) + } + + // wrapper of `mult` + #[pyfn(m, "mult")] + fn mult_py(_py: Python, a: f64, x: &PyArray) -> PyResult<()> { + let x = x.as_array_mut().into_pyresult("x must be f64 array")?; + mult(a, x); + Ok(()) + } -/* Define module "_rust_ext" */ -py_module_initializer!(_rust_ext, init_rust_ext, PyInit__rust_ext, |py, m| { - m.add(py, "__doc__", "Rust extension for NumPy")?; - m.add(py, "axpy", py_fn!(py, axpy_py(a: f64, x: PyArray, y: PyArray)))?; - m.add(py, "mult", py_fn!(py, mult_py(a: f64, x: PyArray)))?; Ok(()) -}); +} ``` Contribution @@ -84,6 +89,8 @@ We need your feedback. Don't hesitate to open [issue](https://github.com/termosh Version -------- +- v0.3.0 + - Breaking Change: Migrated to pyo3 from rust-cpython - v0.2.1 - NEW: trait `IntoPyErr`, `IntoPyResult` for error translation