Skip to content

Commit 53df004

Browse files
committed
Deprecate PyArray::from_exact_iter as it does not really add anything on top of PyArray::from_iter.
1 parent a80e9c7 commit 53df004

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
- Unreleased
4+
- Deprecate `PyArray::from_exact_iter` after optimizing `PyArray::from_iter`. ([#292](https://github.com/PyO3/rust-numpy/pull/292))
45

56
- v0.16.2
67
- Fix build on platforms where `c_char` is `u8` like Linux/AArch64. ([#296](https://github.com/PyO3/rust-numpy/pull/296))

benches/array.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn from_exact_iter(bencher: &mut Bencher, size: usize) {
6565
iter_with_gil(bencher, |py| {
6666
let iter = black_box(ExactIter(0..size));
6767

68+
#[allow(deprecated)]
6869
PyArray1::from_exact_iter(py, iter);
6970
});
7071
}

src/array.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -984,34 +984,45 @@ impl<T: Element> PyArray<T, Ix1> {
984984
/// # Example
985985
/// ```
986986
/// use numpy::PyArray;
987-
/// use std::collections::BTreeSet;
988-
/// let vec = vec![1, 2, 3, 4, 5];
989-
/// pyo3::Python::with_gil(|py| {
990-
/// let pyarray = PyArray::from_exact_iter(py, vec.iter().map(|&x| x));
987+
/// use pyo3::Python;
988+
///
989+
/// Python::with_gil(|py| {
990+
/// let pyarray = PyArray::from_exact_iter(py, [1, 2, 3, 4, 5].into_iter().copied());
991991
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
992992
/// });
993993
/// ```
994-
pub fn from_exact_iter(py: Python<'_>, iter: impl ExactSizeIterator<Item = T>) -> &Self {
995-
let data = iter.collect::<Box<[_]>>();
996-
data.into_pyarray(py)
994+
#[deprecated(
995+
note = "`from_exact_iter` is deprecated as it does not provide any benefit over `from_iter`."
996+
)]
997+
#[inline(always)]
998+
pub fn from_exact_iter<I>(py: Python<'_>, iter: I) -> &Self
999+
where
1000+
I: IntoIterator<Item = T>,
1001+
I::IntoIter: ExactSizeIterator,
1002+
{
1003+
Self::from_iter(py, iter)
9971004
}
9981005

999-
/// Construct one-dimension PyArray from a type which implements
1000-
/// [`IntoIterator`](https://doc.rust-lang.org/std/iter/trait.IntoIterator.html).
1006+
/// Construct one-dimension PyArray from a type which implements [`IntoIterator`].
10011007
///
1002-
/// If no reliable [`size_hint`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.size_hint) is available,
1008+
/// If no reliable [`size_hint`][Iterator::size_hint] is available,
10031009
/// this method can allocate memory multiple time, which can hurt performance.
10041010
///
10051011
/// # Example
1012+
///
10061013
/// ```
10071014
/// use numpy::PyArray;
1008-
/// let set: std::collections::BTreeSet<u32> = [4, 3, 2, 5, 1].into_iter().cloned().collect();
1009-
/// pyo3::Python::with_gil(|py| {
1010-
/// let pyarray = PyArray::from_iter(py, set);
1011-
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
1015+
/// use pyo3::Python;
1016+
///
1017+
/// Python::with_gil(|py| {
1018+
/// let pyarray = PyArray::from_iter(py, "abcde".chars().map(u32::from));
1019+
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[97, 98, 99, 100, 101]);
10121020
/// });
10131021
/// ```
1014-
pub fn from_iter(py: Python<'_>, iter: impl IntoIterator<Item = T>) -> &Self {
1022+
pub fn from_iter<I>(py: Python<'_>, iter: I) -> &Self
1023+
where
1024+
I: IntoIterator<Item = T>,
1025+
{
10151026
let data = iter.into_iter().collect::<Vec<_>>();
10161027
data.into_pyarray(py)
10171028
}

tests/to_py.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn long_iter_to_pyarray() {
6565
#[test]
6666
fn exact_iter_to_pyarray() {
6767
Python::with_gil(|py| {
68+
#[allow(deprecated)]
6869
let arr = PyArray::from_exact_iter(py, 0_u32..512);
6970

7071
assert_eq!(

0 commit comments

Comments
 (0)