Skip to content

Commit d80f2a3

Browse files
committed
Modify docs
1 parent 8485d97 commit d80f2a3

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/array.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,26 @@ impl<T: TypeNum> PyArray<T> {
443443
}
444444
}
445445

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+
/// ```
447460
pub fn as_array(&self) -> Result<ArrayViewD<T>, ErrorKind> {
448461
self.type_check()?;
449462
unsafe { Ok(ArrayView::from_shape_ptr(self.ndarray_shape(), self.data())) }
450463
}
451464

452-
/// Get data as a ndarray::ArrayViewMut
465+
/// Get the mmutable view of the internal data of `PyArray`, as `ndarray::ArrayViewMut`.
453466
pub fn as_array_mut(&self) -> Result<ArrayViewMutD<T>, ErrorKind> {
454467
self.type_check()?;
455468
unsafe {
@@ -460,23 +473,22 @@ impl<T: TypeNum> PyArray<T> {
460473
}
461474
}
462475

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.
465477
/// # Example
466478
/// ```
467479
/// # extern crate pyo3; extern crate numpy; fn main() {
468480
/// use numpy::PyArray;
469481
/// 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]);
472484
/// # }
473485
/// ```
474486
pub fn as_slice(&self) -> Result<&[T], ErrorKind> {
475487
self.type_check()?;
476488
unsafe { Ok(::std::slice::from_raw_parts(self.data(), self.len())) }
477489
}
478490

479-
/// Get data as a Rust mutable slice
491+
/// Get the mmutable view of the internal data of `PyArray`, as slice.
480492
pub fn as_slice_mut(&self) -> Result<&mut [T], ErrorKind> {
481493
self.type_check()?;
482494
unsafe { Ok(::std::slice::from_raw_parts_mut(self.data(), self.len())) }

src/convert.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Defines conversion trait between rust types and numpy data types.
1+
//! Defines conversion traits between rust types and numpy data types.
22
33
use ndarray::{ArrayBase, Data, Dimension};
44
use pyo3::Python;
@@ -9,6 +9,8 @@ use super::*;
99

1010
/// Covversion trait from rust types to `PyArray`.
1111
///
12+
/// This trait takes `&self`, which means **it alocates in Python heap and then copies
13+
/// elements there**.
1214
/// # Example
1315
/// ```
1416
/// # extern crate pyo3; extern crate numpy; fn main() {

0 commit comments

Comments
 (0)