@@ -443,13 +443,26 @@ impl<T: TypeNum> PyArray<T> {
443
443
}
444
444
}
445
445
446
- /// Get data as a ndarray::ArrayView
446
+ /// Get the immutable view of the internal data of `PyArray`, as `ndarray::ArrayView`.
447
+ ///
448
+ /// # Example
449
+ /// ```
450
+ /// # #[macro_use] extern crate ndarray; extern crate pyo3; extern crate numpy; fn main() {
451
+ /// use numpy::PyArray;
452
+ /// let gil = pyo3::Python::acquire_gil();
453
+ /// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
454
+ /// assert_eq!(
455
+ /// py_array.as_array().unwrap(),
456
+ /// array![[0, 1], [2, 3]].into_dyn()
457
+ /// )
458
+ /// # }
459
+ /// ```
447
460
pub fn as_array ( & self ) -> Result < ArrayViewD < T > , ErrorKind > {
448
461
self . type_check ( ) ?;
449
462
unsafe { Ok ( ArrayView :: from_shape_ptr ( self . ndarray_shape ( ) , self . data ( ) ) ) }
450
463
}
451
464
452
- /// Get data as a ndarray::ArrayViewMut
465
+ /// Get the mmutable view of the internal data of `PyArray`, as ` ndarray::ArrayViewMut`.
453
466
pub fn as_array_mut ( & self ) -> Result < ArrayViewMutD < T > , ErrorKind > {
454
467
self . type_check ( ) ?;
455
468
unsafe {
@@ -460,23 +473,22 @@ impl<T: TypeNum> PyArray<T> {
460
473
}
461
474
}
462
475
463
- /// Get the immutable view of the internal data of numpy, as slice.
464
- ///
476
+ /// Get the immutable view of the internal data of `PyArray`, as slice.
465
477
/// # Example
466
478
/// ```
467
479
/// # extern crate pyo3; extern crate numpy; fn main() {
468
480
/// use numpy::PyArray;
469
481
/// let gil = pyo3::Python::acquire_gil();
470
- /// let py_array = PyArray::arange()
471
- /// assert_eq!(py_array.as_slice().unwrap(), &[1, 2, 3]);
482
+ /// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
483
+ /// assert_eq!(py_array.as_slice().unwrap(), &[0, 1, 2, 3]);
472
484
/// # }
473
485
/// ```
474
486
pub fn as_slice ( & self ) -> Result < & [ T ] , ErrorKind > {
475
487
self . type_check ( ) ?;
476
488
unsafe { Ok ( :: std:: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) ) }
477
489
}
478
490
479
- /// Get data as a Rust mutable slice
491
+ /// Get the mmutable view of the internal data of `PyArray`, as slice.
480
492
pub fn as_slice_mut ( & self ) -> Result < & mut [ T ] , ErrorKind > {
481
493
self . type_check ( ) ?;
482
494
unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
0 commit comments