Skip to content

Commit bc271a2

Browse files
committed
Change the trait bound of move_to
1 parent 964b320 commit bc271a2

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/array.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Safe interface for NumPy ndarray
22
use ndarray::*;
3-
use npyffi::{self, npy_intp, PY_ARRAY_API, NPY_ORDER};
3+
use npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API};
44
use num_traits::AsPrimitive;
55
use pyo3::*;
66
use std::iter::ExactSizeIterator;
@@ -799,28 +799,6 @@ impl<T: TypeNum, D> PyArray<T, D> {
799799
}
800800
}
801801

802-
/// Move the data of self into `other`, performing a data-type conversion if necessary.
803-
/// # Example
804-
/// ```
805-
/// # extern crate pyo3; extern crate numpy; fn main() {
806-
/// use numpy::PyArray;
807-
/// let gil = pyo3::Python::acquire_gil();
808-
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
809-
/// let pyarray_i = PyArray::<i64, _>::new(gil.python(), [3], false);
810-
/// assert!(pyarray_f.move_to(pyarray_i).is_ok());
811-
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
812-
/// # }
813-
pub fn move_to<U: TypeNum>(&self, other: &PyArray<U, D>) -> Result<(), ErrorKind> {
814-
let self_ptr = self.as_array_ptr();
815-
let other_ptr = other.as_array_ptr();
816-
let result = unsafe { PY_ARRAY_API.PyArray_MoveInto(other_ptr, self_ptr) };
817-
if result == -1 {
818-
Err(ErrorKind::dtype_cast(self, U::npy_data_type()))
819-
} else {
820-
Ok(())
821-
}
822-
}
823-
824802
/// Cast the `PyArray<T>` to `PyArray<U>`, by allocating a new array.
825803
/// # Example
826804
/// ```
@@ -904,6 +882,35 @@ impl<T: TypeNum, D> PyArray<T, D> {
904882
}
905883
}
906884

885+
impl<T: TypeNum> PyArray<T, IxDyn> {
886+
/// Move the data of self into `other`, performing a data-type conversion if necessary.
887+
///
888+
/// For type safety, you have to convert `PyArray` to `PyArrayDyn` before using this method.
889+
/// # Example
890+
/// ```
891+
/// # extern crate pyo3; extern crate numpy; fn main() {
892+
/// use numpy::PyArray;
893+
/// let gil = pyo3::Python::acquire_gil();
894+
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0).into_dyn();
895+
/// let pyarray_i = PyArray::<i64, _>::new(gil.python(), [3], false);
896+
/// assert!(pyarray_f.move_to(pyarray_i).is_ok());
897+
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
898+
/// # }
899+
pub fn move_to<U: TypeNum, D2: Dimension>(
900+
&self,
901+
other: &PyArray<U, D2>,
902+
) -> Result<(), ErrorKind> {
903+
let self_ptr = self.as_array_ptr();
904+
let other_ptr = other.as_array_ptr();
905+
let result = unsafe { PY_ARRAY_API.PyArray_MoveInto(other_ptr, self_ptr) };
906+
if result == -1 {
907+
Err(ErrorKind::dtype_cast(self, U::npy_data_type()))
908+
} else {
909+
Ok(())
910+
}
911+
}
912+
}
913+
907914
impl<T: TypeNum + AsPrimitive<f64>> PyArray<T, Ix1> {
908915
/// Return evenly spaced values within a given interval.
909916
/// Same as [numpy.arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html).

0 commit comments

Comments
 (0)