Skip to content

Commit d9abfb5

Browse files
committed
Bump version to 0.3
1 parent 8999c9d commit d9abfb5

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "numpy"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Toshiki Teramura <[email protected]>"]
55

66
description = "Rust binding of NumPy C-API"

README.md

+51-44
Original file line numberDiff line numberDiff line change
@@ -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

1515
and 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+
1721
Example
1822
---------
1923
Please see [example](example) directory for a complete example
@@ -24,57 +28,58 @@ name = "rust_ext"
2428
crate-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+
3639
extern crate ndarray;
40+
extern crate numpy;
41+
extern crate pyo3;
3742

38-
use numpy::*;
3943
use 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

8085
Contribution
@@ -84,6 +89,8 @@ We need your feedback. Don't hesitate to open [issue](https://github.com/termosh
8489

8590
Version
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

Comments
 (0)