@@ -19,13 +19,11 @@ use pyo3::{
19
19
Python , ToPyObject ,
20
20
} ;
21
21
22
+ use crate :: borrow:: { PyReadonlyArray , PyReadwriteArray } ;
22
23
use crate :: convert:: { ArrayExt , IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
23
24
use crate :: dtype:: { Element , PyArrayDescr } ;
24
25
use crate :: error:: { DimensionalityError , FromVecError , NotContiguousError , TypeError } ;
25
26
use crate :: npyffi:: { self , npy_intp, NPY_ORDER , PY_ARRAY_API } ;
26
- #[ allow( deprecated) ]
27
- use crate :: npyiter:: { NpySingleIter , NpySingleIterBuilder , ReadWrite } ;
28
- use crate :: readonly:: PyReadonlyArray ;
29
27
use crate :: slice_container:: PySliceContainer ;
30
28
31
29
/// A safe, static-typed interface for
@@ -194,18 +192,8 @@ impl<T, D> PyArray<T, D> {
194
192
}
195
193
196
194
#[ inline( always) ]
197
- fn check_flag ( & self , flag : c_int ) -> bool {
198
- unsafe { * self . as_array_ptr ( ) } . flags & flag == flag
199
- }
200
-
201
- #[ inline( always) ]
202
- pub ( crate ) fn get_flag ( & self ) -> c_int {
203
- unsafe { * self . as_array_ptr ( ) } . flags
204
- }
205
-
206
- /// Returns a temporally unwriteable reference of the array.
207
- pub fn readonly ( & self ) -> PyReadonlyArray < T , D > {
208
- self . into ( )
195
+ pub ( crate ) fn check_flags ( & self , flags : c_int ) -> bool {
196
+ unsafe { * self . as_array_ptr ( ) } . flags & flags != 0
209
197
}
210
198
211
199
/// Returns `true` if the internal data of the array is C-style contiguous
@@ -227,18 +215,17 @@ impl<T, D> PyArray<T, D> {
227
215
/// });
228
216
/// ```
229
217
pub fn is_contiguous ( & self ) -> bool {
230
- self . check_flag ( npyffi:: NPY_ARRAY_C_CONTIGUOUS )
231
- | self . check_flag ( npyffi:: NPY_ARRAY_F_CONTIGUOUS )
218
+ self . check_flags ( npyffi:: NPY_ARRAY_C_CONTIGUOUS | npyffi:: NPY_ARRAY_F_CONTIGUOUS )
232
219
}
233
220
234
221
/// Returns `true` if the internal data of the array is Fortran-style contiguous.
235
222
pub fn is_fortran_contiguous ( & self ) -> bool {
236
- self . check_flag ( npyffi:: NPY_ARRAY_F_CONTIGUOUS )
223
+ self . check_flags ( npyffi:: NPY_ARRAY_F_CONTIGUOUS )
237
224
}
238
225
239
226
/// Returns `true` if the internal data of the array is C-style contiguous.
240
227
pub fn is_c_contiguous ( & self ) -> bool {
241
- self . check_flag ( npyffi:: NPY_ARRAY_C_CONTIGUOUS )
228
+ self . check_flags ( npyffi:: NPY_ARRAY_C_CONTIGUOUS )
242
229
}
243
230
244
231
/// Get `Py<PyArray>` from `&PyArray`, which is the owned wrapper of PyObject.
@@ -827,28 +814,37 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
827
814
ToPyArray :: to_pyarray ( arr, py)
828
815
}
829
816
830
- /// Get the immutable view of the internal data of `PyArray`, as
831
- /// [`ndarray::ArrayView`](https://docs.rs/ndarray/latest/ndarray/type.ArrayView.html).
817
+ /// Get an immutable borrow of the NumPy array
818
+ pub fn readonly ( & self ) -> PyReadonlyArray < ' _ , T , D > {
819
+ PyReadonlyArray :: try_new ( self ) . unwrap ( )
820
+ }
821
+
822
+ /// Get a mutable borrow of the NumPy array
823
+ pub fn readwrite ( & self ) -> PyReadwriteArray < ' _ , T , D > {
824
+ PyReadwriteArray :: try_new ( self ) . unwrap ( )
825
+ }
826
+
827
+ /// Returns the internal array as [`ArrayView`].
832
828
///
833
- /// Please consider the use of safe alternatives
834
- /// ([`PyReadonlyArray::as_array`](../struct.PyReadonlyArray.html#method.as_array)
835
- /// or [`to_array`](#method.to_array)) instead of this.
829
+ /// See also [`PyArrayRef::as_array`].
836
830
///
837
831
/// # Safety
838
- /// If the internal array is not readonly and can be mutated from Python code,
839
- /// holding the `ArrayView` might cause undefined behavior.
832
+ ///
833
+ /// The existence of an exclusive reference to the internal data, e.g. `&mut [T]` or `ArrayViewMut`, implies undefined behavior.
840
834
pub unsafe fn as_array ( & self ) -> ArrayView < ' _ , T , D > {
841
835
let ( shape, ptr, inverted_axes) = self . ndarray_shape_ptr ( ) ;
842
836
let mut res = ArrayView :: from_shape_ptr ( shape, ptr) ;
843
837
inverted_axes. invert ( & mut res) ;
844
838
res
845
839
}
846
840
847
- /// Returns the internal array as [`ArrayViewMut`]. See also [`as_array`](#method.as_array).
841
+ /// Returns the internal array as [`ArrayViewMut`].
842
+ ///
843
+ /// See also [`PyArrayRefMut::as_array_mut`].
848
844
///
849
845
/// # Safety
850
- /// If another reference to the internal data exists(e.g., `&[T]` or `ArrayView`),
851
- /// it might cause undefined behavior.
846
+ ///
847
+ /// The existence of another reference to the internal data, e.g. `&[T]` or `ArrayView`, implies undefined behavior.
852
848
pub unsafe fn as_array_mut ( & self ) -> ArrayViewMut < ' _ , T , D > {
853
849
let ( shape, ptr, inverted_axes) = self . ndarray_shape_ptr ( ) ;
854
850
let mut res = ArrayViewMut :: from_shape_ptr ( shape, ptr) ;
@@ -924,7 +920,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
924
920
///
925
921
/// let pyarray = PyArray::from_owned_object_array(py, array);
926
922
///
927
- /// assert!(pyarray.readonly().get(0).unwrap().as_ref(py).is_instance_of::<CustomElement>().unwrap());
923
+ /// assert!(pyarray.readonly().as_array(). get(0).unwrap().as_ref(py).is_instance_of::<CustomElement>().unwrap());
928
924
/// });
929
925
/// ```
930
926
pub fn from_owned_object_array < ' py , T > ( py : Python < ' py > , arr : Array < Py < T > , D > ) -> & ' py Self {
@@ -1073,21 +1069,6 @@ impl<T: Element> PyArray<T, Ix1> {
1073
1069
self . resize_ ( self . py ( ) , [ new_elems] , 1 , NPY_ORDER :: NPY_ANYORDER )
1074
1070
}
1075
1071
1076
- /// Iterates all elements of this array.
1077
- /// See [NpySingleIter](../npyiter/struct.NpySingleIter.html) for more.
1078
- ///
1079
- /// # Safety
1080
- ///
1081
- /// The iterator will produce mutable references into the array which must not be
1082
- /// aliased by other references for the life time of the iterator.
1083
- #[ deprecated(
1084
- note = "The wrappers of the array iterator API are deprecated, please use ndarray's `ArrayBase::iter_mut` instead."
1085
- ) ]
1086
- #[ allow( deprecated) ]
1087
- pub unsafe fn iter < ' py > ( & ' py self ) -> PyResult < NpySingleIter < ' py , T , ReadWrite > > {
1088
- NpySingleIterBuilder :: readwrite ( self ) . build ( )
1089
- }
1090
-
1091
1072
fn resize_ < D : IntoDimension > (
1092
1073
& self ,
1093
1074
py : Python ,
0 commit comments