@@ -23,7 +23,7 @@ use crate::borrow::{PyReadonlyArray, PyReadwriteArray};
23
23
use crate :: cold;
24
24
use crate :: convert:: { ArrayExt , IntoPyArray , NpyIndex , ToNpyDims , ToPyArray } ;
25
25
use crate :: dtype:: { Element , PyArrayDescr } ;
26
- use crate :: error:: { DimensionalityError , FromVecError , NotContiguousError , TypeError } ;
26
+ use crate :: error:: { BorrowError , DimensionalityError , FromVecError , NotContiguousError , TypeError } ;
27
27
use crate :: npyffi:: { self , npy_intp, NPY_ORDER , PY_ARRAY_API } ;
28
28
use crate :: slice_container:: PySliceContainer ;
29
29
@@ -846,13 +846,33 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
846
846
}
847
847
848
848
/// Get an immutable borrow of the NumPy array
849
+ pub fn try_readonly ( & self ) -> Result < PyReadonlyArray < ' _ , T , D > , BorrowError > {
850
+ PyReadonlyArray :: try_new ( self )
851
+ }
852
+
853
+ /// Get an immutable borrow of the NumPy array
854
+ ///
855
+ /// # Panics
856
+ ///
857
+ /// Panics if the allocation backing the array is currently mutably borrowed.
858
+ /// For a non-panicking variant, use [`try_readonly`][Self::try_readonly].
849
859
pub fn readonly ( & self ) -> PyReadonlyArray < ' _ , T , D > {
850
- PyReadonlyArray :: try_new ( self ) . unwrap ( )
860
+ self . try_readonly ( ) . unwrap ( )
851
861
}
852
862
853
863
/// Get a mutable borrow of the NumPy array
864
+ pub fn try_readwrite ( & self ) -> Result < PyReadwriteArray < ' _ , T , D > , BorrowError > {
865
+ PyReadwriteArray :: try_new ( self )
866
+ }
867
+
868
+ /// Get a mutable borrow of the NumPy array
869
+ ///
870
+ /// # Panics
871
+ ///
872
+ /// Panics if the allocation backing the array is currently borrowed.
873
+ /// For a non-panicking variant, use [`try_readwrite`][Self::try_readwrite].
854
874
pub fn readwrite ( & self ) -> PyReadwriteArray < ' _ , T , D > {
855
- PyReadwriteArray :: try_new ( self ) . unwrap ( )
875
+ self . try_readwrite ( ) . unwrap ( )
856
876
}
857
877
858
878
/// Returns the internal array as [`ArrayView`].
0 commit comments