@@ -3,7 +3,7 @@ pub(crate) use array::make_module;
33#[ pymodule( name = "array" ) ]
44mod array {
55 use crate :: common:: {
6- borrow :: { BorrowedValue , BorrowedValueMut } ,
6+ atomic :: { self , AtomicUsize } ,
77 lock:: {
88 PyMappedRwLockReadGuard , PyMappedRwLockWriteGuard , PyRwLock , PyRwLockReadGuard ,
99 PyRwLockWriteGuard ,
@@ -20,18 +20,17 @@ mod array {
2020 ArgBytesLike , ArgIntoFloat , ArgIterable , IntoPyObject , IntoPyResult , OptionalArg ,
2121 } ,
2222 protocol:: {
23- BufferInternal , BufferOptions , BufferResizeGuard , PyBuffer , PyIterReturn ,
23+ BufferDescriptor , BufferMethods , BufferResizeGuard , PyBuffer , PyIterReturn ,
2424 PyMappingMethods ,
2525 } ,
2626 sliceable:: { PySliceableSequence , PySliceableSequenceMut , SaturatedSlice , SequenceIndex } ,
2727 types:: {
2828 AsBuffer , AsMapping , Comparable , Constructor , IterNext , IterNextIterable , Iterable ,
2929 PyComparisonOp ,
3030 } ,
31- IdProtocol , PyComparisonValue , PyObject , PyObjectRef , PyObjectView , PyRef , PyResult ,
32- PyValue , TryFromObject , TypeProtocol , VirtualMachine ,
31+ IdProtocol , PyComparisonValue , PyObject , PyObjectRef , PyObjectView , PyObjectWrap , PyRef ,
32+ PyResult , PyValue , TryFromObject , TypeProtocol , VirtualMachine ,
3333 } ;
34- use crossbeam_utils:: atomic:: AtomicCell ;
3534 use itertools:: Itertools ;
3635 use num_traits:: ToPrimitive ;
3736 use std:: cmp:: Ordering ;
@@ -615,7 +614,7 @@ mod array {
615614 #[ derive( Debug , PyValue ) ]
616615 pub struct PyArray {
617616 array : PyRwLock < ArrayContentType > ,
618- exports : AtomicCell < usize > ,
617+ exports : AtomicUsize ,
619618 }
620619
621620 pub type PyArrayRef = PyRef < PyArray > ;
@@ -624,7 +623,7 @@ mod array {
624623 fn from ( array : ArrayContentType ) -> Self {
625624 PyArray {
626625 array : PyRwLock :: new ( array) ,
627- exports : AtomicCell :: new ( 0 ) ,
626+ exports : AtomicUsize :: new ( 0 ) ,
628627 }
629628 }
630629 }
@@ -1220,40 +1219,35 @@ mod array {
12201219 fn as_buffer ( zelf : & PyObjectView < Self > , _vm : & VirtualMachine ) -> PyResult < PyBuffer > {
12211220 let array = zelf. read ( ) ;
12221221 let buf = PyBuffer :: new (
1223- zelf. as_object ( ) . to_owned ( ) ,
1224- PyArrayBufferInternal ( zelf. to_owned ( ) ) ,
1225- BufferOptions {
1226- readonly : false ,
1227- len : array. len ( ) ,
1228- itemsize : array. itemsize ( ) ,
1229- format : array. typecode_str ( ) . into ( ) ,
1230- ..Default :: default ( )
1231- } ,
1222+ zelf. to_owned ( ) . into ( ) ,
1223+ BufferDescriptor :: format (
1224+ array. len ( ) * array. itemsize ( ) ,
1225+ false ,
1226+ array. itemsize ( ) ,
1227+ array. typecode_str ( ) . into ( ) ,
1228+ ) ,
1229+ & BUFFER_METHODS ,
12321230 ) ;
12331231 Ok ( buf)
12341232 }
12351233 }
12361234
1237- #[ derive( Debug ) ]
1238- struct PyArrayBufferInternal ( PyRef < PyArray > ) ;
1239-
1240- impl BufferInternal for PyArrayBufferInternal {
1241- fn obj_bytes ( & self ) -> BorrowedValue < [ u8 ] > {
1242- self . 0 . get_bytes ( ) . into ( )
1243- }
1244-
1245- fn obj_bytes_mut ( & self ) -> BorrowedValueMut < [ u8 ] > {
1246- self . 0 . get_bytes_mut ( ) . into ( )
1247- }
1248-
1249- fn release ( & self ) {
1250- self . 0 . exports . fetch_sub ( 1 ) ;
1251- }
1252-
1253- fn retain ( & self ) {
1254- self . 0 . exports . fetch_add ( 1 ) ;
1255- }
1256- }
1235+ static BUFFER_METHODS : BufferMethods = BufferMethods {
1236+ obj_bytes : |buffer| buffer. obj_as :: < PyArray > ( ) . get_bytes ( ) . into ( ) ,
1237+ obj_bytes_mut : |buffer| buffer. obj_as :: < PyArray > ( ) . get_bytes_mut ( ) . into ( ) ,
1238+ release : |buffer| {
1239+ buffer
1240+ . obj_as :: < PyArray > ( )
1241+ . exports
1242+ . fetch_sub ( 1 , atomic:: Ordering :: Release ) ;
1243+ } ,
1244+ retain : |buffer| {
1245+ buffer
1246+ . obj_as :: < PyArray > ( )
1247+ . exports
1248+ . fetch_add ( 1 , atomic:: Ordering :: Release ) ;
1249+ } ,
1250+ } ;
12571251
12581252 impl AsMapping for PyArray {
12591253 fn as_mapping ( _zelf : & PyObjectView < Self > , _vm : & VirtualMachine ) -> PyMappingMethods {
@@ -1290,7 +1284,7 @@ mod array {
12901284 impl Iterable for PyArray {
12911285 fn iter ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyResult {
12921286 Ok ( PyArrayIter {
1293- position : AtomicCell :: new ( 0 ) ,
1287+ position : AtomicUsize :: new ( 0 ) ,
12941288 array : zelf,
12951289 }
12961290 . into_object ( vm) )
@@ -1302,7 +1296,7 @@ mod array {
13021296
13031297 fn try_resizable ( & ' a self , vm : & VirtualMachine ) -> PyResult < Self :: Resizable > {
13041298 let w = self . write ( ) ;
1305- if self . exports . load ( ) == 0 {
1299+ if self . exports . load ( atomic :: Ordering :: SeqCst ) == 0 {
13061300 Ok ( w)
13071301 } else {
13081302 Err ( vm. new_buffer_error (
@@ -1316,7 +1310,7 @@ mod array {
13161310 #[ pyclass( name = "array_iterator" ) ]
13171311 #[ derive( Debug , PyValue ) ]
13181312 pub struct PyArrayIter {
1319- position : AtomicCell < usize > ,
1313+ position : AtomicUsize ,
13201314 array : PyArrayRef ,
13211315 }
13221316
@@ -1326,7 +1320,7 @@ mod array {
13261320 impl IterNextIterable for PyArrayIter { }
13271321 impl IterNext for PyArrayIter {
13281322 fn next ( zelf : & PyObjectView < Self > , vm : & VirtualMachine ) -> PyResult < PyIterReturn > {
1329- let pos = zelf. position . fetch_add ( 1 ) ;
1323+ let pos = zelf. position . fetch_add ( 1 , atomic :: Ordering :: SeqCst ) ;
13301324 let r = if let Some ( item) = zelf. array . read ( ) . getitem_by_idx ( pos, vm) ? {
13311325 PyIterReturn :: Return ( item)
13321326 } else {
0 commit comments