@@ -257,6 +257,41 @@ impl PySequence {
257
257
}
258
258
}
259
259
260
+ #[ cfg( feature = "const-generics" ) ]
261
+ impl < ' a , T , const N : usize > FromPyObject < ' a > for [ T ; N ]
262
+ where
263
+ T : FromPyObject < ' a > ,
264
+ {
265
+ #[ cfg( not( feature = "nightly" ) ) ]
266
+ fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
267
+ create_array_from_obj ( obj)
268
+ }
269
+
270
+ #[ cfg( feature = "nightly" ) ]
271
+ default fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
272
+ create_array_from_obj ( obj)
273
+ }
274
+ }
275
+
276
+ #[ cfg( all( feature = "const-generics" , feature = "nightly" ) ) ]
277
+ impl < ' source , T , const N : usize > FromPyObject < ' source > for [ T ; N ]
278
+ where
279
+ for < ' a > T : FromPyObject < ' a > + crate :: buffer:: Element ,
280
+ {
281
+ fn extract ( obj : & ' source PyAny ) -> PyResult < Self > {
282
+ let mut array = create_array_from_obj ( obj) ?;
283
+ if let Ok ( buf) = crate :: buffer:: PyBuffer :: get ( obj) {
284
+ if buf. dimensions ( ) == 1 && buf. copy_to_slice ( obj. py ( ) , & mut array) . is_ok ( ) {
285
+ buf. release ( obj. py ( ) ) ;
286
+ return Ok ( array) ;
287
+ }
288
+ buf. release ( obj. py ( ) ) ;
289
+ }
290
+ Ok ( array)
291
+ }
292
+ }
293
+
294
+ #[ cfg( not( feature = "const-generics" ) ) ]
260
295
macro_rules! array_impls {
261
296
( $( $N: expr) ,+) => {
262
297
$(
@@ -303,6 +338,7 @@ macro_rules! array_impls {
303
338
}
304
339
}
305
340
341
+ #[ cfg( not( feature = "const-generics" ) ) ]
306
342
array_impls ! (
307
343
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 ,
308
344
26 , 27 , 28 , 29 , 30 , 31 , 32
@@ -343,6 +379,21 @@ where
343
379
}
344
380
}
345
381
382
+ #[ cfg( feature = "const-generics" ) ]
383
+ fn create_array_from_obj < ' s , T , const N : usize > ( obj : & ' s PyAny ) -> PyResult < [ T ; N ] >
384
+ where
385
+ T : FromPyObject < ' s > ,
386
+ {
387
+ let seq = <PySequence as PyTryFrom >:: try_from ( obj) ?;
388
+ crate :: types:: try_create_array ( |idx| {
389
+ seq. get_item ( idx as isize )
390
+ . map_err ( |_| {
391
+ exceptions:: PyBufferError :: py_err ( "Slice length does not match buffer length." )
392
+ } ) ?
393
+ . extract :: < T > ( )
394
+ } )
395
+ }
396
+
346
397
fn extract_sequence < ' s , T > ( obj : & ' s PyAny ) -> PyResult < Vec < T > >
347
398
where
348
399
T : FromPyObject < ' s > ,
@@ -355,6 +406,7 @@ where
355
406
Ok ( v)
356
407
}
357
408
409
+ #[ cfg( not( feature = "const-generics" ) ) ]
358
410
fn extract_sequence_into_slice < ' s , T > ( obj : & ' s PyAny , slice : & mut [ T ] ) -> PyResult < ( ) >
359
411
where
360
412
T : FromPyObject < ' s > ,
0 commit comments