@@ -455,4 +455,64 @@ impl<T: TypeNum> PyArray<T> {
455
455
Self :: from_owned_ptr ( py, ptr)
456
456
}
457
457
}
458
+
459
+ /// Copies self into `other`, performing a data-type conversion if necessary.
460
+ /// # Example
461
+ /// ```
462
+ /// # extern crate pyo3; extern crate numpy; fn main() {
463
+ /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
464
+ /// let gil = pyo3::Python::acquire_gil();
465
+ /// let np = PyArrayModule::import(gil.python()).unwrap();
466
+ /// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
467
+ /// let mut pyarray_i = PyArray::<i64>::new(gil.python(), &np, &[3]);
468
+ /// assert!(pyarray_f.copy_to(&np, &mut pyarray_i).is_ok());
469
+ /// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
470
+ /// # }
471
+ pub fn copy_to < U : TypeNum > (
472
+ & self ,
473
+ np : & PyArrayModule ,
474
+ other : & mut PyArray < U > ,
475
+ ) -> Result < ( ) , ArrayCastError > {
476
+ let self_ptr = self . as_array_ptr ( ) ;
477
+ let other_ptr = other. as_array_ptr ( ) ;
478
+ let result = unsafe { np. PyArray_CopyInto ( other_ptr, self_ptr) } ;
479
+ if result == -1 {
480
+ Err ( ArrayCastError :: Numpy {
481
+ from : T :: npy_data_type ( ) ,
482
+ to : U :: npy_data_type ( ) ,
483
+ } )
484
+ } else {
485
+ Ok ( ( ) )
486
+ }
487
+ }
488
+
489
+ /// Move the data of self into `other`, performing a data-type conversion if necessary.
490
+ /// # Example
491
+ /// ```
492
+ /// # extern crate pyo3; extern crate numpy; fn main() {
493
+ /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
494
+ /// let gil = pyo3::Python::acquire_gil();
495
+ /// let np = PyArrayModule::import(gil.python()).unwrap();
496
+ /// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
497
+ /// let mut pyarray_i = PyArray::<i64>::new(gil.python(), &np, &[3]);
498
+ /// assert!(pyarray_f.move_to(&np, &mut pyarray_i).is_ok());
499
+ /// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
500
+ /// # }
501
+ pub fn move_to < U : TypeNum > (
502
+ self ,
503
+ np : & PyArrayModule ,
504
+ other : & mut PyArray < U > ,
505
+ ) -> Result < ( ) , ArrayCastError > {
506
+ let self_ptr = self . as_array_ptr ( ) ;
507
+ let other_ptr = other. as_array_ptr ( ) ;
508
+ let result = unsafe { np. PyArray_MoveInto ( other_ptr, self_ptr) } ;
509
+ if result == -1 {
510
+ Err ( ArrayCastError :: Numpy {
511
+ from : T :: npy_data_type ( ) ,
512
+ to : U :: npy_data_type ( ) ,
513
+ } )
514
+ } else {
515
+ Ok ( ( ) )
516
+ }
517
+ }
458
518
}
0 commit comments