Skip to content

Commit 6b19b6d

Browse files
committed
Expose resize as public API
1 parent f3563fb commit 6b19b6d

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/array.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,35 @@ impl<T: TypeNum> PyArray<T, Ix1> {
530530
if length >= capacity {
531531
capacity *= 2;
532532
array
533-
.resize_([capacity], 0, NPY_ORDER::NPY_ANYORDER)
533+
.resize(capacity)
534534
.expect("PyArray::from_iter: Failed to allocate memory");
535535
}
536536
*array.uget_mut([i]) = item;
537537
}
538538
}
539539
if capacity > length {
540-
array.resize_([length], 0, NPY_ORDER::NPY_ANYORDER).unwrap()
540+
array.resize(length).unwrap()
541541
}
542542
array
543543
}
544544

545-
// TODO: expose?
545+
/// Extends or trancates the length of 1 dimension PyArray.
546+
///
547+
/// # Example
548+
/// ```
549+
/// # extern crate pyo3; extern crate numpy; fn main() {
550+
/// use numpy::PyArray;
551+
/// let gil = pyo3::Python::acquire_gil();
552+
/// let pyarray = PyArray::arange(gil.python(), 0, 10, 1);
553+
/// assert_eq!(pyarray.len(), 10);
554+
/// pyarray.resize(100).unwrap();
555+
/// assert_eq!(pyarray.len(), 100);
556+
/// # }
557+
/// ```
558+
pub fn resize(&self, new_elems: usize) -> Result<(), ErrorKind> {
559+
self.resize_([new_elems], 1, NPY_ORDER::NPY_ANYORDER)
560+
}
561+
546562
fn resize_<'py, D: IntoDimension>(
547563
&'py self,
548564
dims: D,

0 commit comments

Comments
 (0)