@@ -9,7 +9,7 @@ use crate::{
9
9
Borrowed , Bound , BoundObject , IntoPyObject , IntoPyObjectExt , PyAny , PyErr , PyObject , Python ,
10
10
} ;
11
11
use std:: iter:: FusedIterator ;
12
- #[ cfg( all ( not ( Py_LIMITED_API ) , feature = "nightly" ) ) ]
12
+ #[ cfg( feature = "nightly" ) ]
13
13
use std:: num:: NonZero ;
14
14
15
15
/// Represents a Python `list`.
@@ -548,28 +548,7 @@ impl<'py> BoundListIterator<'py> {
548
548
}
549
549
550
550
#[ inline]
551
- #[ cfg( all( not( Py_LIMITED_API ) , feature = "nightly" ) ) ]
552
- #[ deny( unsafe_op_in_unsafe_fn) ]
553
- unsafe fn nth_unchecked (
554
- index : & mut Index ,
555
- length : & mut Length ,
556
- list : & Bound < ' py , PyList > ,
557
- n : usize ,
558
- ) -> Option < Bound < ' py , PyAny > > {
559
- let length = length. 0 . min ( list. len ( ) ) ;
560
- let target_index = index. 0 + n;
561
- if index. 0 + n < length {
562
- let item = unsafe { list. get_item_unchecked ( target_index) } ;
563
- index. 0 = target_index + 1 ;
564
- Some ( item)
565
- } else {
566
- None
567
- }
568
- }
569
-
570
- #[ inline]
571
- #[ cfg( all( Py_LIMITED_API , feature = "nightly" ) ) ]
572
- #[ deny( unsafe_op_in_unsafe_fn) ]
551
+ #[ cfg( not( feature = "nightly" ) ) ]
573
552
fn nth (
574
553
index : & mut Index ,
575
554
length : & mut Length ,
@@ -578,8 +557,16 @@ impl<'py> BoundListIterator<'py> {
578
557
) -> Option < Bound < ' py , PyAny > > {
579
558
let length = length. 0 . min ( list. len ( ) ) ;
580
559
let target_index = index. 0 + n;
581
- if index. 0 + n < length {
582
- let item = list. get_item ( target_index) . expect ( "get-item failed" ) ;
560
+ if target_index < length {
561
+ let item = {
562
+ #[ cfg( Py_LIMITED_API ) ] {
563
+ list. get_item ( target_index) . expect ( "get-item failed" )
564
+ }
565
+
566
+ #[ cfg( not( Py_LIMITED_API ) ) ] {
567
+ unsafe { list. get_item_unchecked ( target_index) }
568
+ }
569
+ } ;
583
570
index. 0 = target_index + 1 ;
584
571
Some ( item)
585
572
} else {
@@ -630,27 +617,7 @@ impl<'py> BoundListIterator<'py> {
630
617
}
631
618
632
619
#[ inline]
633
- #[ cfg( all( not( Py_LIMITED_API ) , feature = "nightly" ) ) ]
634
- #[ deny( unsafe_op_in_unsafe_fn) ]
635
- unsafe fn nth_back_unchecked (
636
- index : & mut Index ,
637
- length : & mut Length ,
638
- list : & Bound < ' py , PyList > ,
639
- n : usize ,
640
- ) -> Option < Bound < ' py , PyAny > > {
641
- let length_size = length. 0 . min ( list. len ( ) ) ;
642
- if index. 0 + n < length_size {
643
- let target_index = length_size - n - 1 ;
644
- let item = unsafe { list. get_item_unchecked ( target_index) } ;
645
- * length = Length ( target_index) ;
646
- Some ( item)
647
- } else {
648
- None
649
- }
650
- }
651
-
652
- #[ inline]
653
- #[ cfg( all( Py_LIMITED_API , feature = "nightly" ) ) ]
620
+ #[ cfg( not( feature = "nightly" ) ) ]
654
621
fn nth_back (
655
622
index : & mut Index ,
656
623
length : & mut Length ,
@@ -660,7 +627,17 @@ impl<'py> BoundListIterator<'py> {
660
627
let length_size = length. 0 . min ( list. len ( ) ) ;
661
628
if index. 0 + n < length_size {
662
629
let target_index = length_size - n - 1 ;
663
- let item = list. get_item ( target_index) . expect ( "get-item failed" ) ;
630
+ let item = {
631
+ #[ cfg( not( Py_LIMITED_API ) ) ]
632
+ {
633
+ unsafe { list. get_item_unchecked ( target_index) }
634
+ }
635
+
636
+ #[ cfg( Py_LIMITED_API ) ]
637
+ {
638
+ list. get_item ( target_index) . expect ( "get-item failed" )
639
+ }
640
+ } ;
664
641
* length = Length ( target_index) ;
665
642
Some ( item)
666
643
} else {
@@ -705,23 +682,11 @@ impl<'py> Iterator for BoundListIterator<'py> {
705
682
}
706
683
707
684
#[ inline]
708
- #[ cfg( feature = "nightly" ) ]
685
+ #[ cfg( not ( feature = "nightly" ) ) ]
709
686
fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
710
- #[ cfg( not( Py_LIMITED_API ) ) ]
711
- {
712
- self . with_critical_section ( |index, length, list| unsafe {
713
- Self :: nth_unchecked ( index, length, list, n)
714
- } )
715
- }
716
- #[ cfg( Py_LIMITED_API ) ]
717
- {
718
- let Self {
719
- index,
720
- length,
721
- list,
722
- } = self ;
687
+ self . with_critical_section ( |index, length, list| {
723
688
Self :: nth ( index, length, list, n)
724
- }
689
+ } )
725
690
}
726
691
727
692
#[ inline]
@@ -851,7 +816,7 @@ impl<'py> Iterator for BoundListIterator<'py> {
851
816
}
852
817
853
818
#[ inline]
854
- #[ cfg( all ( not ( Py_LIMITED_API ) , feature = "nightly" ) ) ]
819
+ #[ cfg( feature = "nightly" ) ]
855
820
fn advance_by ( & mut self , n : usize ) -> Result < ( ) , NonZero < usize > > {
856
821
self . with_critical_section ( |index, length, list| {
857
822
let max_len = length. 0 . min ( list. len ( ) ) ;
@@ -898,23 +863,9 @@ impl DoubleEndedIterator for BoundListIterator<'_> {
898
863
}
899
864
900
865
#[ inline]
901
- #[ cfg( feature = "nightly" ) ]
866
+ #[ cfg( not ( feature = "nightly" ) ) ]
902
867
fn nth_back ( & mut self , n : usize ) -> Option < Self :: Item > {
903
- #[ cfg( not( Py_LIMITED_API ) ) ]
904
- {
905
- self . with_critical_section ( |index, length, list| unsafe {
906
- Self :: nth_back_unchecked ( index, length, list, n)
907
- } )
908
- }
909
- #[ cfg( Py_LIMITED_API ) ]
910
- {
911
- let Self {
912
- index,
913
- length,
914
- list,
915
- } = self ;
916
- Self :: nth_back ( index, length, list, n)
917
- }
868
+ self . with_critical_section ( |index, length, list| Self :: nth_back ( index, length, list, n) )
918
869
}
919
870
920
871
#[ inline]
0 commit comments