Skip to content

Commit ed8dba6

Browse files
committed
Rewrite nth & nth_back using conditional compilation. Rearrange flags for proper compilation
1 parent 3a7a171 commit ed8dba6

File tree

1 file changed

+30
-79
lines changed

1 file changed

+30
-79
lines changed

src/types/list.rs

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyObject, Python,
1010
};
1111
use std::iter::FusedIterator;
12-
#[cfg(all(not(Py_LIMITED_API), feature = "nightly"))]
12+
#[cfg(feature = "nightly")]
1313
use std::num::NonZero;
1414

1515
/// Represents a Python `list`.
@@ -548,28 +548,7 @@ impl<'py> BoundListIterator<'py> {
548548
}
549549

550550
#[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"))]
573552
fn nth(
574553
index: &mut Index,
575554
length: &mut Length,
@@ -578,8 +557,16 @@ impl<'py> BoundListIterator<'py> {
578557
) -> Option<Bound<'py, PyAny>> {
579558
let length = length.0.min(list.len());
580559
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+
};
583570
index.0 = target_index + 1;
584571
Some(item)
585572
} else {
@@ -630,27 +617,7 @@ impl<'py> BoundListIterator<'py> {
630617
}
631618

632619
#[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"))]
654621
fn nth_back(
655622
index: &mut Index,
656623
length: &mut Length,
@@ -660,7 +627,17 @@ impl<'py> BoundListIterator<'py> {
660627
let length_size = length.0.min(list.len());
661628
if index.0 + n < length_size {
662629
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+
};
664641
*length = Length(target_index);
665642
Some(item)
666643
} else {
@@ -705,23 +682,11 @@ impl<'py> Iterator for BoundListIterator<'py> {
705682
}
706683

707684
#[inline]
708-
#[cfg(feature = "nightly")]
685+
#[cfg(not(feature = "nightly"))]
709686
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| {
723688
Self::nth(index, length, list, n)
724-
}
689+
})
725690
}
726691

727692
#[inline]
@@ -851,7 +816,7 @@ impl<'py> Iterator for BoundListIterator<'py> {
851816
}
852817

853818
#[inline]
854-
#[cfg(all(not(Py_LIMITED_API), feature = "nightly"))]
819+
#[cfg(feature = "nightly")]
855820
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
856821
self.with_critical_section(|index, length, list| {
857822
let max_len = length.0.min(list.len());
@@ -898,23 +863,9 @@ impl DoubleEndedIterator for BoundListIterator<'_> {
898863
}
899864

900865
#[inline]
901-
#[cfg(feature = "nightly")]
866+
#[cfg(not(feature = "nightly"))]
902867
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))
918869
}
919870

920871
#[inline]

0 commit comments

Comments
 (0)