Skip to content

Commit 60265d3

Browse files
committed
Deprecate the wrappers of the array iterator API.
1 parent d8ff63a commit 60265d3

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fixed downcasting ignoring element type and dimensionality ([#265](https://github.com/PyO3/rust-numpy/pull/265))
88
- `PyArray::new` is now `unsafe`, as it produces uninitialized arrays ([#220](https://github.com/PyO3/rust-numpy/pull/220))
99
- `PyArray::iter`, `NpySingleIterBuilder::readwrite` and `NpyMultiIterBuilder::add_readwrite` are now `unsafe`, as they allow aliasing mutable references to be created ([#278/](https://github.com/PyO3/rust-numpy/pull/278))
10+
- The `npyiter` module is deprecated as rust-ndarray's facilities for iteration are more flexible and performant ([#280](https://github.com/PyO3/rust-numpy/pull/280))
1011
- `PyArray::from_exact_iter` does not unsoundly trust `ExactSizeIterator::len` any more ([#262](https://github.com/PyO3/rust-numpy/pull/262))
1112
- `PyArray::as_cell_slice` was removed as it unsoundly interacts with `PyReadonlyArray` allowing safe code to violate aliasing rules ([#260](https://github.com/PyO3/rust-numpy/pull/260))
1213
- `rayon` feature is now removed, and directly specifying the feature via `ndarray` dependency is recommended ([#250](https://github.com/PyO3/rust-numpy/pull/250))

src/array.rs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::convert::{ArrayExt, IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
2323
use crate::dtype::{Element, PyArrayDescr};
2424
use crate::error::{DimensionalityError, FromVecError, NotContiguousError, TypeError};
2525
use crate::npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API};
26+
#[allow(deprecated)]
2627
use crate::npyiter::{NpySingleIter, NpySingleIterBuilder, ReadWrite};
2728
use crate::readonly::PyReadonlyArray;
2829
use crate::slice_container::PySliceContainer;
@@ -1079,6 +1080,10 @@ impl<T: Element> PyArray<T, Ix1> {
10791080
///
10801081
/// The iterator will produce mutable references into the array which must not be
10811082
/// aliased by other references for the life time of the iterator.
1083+
#[deprecated(
1084+
note = "The wrappers of the array iterator API are deprecated, please use ndarray's `ArrayBase::iter_mut` instead."
1085+
)]
1086+
#[allow(deprecated)]
10821087
pub unsafe fn iter<'py>(&'py self) -> PyResult<NpySingleIter<'py, T, ReadWrite>> {
10831088
NpySingleIterBuilder::readwrite(self).build()
10841089
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub use crate::convert::{IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
5656
pub use crate::dtype::{dtype, Complex32, Complex64, Element, PyArrayDescr};
5757
pub use crate::error::{DimensionalityError, FromVecError, NotContiguousError, TypeError};
5858
pub use crate::npyffi::{PY_ARRAY_API, PY_UFUNC_API};
59+
#[allow(deprecated)]
5960
pub use crate::npyiter::{
6061
IterMode, NpyIterFlag, NpyMultiIter, NpyMultiIterBuilder, NpySingleIter, NpySingleIterBuilder,
6162
};

src/npyiter.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
//! This module exposes two iterators:
44
//! [NpySingleIter](./struct.NpySingleIter.html) and
55
//! [NpyMultiIter](./struct.NpyMultiIter.html).
6+
#![deprecated(
7+
note = "The wrappers of the array iterator API are deprecated, please use ndarray's iterators like `Lanes` and `Zip` instead."
8+
)]
9+
10+
use pyo3::{prelude::*, PyNativeType};
11+
612
use crate::npyffi::{
713
array::PY_ARRAY_API,
814
npy_intp, npy_uint32,
@@ -14,7 +20,6 @@ use crate::npyffi::{
1420
NPY_ITER_ZEROSIZE_OK,
1521
};
1622
use crate::{Element, PyArray, PyArrayDyn, PyReadonlyArray};
17-
use pyo3::{prelude::*, PyNativeType};
1823

1924
use std::marker::PhantomData;
2025
use std::os::raw::*;

src/readonly.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Readonly arrays
2-
use crate::npyffi::NPY_ARRAY_WRITEABLE;
3-
use crate::{Element, NotContiguousError, NpyIndex, PyArray};
42
use ndarray::{ArrayView, Dimension, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn};
53
use pyo3::{prelude::*, types::PyAny, AsPyPointer};
64

5+
use crate::npyffi::NPY_ARRAY_WRITEABLE;
6+
#[allow(deprecated)]
7+
use crate::npyiter::{NpySingleIter, NpySingleIterBuilder, Readonly};
8+
use crate::{Element, NotContiguousError, NpyIndex, PyArray};
9+
710
/// Readonly reference of [`PyArray`](../array/struct.PyArray.html).
811
///
912
/// This struct ensures that the internal array is not writeable while holding `PyReadonlyArray`.
@@ -135,8 +138,12 @@ impl<'py, T: Element, D: Dimension> PyReadonlyArray<'py, T, D> {
135138

136139
/// Iterates all elements of this array.
137140
/// See [NpySingleIter](../npyiter/struct.NpySingleIter.html) for more.
138-
pub fn iter(self) -> PyResult<crate::NpySingleIter<'py, T, crate::npyiter::Readonly>> {
139-
crate::NpySingleIterBuilder::readonly(self).build()
141+
#[deprecated(
142+
note = "The wrappers of the array iterator API are deprecated, please use ndarray's `ArrayBase::iter` instead."
143+
)]
144+
#[allow(deprecated)]
145+
pub fn iter(self) -> PyResult<NpySingleIter<'py, T, Readonly>> {
146+
NpySingleIterBuilder::readonly(self).build()
140147
}
141148

142149
pub(crate) fn destruct(self) -> (&'py PyArray<T, D>, bool) {

tests/iter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(deprecated)]
2+
13
use ndarray::array;
24
use numpy::{NpyMultiIterBuilder, NpySingleIterBuilder, PyArray};
35
use pyo3::PyResult;

0 commit comments

Comments
 (0)