Skip to content

Commit

Permalink
Use out param for __next__() in more iterators
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Grenier <[email protected]>
  • Loading branch information
bgreni committed Jan 16, 2025
1 parent ccdb14a commit 7e669b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions stdlib/src/collections/deque.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,14 @@ struct _DequeIter[
fn __iter__(self) -> Self:
return self

fn __next__(mut self) -> Pointer[ElementType, deque_lifetime]:
fn __next__(mut self, out p: Pointer[ElementType, deque_lifetime]):
@parameter
if forward:
p = Pointer.address_of(self.src[][self.index])
self.index += 1
return Pointer.address_of(self.src[][self.index - 1])
else:
self.index -= 1
return Pointer.address_of(self.src[][self.index])
p = Pointer.address_of(self.src[][self.index])

fn __len__(self) -> Int:
@parameter
Expand Down
8 changes: 3 additions & 5 deletions stdlib/src/collections/inline_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ struct _InlineListIter[
fn __iter__(self) -> Self:
return self

fn __next__(
mut self,
) -> Pointer[T, __origin_of(self.src[][0])]:
fn __next__(mut self, out p: Pointer[T, __origin_of(self.src[][0])]):
@parameter
if forward:
p = Pointer.address_of(self.src[][self.index])
self.index += 1
return Pointer.address_of(self.src[][self.index - 1])
else:
self.index -= 1
return Pointer.address_of(self.src[][self.index])
p = Pointer.address_of(self.src[][self.index])

@always_inline
fn __has_next__(self) -> Bool:
Expand Down
8 changes: 3 additions & 5 deletions stdlib/src/memory/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ struct _SpanIter[
return self

@always_inline
fn __next__(
mut self,
) -> Pointer[T, origin]:
fn __next__(mut self, out p: Pointer[T, origin]):
@parameter
if forward:
p = Pointer.address_of(self.src[self.index])
self.index += 1
return Pointer.address_of(self.src[self.index - 1])
else:
self.index -= 1
return Pointer.address_of(self.src[self.index])
p = Pointer.address_of(self.src[self.index])

@always_inline
fn __has_next__(self) -> Bool:
Expand Down

0 comments on commit 7e669b4

Please sign in to comment.