|
1 | 1 | //! Safe interface for NumPy ndarray
|
2 | 2 | use ndarray::*;
|
3 |
| -use npyffi::{self, npy_intp, PY_ARRAY_API, NPY_ORDER}; |
| 3 | +use npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API}; |
4 | 4 | use num_traits::AsPrimitive;
|
5 | 5 | use pyo3::*;
|
6 | 6 | use std::iter::ExactSizeIterator;
|
@@ -799,28 +799,6 @@ impl<T: TypeNum, D> PyArray<T, D> {
|
799 | 799 | }
|
800 | 800 | }
|
801 | 801 |
|
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 |
| - |
824 | 802 | /// Cast the `PyArray<T>` to `PyArray<U>`, by allocating a new array.
|
825 | 803 | /// # Example
|
826 | 804 | /// ```
|
@@ -904,6 +882,35 @@ impl<T: TypeNum, D> PyArray<T, D> {
|
904 | 882 | }
|
905 | 883 | }
|
906 | 884 |
|
| 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 | + |
907 | 914 | impl<T: TypeNum + AsPrimitive<f64>> PyArray<T, Ix1> {
|
908 | 915 | /// Return evenly spaced values within a given interval.
|
909 | 916 | /// Same as [numpy.arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html).
|
|
0 commit comments