@@ -257,6 +257,7 @@ impl PySequence {
257
257
}
258
258
}
259
259
260
+ #[ cfg( not( min_const_generics) ) ]
260
261
macro_rules! array_impls {
261
262
( $( $N: expr) ,+) => {
262
263
$(
@@ -305,11 +306,46 @@ macro_rules! array_impls {
305
306
}
306
307
}
307
308
309
+ #[ cfg( not( min_const_generics) ) ]
308
310
array_impls ! (
309
311
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 ,
310
312
26 , 27 , 28 , 29 , 30 , 31 , 32
311
313
) ;
312
314
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
+
313
349
impl < ' a , T > FromPyObject < ' a > for Vec < T >
314
350
where
315
351
T : FromPyObject < ' a > ,
@@ -345,6 +381,21 @@ where
345
381
}
346
382
}
347
383
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
+
348
399
fn extract_sequence < ' s , T > ( obj : & ' s PyAny ) -> PyResult < Vec < T > >
349
400
where
350
401
T : FromPyObject < ' s > ,
@@ -357,6 +408,7 @@ where
357
408
Ok ( v)
358
409
}
359
410
411
+ #[ cfg( not( min_const_generics) ) ]
360
412
fn extract_sequence_into_slice < ' s , T > ( obj : & ' s PyAny , slice : & mut [ T ] ) -> PyResult < ( ) >
361
413
where
362
414
T : FromPyObject < ' s > ,
@@ -706,6 +758,7 @@ mod test {
706
758
assert ! ( v == [ 1 , 2 , 3 , 4 ] ) ;
707
759
}
708
760
761
+ #[ cfg( not( min_const_generics) ) ]
709
762
#[ test]
710
763
fn test_extract_bytearray_to_array ( ) {
711
764
let gil = Python :: acquire_gil ( ) ;
@@ -718,6 +771,23 @@ mod test {
718
771
assert ! ( & v == b"abc" ) ;
719
772
}
720
773
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
+
721
791
#[ test]
722
792
fn test_extract_bytearray_to_vec ( ) {
723
793
let gil = Python :: acquire_gil ( ) ;
0 commit comments