Skip to content

Commit 3ad4272

Browse files
committed
Fix doc examples where we use array.as_slice
1 parent 2b411d2 commit 3ad4272

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/array.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<T, D> PyArray<T, D> {
155155
| self.check_flag(npyffi::NPY_ARRAY_F_CONTIGUOUS)
156156
}
157157

158-
pub fn is_forran_contiguous(&self) -> bool {
158+
pub fn is_fotran_contiguous(&self) -> bool {
159159
self.check_flag(npyffi::NPY_ARRAY_F_CONTIGUOUS)
160160
}
161161

@@ -180,7 +180,7 @@ impl<T, D> PyArray<T, D> {
180180
/// }
181181
/// let gil = Python::acquire_gil();
182182
/// let array = return_py_array();
183-
/// assert_eq!(array.as_ref(gil.python()).as_slice(), &[0, 0, 0, 0, 0]);
183+
/// assert_eq!(array.as_ref(gil.python()).as_slice().unwrap(), &[0, 0, 0, 0, 0]);
184184
/// # }
185185
/// ```
186186
pub fn to_owned(&self) -> Py<Self> {
@@ -413,13 +413,24 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
413413
}
414414

415415
/// Get the immutable view of the internal data of `PyArray`, as slice.
416+
///
417+
/// Returns `ErrorKind::NotContiguous` if the internal array is not contiguous.
416418
/// # Example
417419
/// ```
418420
/// # extern crate pyo3; extern crate numpy; fn main() {
419-
/// use numpy::PyArray;
421+
/// use numpy::{PyArray, PyArray1};
422+
/// use pyo3::types::IntoPyDict;
420423
/// let gil = pyo3::Python::acquire_gil();
421-
/// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
422-
/// assert_eq!(py_array.as_slice(), &[0, 1, 2, 3]);
424+
/// let py = gil.python();
425+
/// let py_array = PyArray::arange(py, 0, 4, 1).reshape([2, 2]).unwrap();
426+
/// assert_eq!(py_array.as_slice().unwrap(), &[0, 1, 2, 3]);
427+
/// let locals = [("np", numpy::get_array_module(py).unwrap())].into_py_dict(py);
428+
/// let not_contiguous: &PyArray1<f32> = py
429+
/// .eval("np.zeros(10)[::2]", Some(locals), None)
430+
/// .unwrap()
431+
/// .downcast_ref()
432+
/// .unwrap();
433+
/// assert!(not_contiguous.as_slice().is_err());
423434
/// # }
424435
/// ```
425436
pub fn as_slice(&self) -> Result<&[T], ErrorKind> {
@@ -654,7 +665,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
654665
/// let gil = pyo3::Python::acquire_gil();
655666
/// let array = [1, 2, 3, 4, 5];
656667
/// let pyarray = PyArray::from_slice(gil.python(), &array);
657-
/// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
668+
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
658669
/// # }
659670
/// ```
660671
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
@@ -675,7 +686,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
675686
/// let gil = pyo3::Python::acquire_gil();
676687
/// let vec = vec![1, 2, 3, 4, 5];
677688
/// let pyarray = PyArray::from_vec(gil.python(), vec);
678-
/// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
689+
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
679690
/// # }
680691
/// ```
681692
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
@@ -693,7 +704,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
693704
/// let gil = pyo3::Python::acquire_gil();
694705
/// let vec = vec![1, 2, 3, 4, 5];
695706
/// let pyarray = PyArray::from_iter(gil.python(), vec.iter().map(|&x| x));
696-
/// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
707+
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
697708
/// # }
698709
/// ```
699710
pub fn from_exact_iter(py: Python, iter: impl ExactSizeIterator<Item = T>) -> &Self {
@@ -720,7 +731,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
720731
/// let gil = pyo3::Python::acquire_gil();
721732
/// let set: BTreeSet<u32> = [4, 3, 2, 5, 1].into_iter().cloned().collect();
722733
/// let pyarray = PyArray::from_iter(gil.python(), set);
723-
/// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
734+
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
724735
/// # }
725736
/// ```
726737
pub fn from_iter(py: Python, iter: impl IntoIterator<Item = T>) -> &Self {
@@ -895,7 +906,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
895906
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
896907
/// let pyarray_i = PyArray::<i64, _>::new(gil.python(), [3], false);
897908
/// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
898-
/// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
909+
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
899910
/// # }
900911
pub fn copy_to<U: TypeNum>(&self, other: &PyArray<U, D>) -> Result<(), ErrorKind> {
901912
let self_ptr = self.as_array_ptr();
@@ -916,7 +927,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
916927
/// let gil = pyo3::Python::acquire_gil();
917928
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
918929
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
919-
/// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
930+
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
920931
/// # }
921932
pub fn cast<'py, U: TypeNum>(
922933
&'py self,
@@ -1003,9 +1014,9 @@ impl<T: TypeNum + AsPrimitive<f64>> PyArray<T, Ix1> {
10031014
/// use numpy::PyArray;
10041015
/// let gil = pyo3::Python::acquire_gil();
10051016
/// let pyarray = PyArray::arange(gil.python(), 2.0, 4.0, 0.5);
1006-
/// assert_eq!(pyarray.as_slice(), &[2.0, 2.5, 3.0, 3.5]);
1017+
/// assert_eq!(pyarray.as_slice().unwrap(), &[2.0, 2.5, 3.0, 3.5]);
10071018
/// let pyarray = PyArray::arange(gil.python(), -2, 4, 3);
1008-
/// assert_eq!(pyarray.as_slice(), &[-2, 1]);
1019+
/// assert_eq!(pyarray.as_slice().unwrap(), &[-2, 1]);
10091020
/// # }
10101021
pub fn arange<'py>(py: Python<'py>, start: T, stop: T, step: T) -> &'py Self {
10111022
unsafe {

src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use npyffi::npy_intp;
2323
/// use numpy::{PyArray, IntoPyArray};
2424
/// let gil = pyo3::Python::acquire_gil();
2525
/// let py_array = vec![1, 2, 3].into_pyarray(gil.python());
26-
/// assert_eq!(py_array.as_slice(), &[1, 2, 3]);
26+
/// assert_eq!(py_array.as_slice().unwrap(), &[1, 2, 3]);
2727
/// assert!(py_array.resize(100).is_err()); // You can't resize owned-by-rust array.
2828
/// # }
2929
/// ```
@@ -75,7 +75,7 @@ where
7575
/// use numpy::{PyArray, ToPyArray};
7676
/// let gil = pyo3::Python::acquire_gil();
7777
/// let py_array = vec![1, 2, 3].to_pyarray(gil.python());
78-
/// assert_eq!(py_array.as_slice(), &[1, 2, 3]);
78+
/// assert_eq!(py_array.as_slice().unwrap(), &[1, 2, 3]);
7979
/// # }
8080
/// ```
8181
pub trait ToPyArray {

src/npyffi/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const CAPSULE_NAME: &str = "_ARRAY_API";
2828
/// unsafe {
2929
/// PY_ARRAY_API.PyArray_Sort(array.as_array_ptr(), 0, NPY_SORTKIND::NPY_QUICKSORT);
3030
/// }
31-
/// assert_eq!(array.as_slice(), &[2, 3, 4])
31+
/// assert_eq!(array.as_slice().unwrap(), &[2, 3, 4])
3232
/// # }
3333
/// ```
3434
pub static PY_ARRAY_API: PyArrayAPI = PyArrayAPI {

0 commit comments

Comments
 (0)