@@ -257,57 +257,39 @@ impl PySequence {
257
257
}
258
258
}
259
259
260
- macro_rules! array_impls {
261
- ( $( $N: expr) ,+) => {
262
- $(
263
- impl <' a, T > FromPyObject <' a> for [ T ; $N]
264
- where
265
- T : Copy + Default + FromPyObject <' a>,
266
- {
267
- #[ cfg( not( feature = "nightly" ) ) ]
268
- fn extract( obj: & ' a PyAny ) -> PyResult <Self > {
269
- let mut array = [ T :: default ( ) ; $N] ;
270
- extract_sequence_into_slice( obj, & mut array) ?;
271
- Ok ( array)
272
- }
260
+ impl < ' a , T , const N : usize > FromPyObject < ' a > for [ T ; N ]
261
+ where
262
+ T : FromPyObject < ' a > ,
263
+ {
264
+ #[ cfg( not( feature = "nightly" ) ) ]
265
+ fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
266
+ create_array_from_obj ( obj)
267
+ }
273
268
274
- #[ cfg( feature = "nightly" ) ]
275
- default fn extract( obj: & ' a PyAny ) -> PyResult <Self > {
276
- let mut array = [ T :: default ( ) ; $N] ;
277
- extract_sequence_into_slice( obj, & mut array) ?;
278
- Ok ( array)
279
- }
280
- }
269
+ #[ cfg( feature = "nightly" ) ]
270
+ default fn extract ( obj : & ' a PyAny ) -> PyResult < Self > {
271
+ create_array_from_obj ( obj)
272
+ }
273
+ }
281
274
282
- #[ cfg( feature = "nightly" ) ]
283
- impl <' source, T > FromPyObject <' source> for [ T ; $N]
284
- where
285
- for <' a> T : Default + FromPyObject <' a> + crate :: buffer:: Element ,
286
- {
287
- fn extract( obj: & ' source PyAny ) -> PyResult <Self > {
288
- let mut array = [ T :: default ( ) ; $N] ;
289
- // first try buffer protocol
290
- if let Ok ( buf) = crate :: buffer:: PyBuffer :: get( obj) {
291
- if buf. dimensions( ) == 1 && buf. copy_to_slice( obj. py( ) , & mut array) . is_ok( ) {
292
- buf. release( obj. py( ) ) ;
293
- return Ok ( array) ;
294
- }
295
- buf. release( obj. py( ) ) ;
296
- }
297
- // fall back to sequence protocol
298
- extract_sequence_into_slice( obj, & mut array) ?;
299
- Ok ( array)
300
- }
275
+ #[ cfg( feature = "nightly" ) ]
276
+ impl < ' source , T , const N : usize > FromPyObject < ' source > for [ T ; N ]
277
+ where
278
+ for < ' a > T : FromPyObject < ' a > + crate :: buffer:: Element ,
279
+ {
280
+ fn extract ( obj : & ' source PyAny ) -> PyResult < Self > {
281
+ let mut array = create_array_from_obj ( obj) ?;
282
+ if let Ok ( buf) = crate :: buffer:: PyBuffer :: get ( obj) {
283
+ if buf. dimensions ( ) == 1 && buf. copy_to_slice ( obj. py ( ) , & mut array) . is_ok ( ) {
284
+ buf. release ( obj. py ( ) ) ;
285
+ return Ok ( array) ;
301
286
}
302
- ) +
287
+ buf. release ( obj. py ( ) ) ;
288
+ }
289
+ Ok ( array)
303
290
}
304
291
}
305
292
306
- array_impls ! (
307
- 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
- 26 , 27 , 28 , 29 , 30 , 31 , 32
309
- ) ;
310
-
311
293
impl < ' a , T > FromPyObject < ' a > for Vec < T >
312
294
where
313
295
T : FromPyObject < ' a > ,
@@ -343,32 +325,30 @@ where
343
325
}
344
326
}
345
327
346
- fn extract_sequence < ' s , T > ( obj : & ' s PyAny ) -> PyResult < Vec < T > >
328
+ fn create_array_from_obj < ' s , T , const N : usize > ( obj : & ' s PyAny ) -> PyResult < [ T ; N ] >
347
329
where
348
330
T : FromPyObject < ' s > ,
349
331
{
350
332
let seq = <PySequence as PyTryFrom >:: try_from ( obj) ?;
351
- let mut v = Vec :: with_capacity ( seq. len ( ) . unwrap_or ( 0 ) as usize ) ;
352
- for item in seq. iter ( ) ? {
353
- v. push ( item?. extract :: < T > ( ) ?) ;
354
- }
355
- Ok ( v)
333
+ crate :: types:: try_create_array ( |idx| {
334
+ seq. get_item ( idx as isize )
335
+ . map_err ( |_| {
336
+ exceptions:: PyBufferError :: new_err ( "Slice length does not match buffer length." )
337
+ } ) ?
338
+ . extract :: < T > ( )
339
+ } )
356
340
}
357
341
358
- fn extract_sequence_into_slice < ' s , T > ( obj : & ' s PyAny , slice : & mut [ T ] ) -> PyResult < ( ) >
342
+ fn extract_sequence < ' s , T > ( obj : & ' s PyAny ) -> PyResult < Vec < T > >
359
343
where
360
344
T : FromPyObject < ' s > ,
361
345
{
362
346
let seq = <PySequence as PyTryFrom >:: try_from ( obj) ?;
363
- if seq. len ( ) ? as usize != slice. len ( ) {
364
- return Err ( exceptions:: PyBufferError :: new_err (
365
- "Slice length does not match buffer length." ,
366
- ) ) ;
367
- }
368
- for ( value, item) in slice. iter_mut ( ) . zip ( seq. iter ( ) ?) {
369
- * value = item?. extract :: < T > ( ) ?;
347
+ let mut v = Vec :: with_capacity ( seq. len ( ) . unwrap_or ( 0 ) as usize ) ;
348
+ for item in seq. iter ( ) ? {
349
+ v. push ( item?. extract :: < T > ( ) ?) ;
370
350
}
371
- Ok ( ( ) )
351
+ Ok ( v )
372
352
}
373
353
374
354
impl < ' v > PyTryFrom < ' v > for PySequence {
0 commit comments