@@ -257,6 +257,7 @@ impl PySequence {
257257 }
258258}
259259
260+ #[ cfg( not( min_const_generics) ) ]
260261macro_rules! array_impls {
261262 ( $( $N: expr) ,+) => {
262263 $(
@@ -305,11 +306,46 @@ macro_rules! array_impls {
305306 }
306307}
307308
309+ #[ cfg( not( min_const_generics) ) ]
308310array_impls ! (
309311 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 ,
310312 26 , 27 , 28 , 29 , 30 , 31 , 32
311313) ;
312314
315+ #[ cfg( all( min_const_generics, not( feature = "nightly" ) ) ) ]
316+ impl < ' a , T , const N : usize > FromPyObject < ' a > for [ T ; N ]
317+ where
318+ T : FromPyObject < ' a > ,
319+ {
320+ #[ cfg( not( feature = "nightly" ) ) ]
321+ fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
322+ create_array_from_obj ( obj)
323+ }
324+
325+ #[ cfg( feature = "nightly" ) ]
326+ default fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
327+ create_array_from_obj ( obj)
328+ }
329+ }
330+
331+ #[ cfg( all( min_const_generics, feature = "nightly" ) ) ]
332+ impl < ' source , T , const N : usize > FromPyObject < ' source > for [ T ; N ]
333+ where
334+ for < ' a > T : FromPyObject < ' a > + crate :: buffer:: Element ,
335+ {
336+ fn extract ( obj : & ' source PyAny ) -> PyResult < Self > {
337+ let mut array = create_array_from_obj ( obj) ?;
338+ if let Ok ( buf) = crate :: buffer:: PyBuffer :: get ( obj) {
339+ if buf. dimensions ( ) == 1 && buf. copy_to_slice ( obj. py ( ) , & mut array) . is_ok ( ) {
340+ buf. release ( obj. py ( ) ) ;
341+ return Ok ( array) ;
342+ }
343+ buf. release ( obj. py ( ) ) ;
344+ }
345+ Ok ( array)
346+ }
347+ }
348+
313349impl < ' a , T > FromPyObject < ' a > for Vec < T >
314350where
315351 T : FromPyObject < ' a > ,
@@ -345,6 +381,21 @@ where
345381 }
346382}
347383
384+ #[ cfg( min_const_generics) ]
385+ fn create_array_from_obj < ' s , T , const N : usize > ( obj : & ' s PyAny ) -> PyResult < [ T ; N ] >
386+ where
387+ T : FromPyObject < ' s > ,
388+ {
389+ let seq = <PySequence as PyTryFrom >:: try_from ( obj) ?;
390+ crate :: types:: try_create_array ( |idx| {
391+ seq. get_item ( idx as isize )
392+ . map_err ( |_| {
393+ exceptions:: PyBufferError :: new_err ( "Slice length does not match buffer length." )
394+ } ) ?
395+ . extract :: < T > ( )
396+ } )
397+ }
398+
348399fn extract_sequence < ' s , T > ( obj : & ' s PyAny ) -> PyResult < Vec < T > >
349400where
350401 T : FromPyObject < ' s > ,
@@ -357,6 +408,7 @@ where
357408 Ok ( v)
358409}
359410
411+ #[ cfg( not( min_const_generics) ) ]
360412fn extract_sequence_into_slice < ' s , T > ( obj : & ' s PyAny , slice : & mut [ T ] ) -> PyResult < ( ) >
361413where
362414 T : FromPyObject < ' s > ,
@@ -706,6 +758,7 @@ mod test {
706758 assert ! ( v == [ 1 , 2 , 3 , 4 ] ) ;
707759 }
708760
761+ #[ cfg( not( min_const_generics) ) ]
709762 #[ test]
710763 fn test_extract_bytearray_to_array ( ) {
711764 let gil = Python :: acquire_gil ( ) ;
@@ -718,6 +771,23 @@ mod test {
718771 assert ! ( & v == b"abc" ) ;
719772 }
720773
774+ #[ cfg( min_const_generics) ]
775+ #[ test]
776+ fn test_extract_bytearray_to_array ( ) {
777+ let gil = Python :: acquire_gil ( ) ;
778+ let py = gil. python ( ) ;
779+ let v: [ u8 ; 33 ] = py
780+ . eval (
781+ "bytearray(b'abcabcabcabcabcabcabcabcabcabcabc')" ,
782+ None ,
783+ None ,
784+ )
785+ . unwrap ( )
786+ . extract ( )
787+ . unwrap ( ) ;
788+ assert ! ( & v == b"abcabcabcabcabcabcabcabcabcabcabc" ) ;
789+ }
790+
721791 #[ test]
722792 fn test_extract_bytearray_to_vec ( ) {
723793 let gil = Python :: acquire_gil ( ) ;
0 commit comments