Skip to content

Commit

Permalink
Fix the DoubleEndedIterator implementation of VarRange
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekker1 committed Apr 7, 2024
1 parent 4f77fa3 commit 55fb1bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions crates/pindakaas/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ impl VarRange {
Self { start, end }
}

pub fn empty() -> Self {
Self {
start: Var(NonZeroI32::new(2).unwrap()),
end: Var(NonZeroI32::new(1).unwrap()),
}
}

/// Performs the indexing operation into the variable range
pub fn index(&self, index: usize) -> Var {
if index >= self.len() {
Expand Down Expand Up @@ -273,8 +280,13 @@ impl DoubleEndedIterator for VarRange {
fn next_back(&mut self) -> Option<Self::Item> {
if self.start <= self.end {
let item = self.end;
self.end = self.end.prev_var().unwrap();
Some(item)
if let Some(prev) = self.end.prev_var() {
self.end = prev;
Some(item)
} else {
*self = VarRange::empty();
None
}
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pindakaas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Var {

fn prev_var(&self) -> Option<Var> {
let prev = self.0.get() - 1;
if prev >= 0 {
if prev > 0 {
Some(Var(NonZeroI32::new(prev).unwrap()))
} else {
None
Expand Down

0 comments on commit 55fb1bd

Please sign in to comment.