Skip to content

Commit

Permalink
fixup: EXT: nextImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Dec 31, 2024
1 parent dab4523 commit 568a8c3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/pylib/builtins/iter_next.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## builtins.iter

import ../collections_abc
import std/options

#iterator items*[T](it: PyIterable[T]): T = for i in it.iter: yield i

Expand Down Expand Up @@ -35,8 +36,17 @@ iterator items*[T](self: PyIterator[T]): T =
for i in self.iter():
yield i

proc nextImpl*[T](self: PyIterator[T]): Option[T]{.inline.} =
## EXT. Get rid of exception for faster iteration.
result = some self.iter()
if self.iter.finished(): result = none(T)

proc nextImpl*[T](self: PyIterator[T], res: var T): bool{.inline.} =
## EXT. Get rid of exception for faster iteration.
res = self.iter()
not self.iter.finished()

proc next*[T](self: PyIterator[T]): T =
# see manual, sysmte.finished shall validate after a value is getten from iterator.
result = self.iter()
if self.iter.finished():
if not self.nextImpl result:
raise newException(StopIteration, "iterator stop")

0 comments on commit 568a8c3

Please sign in to comment.