@@ -175,8 +175,8 @@ use crate::array::{PyArray, PyArrayMethods};
175
175
use crate :: convert:: NpyIndex ;
176
176
use crate :: dtype:: Element ;
177
177
use crate :: error:: { BorrowError , NotContiguousError } ;
178
- use crate :: untyped_array:: PyUntypedArrayMethods ;
179
178
use crate :: npyffi:: flags;
179
+ use crate :: untyped_array:: PyUntypedArrayMethods ;
180
180
181
181
use shared:: { acquire, acquire_mut, release, release_mut} ;
182
182
@@ -454,6 +454,18 @@ where
454
454
unsafe { & * ( self as * const Self as * const Self :: Target ) }
455
455
}
456
456
}
457
+ impl < ' py , T , D > From < PyReadwriteArray < ' py , T , D > > for PyReadonlyArray < ' py , T , D >
458
+ where
459
+ T : Element ,
460
+ D : Dimension ,
461
+ {
462
+ fn from ( value : PyReadwriteArray < ' py , T , D > ) -> Self {
463
+ let array = value. array . clone ( ) ;
464
+ :: std:: mem:: drop ( value) ;
465
+ Self :: try_new ( array)
466
+ . expect ( "releasing an exclusive reference should immediately permit a shared reference" )
467
+ }
468
+ }
457
469
458
470
impl < ' py , T : Element , D : Dimension > FromPyObject < ' py > for PyReadwriteArray < ' py , T , D > {
459
471
fn extract_bound ( obj : & Bound < ' py , PyAny > ) -> PyResult < Self > {
@@ -504,12 +516,13 @@ where
504
516
///
505
517
/// [writeable]: https://numpy.org/doc/stable/reference/c-api/array.html#c.NPY_ARRAY_WRITEABLE
506
518
/// [owndata]: https://numpy.org/doc/stable/reference/c-api/array.html#c.NPY_ARRAY_OWNDATA
507
- pub fn make_nonwriteable ( self ) {
519
+ pub fn make_nonwriteable ( self ) -> PyReadonlyArray < ' py , T , D > {
508
520
// SAFETY: consuming the only extant mutable reference guarantees we cannot invalidate an
509
521
// existing reference, nor allow the caller to keep hold of one.
510
522
unsafe {
511
523
( * self . as_array_ptr ( ) ) . flags &= !flags:: NPY_ARRAY_WRITEABLE ;
512
524
}
525
+ self . into ( )
513
526
}
514
527
}
515
528
0 commit comments