Skip to content

Commit

Permalink
Bump version to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Jul 19, 2018
1 parent 8999c9d commit d9abfb5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "numpy"
version = "0.2.1"
version = "0.3.0"
authors = ["Toshiki Teramura <[email protected]>"]

description = "Rust binding of NumPy C-API"
Expand Down
95 changes: 51 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
a * &x + &y
}

// mutable example (no return)
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
x *= a;
}

/* rust-cpython wrappers (to be exposed) */

// wrapper of `axpy`
fn axpy_py(py: Python, a: f64, x: PyArray, y: PyArray) -> PyResult<PyArray> {
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<PyObject> {
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<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
a * &x + &y
}

// mutable example (no return)
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
x *= a;
}

// wrapper of `axpy`
#[pyfn(m, "axpy")]
fn axpy_py(py: Python, a: f64, x: &PyArray, y: &PyArray) -> PyResult<PyArray> {
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
Expand All @@ -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
Expand Down

0 comments on commit d9abfb5

Please sign in to comment.