@@ -19,20 +19,21 @@ use types::{NpyDataType, TypeNum};
19
19
///
20
20
/// # Memory location
21
21
///
22
- /// 1.`PyArray` constructed via `IntoPyArray::into_pyarray` or `PyArray::from_vec`
23
- /// or `PyArray:: from_owned_array`
22
+ /// - Case1: Constructed via [ `IntoPyArray`](../convert/trait.IntoPyArray.html) or
23
+ /// [`from_vec`](#method.from_vec) or [` from_owned_array`](#method.from_owned_vec).
24
24
///
25
- /// These methods don't allocate and use `Box<[T]>` as container .
25
+ /// These methods don't allocate and use `Box<[T]>` as a internal buffer .
26
26
///
27
27
/// Please take care that **you cannot use some destructive methods like `resize`,
28
28
/// for this kind of array**.
29
29
///
30
- /// 2.`PyArray` constructed via other methods, like `ToPyArray::to_pyarray` or `PyArray::from_slice`
31
- /// or `PyArray:: from_array`.
30
+ /// - Case2: Constructed via other methods, like [ `ToPyArray`](../convert/trait.ToPyArray.html) or
31
+ /// [`from_slice`](#method.from_slice) or [` from_array`](#from_array) .
32
32
///
33
33
/// These methods allocate a memory area in Python's private heap.
34
34
///
35
- /// In this case, you have no restriction.
35
+ /// In both cases, **an internal buffer of `PyArray` is managed by Python GC.**
36
+ /// So you can neither retrieve it nor deallocate it manually.
36
37
///
37
38
/// # Reference
38
39
///
@@ -68,7 +69,7 @@ use types::{NpyDataType, TypeNum};
68
69
/// let pyarray = PyArray::arange(gil.python(), 0., 4., 1.).reshape([2, 2]).unwrap();
69
70
/// let array = array![[3., 4.], [5., 6.]];
70
71
/// assert_eq!(
71
- /// array.dot(&pyarray.as_array().unwrap() ),
72
+ /// array.dot(&pyarray.as_array()),
72
73
/// array![[8., 15.], [12., 23.]]
73
74
/// );
74
75
/// # }
@@ -156,9 +157,6 @@ impl<T, D> PyArray<T, D> {
156
157
/// You can use this method when you have to avoid lifetime annotation to your function args
157
158
/// or return types, like used with pyo3's `pymethod`.
158
159
///
159
- /// Since this method increases refcount, you can use `PyArray` even after `pyo3::GILGuard`
160
- /// dropped, in most cases.
161
- ///
162
160
/// # Example
163
161
/// ```
164
162
/// # extern crate pyo3; extern crate numpy; fn main() {
@@ -170,7 +168,7 @@ impl<T, D> PyArray<T, D> {
170
168
/// array.to_owned(gil.python())
171
169
/// }
172
170
/// let array = return_py_array();
173
- /// assert_eq!(array.as_slice().unwrap() , &[0, 0, 0, 0, 0]);
171
+ /// assert_eq!(array.as_slice(), &[0, 0, 0, 0, 0]);
174
172
/// # }
175
173
/// ```
176
174
pub fn to_owned ( & self , py : Python ) -> Self {
@@ -376,7 +374,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
376
374
/// use numpy::PyArray2;
377
375
/// let gil = pyo3::Python::acquire_gil();
378
376
/// let pyarray = PyArray2::zeros(gil.python(), [2, 2], false);
379
- /// assert_eq!(pyarray.as_array().unwrap() , array![[0, 0], [0, 0]]);
377
+ /// assert_eq!(pyarray.as_array(), array![[0, 0], [0, 0]]);
380
378
/// # }
381
379
/// ```
382
380
pub fn zeros < ' py , ID > ( py : Python < ' py > , dims : ID , is_fortran : bool ) -> & ' py Self
@@ -407,7 +405,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
407
405
/// use numpy::PyArray;
408
406
/// let gil = pyo3::Python::acquire_gil();
409
407
/// let pyarray = PyArray::from_array(gil.python(), &array![[1, 2], [3, 4]]);
410
- /// assert_eq!(pyarray.as_array().unwrap() , array![[1, 2], [3, 4]]);
408
+ /// assert_eq!(pyarray.as_array(), array![[1, 2], [3, 4]]);
411
409
/// # }
412
410
/// ```
413
411
pub fn from_array < ' py , S > ( py : Python < ' py > , arr : & ArrayBase < S , D > ) -> & ' py Self
@@ -427,7 +425,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
427
425
/// use numpy::PyArray;
428
426
/// let gil = pyo3::Python::acquire_gil();
429
427
/// let pyarray = PyArray::from_owned_array(gil.python(), array![[1, 2], [3, 4]]);
430
- /// assert_eq!(pyarray.as_array().unwrap() , array![[1, 2], [3, 4]]);
428
+ /// assert_eq!(pyarray.as_array(), array![[1, 2], [3, 4]]);
431
429
/// # }
432
430
/// ```
433
431
pub fn from_owned_array < ' py > ( py : Python < ' py > , arr : Array < T , D > ) -> & ' py Self {
@@ -443,25 +441,20 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
443
441
/// let gil = pyo3::Python::acquire_gil();
444
442
/// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
445
443
/// assert_eq!(
446
- /// py_array.as_array().unwrap() ,
444
+ /// py_array.as_array(),
447
445
/// array![[0, 1], [2, 3]]
448
446
/// )
449
447
/// # }
450
448
/// ```
451
- pub fn as_array ( & self ) -> Result < ArrayView < T , D > , ErrorKind > {
452
- self . type_check ( ) ? ;
453
- unsafe { Ok ( ArrayView :: from_shape_ptr ( self . ndarray_shape ( ) , self . data ( ) ) ) }
449
+ pub fn as_array ( & self ) -> ArrayView < T , D > {
450
+ self . type_check_assert ( ) ;
451
+ unsafe { ArrayView :: from_shape_ptr ( self . ndarray_shape ( ) , self . data ( ) ) }
454
452
}
455
453
456
454
/// Almost same as [`as_array`](#method.as_array), but returns `ArrayViewMut`.
457
- pub fn as_array_mut ( & self ) -> Result < ArrayViewMut < T , D > , ErrorKind > {
458
- self . type_check ( ) ?;
459
- unsafe {
460
- Ok ( ArrayViewMut :: from_shape_ptr (
461
- self . ndarray_shape ( ) ,
462
- self . data ( ) ,
463
- ) )
464
- }
455
+ pub fn as_array_mut ( & self ) -> ArrayViewMut < T , D > {
456
+ self . type_check_assert ( ) ;
457
+ unsafe { ArrayViewMut :: from_shape_ptr ( self . ndarray_shape ( ) , self . data ( ) ) }
465
458
}
466
459
467
460
/// Get an immutable reference of a specified element, without checking the
@@ -574,7 +567,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
574
567
/// let gil = pyo3::Python::acquire_gil();
575
568
/// let array = [1, 2, 3, 4, 5];
576
569
/// let pyarray = PyArray::from_slice(gil.python(), &array);
577
- /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
570
+ /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
578
571
/// # }
579
572
/// ```
580
573
pub fn from_slice < ' py > ( py : Python < ' py > , slice : & [ T ] ) -> & ' py Self {
@@ -594,7 +587,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
594
587
/// let gil = pyo3::Python::acquire_gil();
595
588
/// let vec = vec![1, 2, 3, 4, 5];
596
589
/// let pyarray = PyArray::from_vec(gil.python(), vec);
597
- /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
590
+ /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
598
591
/// # }
599
592
/// ```
600
593
pub fn from_vec < ' py > ( py : Python < ' py > , vec : Vec < T > ) -> & ' py Self {
@@ -611,7 +604,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
611
604
/// let gil = pyo3::Python::acquire_gil();
612
605
/// let vec = vec![1, 2, 3, 4, 5];
613
606
/// let pyarray = PyArray::from_iter(gil.python(), vec.iter().map(|&x| x));
614
- /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
607
+ /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
615
608
/// # }
616
609
/// ```
617
610
pub fn from_exact_iter ( py : Python , iter : impl ExactSizeIterator < Item = T > ) -> & Self {
@@ -636,7 +629,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
636
629
/// let gil = pyo3::Python::acquire_gil();
637
630
/// let set: BTreeSet<u32> = [4, 3, 2, 5, 1].into_iter().cloned().collect();
638
631
/// let pyarray = PyArray::from_iter(gil.python(), set);
639
- /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
632
+ /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
640
633
/// # }
641
634
/// ```
642
635
pub fn from_iter ( py : Python , iter : impl IntoIterator < Item = T > ) -> & Self {
@@ -715,7 +708,7 @@ impl<T: TypeNum> PyArray<T, Ix2> {
715
708
/// let gil = pyo3::Python::acquire_gil();
716
709
/// let vec2 = vec![vec![1, 2, 3]; 2];
717
710
/// let pyarray = PyArray::from_vec2(gil.python(), &vec2).unwrap();
718
- /// assert_eq!(pyarray.as_array().unwrap() , array![[1, 2, 3], [1, 2, 3]]);
711
+ /// assert_eq!(pyarray.as_array(), array![[1, 2, 3], [1, 2, 3]]);
719
712
/// assert!(PyArray::from_vec2(gil.python(), &vec![vec![1], vec![2, 3]]).is_err());
720
713
/// # }
721
714
/// ```
@@ -757,7 +750,7 @@ impl<T: TypeNum> PyArray<T, Ix3> {
757
750
/// let vec2 = vec![vec![vec![1, 2]; 2]; 2];
758
751
/// let pyarray = PyArray::from_vec3(gil.python(), &vec2).unwrap();
759
752
/// assert_eq!(
760
- /// pyarray.as_array().unwrap() ,
753
+ /// pyarray.as_array(),
761
754
/// array![[[1, 2], [1, 2]], [[1, 2], [1, 2]]]
762
755
/// );
763
756
/// assert!(PyArray::from_vec3(gil.python(), &vec![vec![vec![1], vec![]]]).is_err());
@@ -802,6 +795,11 @@ impl<T: TypeNum, D> PyArray<T, D> {
802
795
NpyDataType :: from_i32 ( self . typenum ( ) )
803
796
}
804
797
798
+ fn type_check_assert ( & self ) {
799
+ let type_check = self . type_check ( ) ;
800
+ assert ! ( type_check. is_ok( ) , "{:?}" , type_check) ;
801
+ }
802
+
805
803
fn type_check ( & self ) -> Result < ( ) , ErrorKind > {
806
804
let truth = self . typenum ( ) ;
807
805
if T :: is_same_type ( truth) {
@@ -818,18 +816,18 @@ impl<T: TypeNum, D> PyArray<T, D> {
818
816
/// use numpy::PyArray;
819
817
/// let gil = pyo3::Python::acquire_gil();
820
818
/// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
821
- /// assert_eq!(py_array.as_slice().unwrap() , &[0, 1, 2, 3]);
819
+ /// assert_eq!(py_array.as_slice(), &[0, 1, 2, 3]);
822
820
/// # }
823
821
/// ```
824
- pub fn as_slice ( & self ) -> Result < & [ T ] , ErrorKind > {
825
- self . type_check ( ) ? ;
826
- unsafe { Ok ( :: std:: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) ) }
822
+ pub fn as_slice ( & self ) -> & [ T ] {
823
+ self . type_check_assert ( ) ;
824
+ unsafe { :: std:: slice:: from_raw_parts ( self . data ( ) , self . len ( ) ) }
827
825
}
828
826
829
827
/// Get the mmutable view of the internal data of `PyArray`, as slice.
830
- pub fn as_slice_mut ( & self ) -> Result < & mut [ T ] , ErrorKind > {
831
- self . type_check ( ) ? ;
832
- unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
828
+ pub fn as_slice_mut ( & self ) -> & mut [ T ] {
829
+ self . type_check_assert ( ) ;
830
+ unsafe { :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) }
833
831
}
834
832
835
833
/// Copies self into `other`, performing a data-type conversion if necessary.
@@ -841,7 +839,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
841
839
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
842
840
/// let pyarray_i = PyArray::<i64, _>::new(gil.python(), [3], false);
843
841
/// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
844
- /// assert_eq!(pyarray_i.as_slice().unwrap() , &[2, 3, 4]);
842
+ /// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
845
843
/// # }
846
844
pub fn copy_to < U : TypeNum > ( & self , other : & PyArray < U , D > ) -> Result < ( ) , ErrorKind > {
847
845
let self_ptr = self . as_array_ptr ( ) ;
@@ -862,7 +860,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
862
860
/// let gil = pyo3::Python::acquire_gil();
863
861
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
864
862
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
865
- /// assert_eq!(pyarray_i.as_slice().unwrap() , &[2, 3, 4]);
863
+ /// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
866
864
/// # }
867
865
pub fn cast < ' py , U : TypeNum > (
868
866
& ' py self ,
@@ -897,7 +895,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
897
895
/// let gil = pyo3::Python::acquire_gil();
898
896
/// let array = PyArray::from_exact_iter(gil.python(), 0..9);
899
897
/// let array = array.reshape([3, 3]).unwrap();
900
- /// assert_eq!(array.as_array().unwrap() , array![[0, 1, 2], [3, 4, 5], [6, 7, 8]]);
898
+ /// assert_eq!(array.as_array(), array![[0, 1, 2], [3, 4, 5], [6, 7, 8]]);
901
899
/// assert!(array.reshape([5]).is_err());
902
900
/// # }
903
901
/// ```
@@ -949,9 +947,9 @@ impl<T: TypeNum + AsPrimitive<f64>> PyArray<T, Ix1> {
949
947
/// use numpy::PyArray;
950
948
/// let gil = pyo3::Python::acquire_gil();
951
949
/// let pyarray = PyArray::arange(gil.python(), 2.0, 4.0, 0.5);
952
- /// assert_eq!(pyarray.as_slice().unwrap() , &[2.0, 2.5, 3.0, 3.5]);
950
+ /// assert_eq!(pyarray.as_slice(), &[2.0, 2.5, 3.0, 3.5]);
953
951
/// let pyarray = PyArray::arange(gil.python(), -2, 4, 3);
954
- /// assert_eq!(pyarray.as_slice().unwrap() , &[-2, 1]);
952
+ /// assert_eq!(pyarray.as_slice(), &[-2, 1]);
955
953
/// # }
956
954
pub fn arange < ' py > ( py : Python < ' py > , start : T , stop : T , step : T ) -> & ' py Self {
957
955
unsafe {
0 commit comments