Skip to content

Commit 6b6c70c

Browse files
authored
Merge pull request #37 from rust-numpy/version-up
Bump version to 0.3
2 parents 40fceee + cb4fb83 commit 6b6c70c

File tree

6 files changed

+104
-51
lines changed

6 files changed

+104
-51
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

+66-49
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@ rust-numpy
33
[![Build Status](https://travis-ci.org/rust-numpy/rust-numpy.svg?branch=master)](https://travis-ci.org/rust-numpy/rust-numpy)
44
[![Build status](https://ci.appveyor.com/api/projects/status/bjaru43c7t1alx2x/branch/master?svg=true)](https://ci.appveyor.com/project/kngwyu/rust-numpy/branch/master)
55
[![Crate](http://meritbadge.herokuapp.com/numpy)](https://crates.io/crates/numpy)
6-
[![docs.rs](https://docs.rs/numpy/badge.svg)](https://docs.rs/numpy)
76

87
Rust binding of NumPy C-API
98

10-
Dependencies
9+
API documentation
1110
-------------
11+
- [Latest release(possibly broken)](https://docs.rs/numpy)
12+
- [Current Master](https://rust-numpy.github.io/rust-numpy)
1213

13-
- [rust-ndarray](https://github.com/bluss/rust-ndarray)
14-
- [rust-cpython](https://github.com/dgrunwald/rust-cpython)
1514

16-
and more (see [Cargo.toml](Cargo.toml))
15+
Requirements
16+
-------------
17+
- current nightly rust (see https://github.com/PyO3/pyo3/issues/5 for nightly features, and
18+
https://github.com/PyO3/pyo3/blob/master/build.rs for minimum required version)
19+
- some rust libraries
20+
- [rust-ndarray](https://github.com/bluss/rust-ndarray) for rust-side matrix library
21+
- [pyo3](https://github.com/PyO3/pyo3) for cpython binding
22+
- and more (see [Cargo.toml](Cargo.toml))
23+
- [numpy](http://www.numpy.org/) installed in your python environments(e.g. via `pip install numpy`)
24+
25+
**Note**
26+
From 0.3, we migrated from rust-cpython to pyo3.
27+
If you want to use rust-cpython, use version 0.2.1 from crates.io.
28+
1729

1830
Example
1931
---------
@@ -25,66 +37,71 @@ name = "rust_ext"
2537
crate-type = ["cdylib"]
2638
2739
[dependencies]
28-
numpy = "*"
29-
cpython = "*"
30-
ndarray = "*"
40+
numpy = "0.3"
41+
pyo3 = "^0.3.1"
42+
ndarray = "0.11"
3143
```
3244

3345
```rust
34-
#[macro_use]
35-
extern crate cpython;
36-
extern crate numpy;
46+
#![feature(use_extern_macros, specialization)]
47+
3748
extern crate ndarray;
49+
extern crate numpy;
50+
extern crate pyo3;
3851

39-
use numpy::*;
4052
use 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-
}
53+
use numpy::*;
54+
use pyo3::prelude::*;
55+
56+
#[pymodinit]
57+
fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
58+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59+
// You **must** write this sentence for PyArray type checker working correctly
60+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
61+
let _np = PyArrayModule::import(py)?;
62+
63+
// immutable example
64+
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
65+
a * &x + &y
66+
}
67+
68+
// mutable example (no return)
69+
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
70+
x *= a;
71+
}
72+
73+
// wrapper of `axpy`
74+
#[pyfn(m, "axpy")]
75+
fn axpy_py(py: Python, a: f64, x: &PyArray, y: &PyArray) -> PyResult<PyArray> {
76+
let np = PyArrayModule::import(py)?;
77+
let x = x.as_array().into_pyresult("x must be f64 array")?;
78+
let y = y.as_array().into_pyresult("y must be f64 array")?;
79+
Ok(axpy(a, x, y).into_pyarray(py, &np))
80+
}
81+
82+
// wrapper of `mult`
83+
#[pyfn(m, "mult")]
84+
fn mult_py(_py: Python, a: f64, x: &PyArray) -> PyResult<()> {
85+
let x = x.as_array_mut().into_pyresult("x must be f64 array")?;
86+
mult(a, x);
87+
Ok(())
88+
}
7189

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)))?;
7790
Ok(())
78-
});
91+
}
7992
```
8093

8194
Contribution
8295
-------------
8396
This project is in pre-alpha version.
84-
We need your feedback. Don't hesitate to open [issue](https://github.com/termoshtt/rust-numpy/issues)!
97+
We need your feedback. Don't hesitate to open [issues](https://github.com/termoshtt/rust-numpy/issues)!
8598

8699
Version
87100
--------
101+
- v0.3.0
102+
- Breaking Change: Migrated to pyo3 from rust-cpython
103+
- Some api addition
104+
- [Static type checking with PhantomData](https://github.com/rust-numpy/rust-numpy/pull/41)
88105

89106
- v0.2.1
90107
- NEW: trait `IntoPyErr`, `IntoPyResult` for error translation

src/convert.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Defines conversion trait between rust types and numpy data types.
2+
13
use ndarray::*;
24
use pyo3::Python;
35

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Define Errors
1+
//! Defines error types.
22
33
use pyo3::*;
44
use std::error;

src/lib.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
//! `rust-numpy` provides Rust interfaces for [NumPy C APIs](https://docs.scipy.org/doc/numpy/reference/c-api.html),
2+
//! especially for [ndarray](https://www.numpy.org/devdocs/reference/arrays.ndarray.html) class.
3+
//!
4+
//! It uses [pyo3](https://github.com/PyO3/pyo3) for rust bindings to cpython, and uses
5+
//! [ndarray](https://github.com/bluss/ndarray) for rust side matrix library.
6+
//!
7+
//! For numpy dependency, it calls `import numpy.core` internally. So you just need numpy
8+
//! installed by `pip install numpy` or other ways in your python environment.
9+
//! You can use both system environment and `virtualenv`.
10+
//!
11+
//! # Example
12+
//!
13+
//! ```
14+
//! #[macro_use]
15+
//! extern crate ndarray;
16+
//! extern crate numpy;
17+
//! #[macro_use]
18+
//! extern crate pyo3;
19+
//! use pyo3::prelude::*;
20+
//! use numpy::*;
21+
//! fn main() {
22+
//! let gil = Python::acquire_gil();
23+
//! let py = gil.python();
24+
//! let np = PyArrayModule::import(py).unwrap();
25+
//! let py_array = array![[1i64, 2], [3, 4]].into_pyarray(py, &np);
26+
//! assert_eq!(
27+
//! py_array.as_array().unwrap(),
28+
//! array![[1i64, 2], [3, 4]].into_dyn(),
29+
//! );
30+
//! }
31+
//! ```
32+
133
#![feature(specialization)]
234

335
#[macro_use]

src/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Implements conversion utitlities.
2+
13
pub use num_complex::Complex32 as c32;
24
pub use num_complex::Complex64 as c64;
35

0 commit comments

Comments
 (0)