From d88380213c1c29379ba3e130883a071f5f1b6c0b Mon Sep 17 00:00:00 2001 From: kngwyu Date: Tue, 16 Mar 2021 01:27:17 +0900 Subject: [PATCH] Relax MSRV version to 1.41.1 and test it in CI --- .github/workflows/ci.yml | 38 ++++++++++++++++++++- Cargo.toml | 4 +-- README.md | 7 ++-- examples/linalg/Cargo.toml | 2 +- examples/simple-extension/Cargo.toml | 1 + examples/simple-extension/src/lib.rs | 20 ++++++++--- examples/simple-extension/tests/test_ext.py | 7 +++- src/lib.rs | 0 8 files changed, 66 insertions(+), 13 deletions(-) mode change 100755 => 100644 src/lib.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ba92d1cd..b57d6a1ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,43 @@ jobs: env: RUST_BACKTRACE: 1 + check-msrv: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + - name: Install MSRV Rust + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: 1.41.1 + - name: Install maturin, poetry, and toml + run: pip install maturin poetry toml + - name: Create an isolated example directory + run: cp -r examples/simple-extension/ ../simple-extension-msrv + - name: Edit Cargo.toml + run: | + import toml + cargo_toml = toml.load("Cargo.toml") + cargo_toml["dependencies"]["ndarray"] = "0.13.1" + cargo_toml["dependencies"]["num-complex"] = "0.2.4" + cargo_toml["dependencies"]["numpy"]["path"] = "../rust-numpy" + with open("Cargo.toml", "w") as f: + toml.dump(cargo_toml, f) + working-directory: ../simple-extension-msrv + shell: python + - name: Remove ndarray 0.14.0 + run: cargo update -p ndarray:0.14.0 --precise 0.13.1 + working-directory: ../simple-extension-msrv + - name: Test Example + run: | + poetry install && poetry run maturin develop && poetry run pytest + working-directory: ../simple-extension-msrv + shell: bash + linalg-example: runs-on: ubuntu-latest steps: @@ -85,7 +122,6 @@ jobs: uses: actions-rs/toolchain@v1 with: toolchain: stable - default: true - name: Install maturin and poetry run: pip install maturin poetry - name: Test Examples diff --git a/Cargo.toml b/Cargo.toml index d3ac062cd..6535b8900 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,9 @@ license-file = "LICENSE" [dependencies] cfg-if = "0.1" libc = "0.2" -num-complex = "0.3" +num-complex = ">= 0.2, < 0.4" num-traits = "0.2" -ndarray = "0.14" +ndarray = ">= 0.13, < 0.15" pyo3 = "0.13" [features] diff --git a/README.md b/README.md index ba2caa253..d681eaa86 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ rust-numpy =========== [![Actions Status](https://github.com/pyo3/rust-numpy/workflows/CI/badge.svg)](https://github.com/pyo3/rust-numpy/actions) [![Crate](http://meritbadge.herokuapp.com/numpy)](https://crates.io/crates/numpy) -[![minimum rustc 1.39](https://img.shields.io/badge/rustc-1.39+-blue.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) +[![minimum rustc 1.41](https://img.shields.io/badge/rustc-1.41+-blue.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) Rust bindings for the NumPy C-API @@ -12,12 +12,13 @@ Rust bindings for the NumPy C-API ## Requirements -- Rust 1.39+ +- Rust >= 1.41.1 + - Basically, our MSRV follows the one of [PyO3](https://github.com/PyO3/pyo3). - Python >= 3.6 - Python 3.5 support is dropped from 0.13 - Some Rust libraries - [ndarray](https://github.com/bluss/ndarray) for rust-side matrix library - - [pyo3](https://github.com/PyO3/pyo3) for cpython binding + - [PyO3](https://github.com/PyO3/pyo3) for cpython binding - and more (see [Cargo.toml](Cargo.toml)) - [numpy](http://www.numpy.org/) installed in your python environments (e.g., via `pip install numpy`) - We recommend `numpy >= 1.16.0`, though older version may work. diff --git a/examples/linalg/Cargo.toml b/examples/linalg/Cargo.toml index a669f8630..848ecec3a 100644 --- a/examples/linalg/Cargo.toml +++ b/examples/linalg/Cargo.toml @@ -11,7 +11,7 @@ crate-type = ["cdylib"] [dependencies] numpy = { path = "../.." } ndarray = "0.14" -ndarray-linalg = { git = "https://github.com/kngwyu/ndarray-linalg", branch = "ndarray-014", features = ["openblas-static"] } +ndarray-linalg = { version = "0.13", features = ["openblas-static"] } [dependencies.pyo3] version = "0.13" diff --git a/examples/simple-extension/Cargo.toml b/examples/simple-extension/Cargo.toml index 8deb234cd..a7304a4a5 100644 --- a/examples/simple-extension/Cargo.toml +++ b/examples/simple-extension/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["cdylib"] [dependencies] numpy = { path = "../.." } ndarray = "0.14" +num-complex = "0.3" [dependencies.pyo3] version = "0.13" diff --git a/examples/simple-extension/src/lib.rs b/examples/simple-extension/src/lib.rs index 855001604..67953cf12 100644 --- a/examples/simple-extension/src/lib.rs +++ b/examples/simple-extension/src/lib.rs @@ -1,5 +1,5 @@ use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD}; -use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn}; +use numpy::{c64, IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn}; use pyo3::prelude::{pymodule, PyModule, PyResult, Python}; #[pymodule] @@ -14,13 +14,18 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> { x *= a; } + // complex example + fn conj(x: ArrayViewD<'_, c64>) -> ArrayD { + x.map(|c| c.conj()) + } + // wrapper of `axpy` #[pyfn(m, "axpy")] fn axpy_py<'py>( py: Python<'py>, a: f64, - x: PyReadonlyArrayDyn, - y: PyReadonlyArrayDyn, + x: PyReadonlyArrayDyn<'_, f64>, + y: PyReadonlyArrayDyn<'_, f64>, ) -> &'py PyArrayDyn { let x = x.as_array(); let y = y.as_array(); @@ -29,10 +34,15 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> { // wrapper of `mult` #[pyfn(m, "mult")] - fn mult_py(_py: Python<'_>, a: f64, x: &PyArrayDyn) -> PyResult<()> { + fn mult_py(a: f64, x: &PyArrayDyn) { let x = unsafe { x.as_array_mut() }; mult(a, x); - Ok(()) + } + + // wrapper of `conj` + #[pyfn(m, "conj")] + fn conj_py<'py>(py: Python<'py>, x: PyReadonlyArrayDyn<'_, c64>) -> &'py PyArrayDyn { + conj(x.as_array()).into_pyarray(py) } Ok(()) diff --git a/examples/simple-extension/tests/test_ext.py b/examples/simple-extension/tests/test_ext.py index 655eb3300..11f7d9b41 100644 --- a/examples/simple-extension/tests/test_ext.py +++ b/examples/simple-extension/tests/test_ext.py @@ -1,5 +1,5 @@ import numpy as np -from rust_ext import axpy, mult +from rust_ext import axpy, conj, mult def test_axpy(): @@ -17,3 +17,8 @@ def test_mult(): x = np.array([1.0, 2.0, 3.0]) mult(3.0, x) np.testing.assert_array_almost_equal(x, np.array([3.0, 6.0, 9.0])) + + +def test_conj(): + x = np.array([1.0 + 2j, 2.0 + 3j, 3.0 + 4j]) + np.testing.assert_array_almost_equal(conj(x), np.conj(x)) diff --git a/src/lib.rs b/src/lib.rs old mode 100755 new mode 100644