Skip to content

Commit

Permalink
Use step_unchecked more liberally
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jun 19, 2020
1 parent 036b5fe commit 7779a11
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,6 @@ impl<A: Step> Iterator for ops::Range<A> {
fn next(&mut self) -> Option<A> {
if self.start < self.end {
// SAFETY: just checked precondition
// We use the unchecked version here, because
// this helps LLVM vectorize loops for some ranges
// that don't get vectorized otherwise.
let n = unsafe { Step::forward_unchecked(self.start.clone(), 1) };
Some(mem::replace(&mut self.start, n))
} else {
Expand All @@ -514,7 +511,8 @@ impl<A: Step> Iterator for ops::Range<A> {
fn nth(&mut self, n: usize) -> Option<A> {
if let Some(plus_n) = Step::forward_checked(self.start.clone(), n) {
if plus_n < self.end {
self.start = Step::forward(plus_n.clone(), 1);
// SAFETY: just checked precondition
self.start = unsafe { Step::forward_unchecked(plus_n.clone(), 1) };
return Some(plus_n);
}
}
Expand Down Expand Up @@ -575,7 +573,8 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
if self.start < self.end {
self.end = Step::backward(self.end.clone(), 1);
// SAFETY: just checked precondition
self.end = unsafe { Step::backward_unchecked(self.end.clone(), 1) };
Some(self.end.clone())
} else {
None
Expand All @@ -586,7 +585,8 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
fn nth_back(&mut self, n: usize) -> Option<A> {
if let Some(minus_n) = Step::backward_checked(self.end.clone(), n) {
if minus_n > self.start {
self.end = Step::backward(minus_n, 1);
// SAFETY: just checked precondition
self.end = unsafe { Step::backward_unchecked(minus_n, 1) };
return Some(self.end.clone());
}
}
Expand Down Expand Up @@ -643,9 +643,6 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
let is_iterating = self.start < self.end;
Some(if is_iterating {
// SAFETY: just checked precondition
// We use the unchecked version here, because
// otherwise `for _ in '\0'..=char::MAX`
// does not successfully remove panicking code.
let n = unsafe { Step::forward_unchecked(self.start.clone(), 1) };
mem::replace(&mut self.start, n)
} else {
Expand Down Expand Up @@ -708,7 +705,8 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
let mut accum = init;

while self.start < self.end {
let n = Step::forward(self.start.clone(), 1);
// SAFETY: just checked precondition
let n = unsafe { Step::forward_unchecked(self.start.clone(), 1) };
let n = mem::replace(&mut self.start, n);
accum = f(accum, n)?;
}
Expand Down Expand Up @@ -761,7 +759,8 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
}
let is_iterating = self.start < self.end;
Some(if is_iterating {
let n = Step::backward(self.end.clone(), 1);
// SAFETY: just checked precondition
let n = unsafe { Step::backward_unchecked(self.end.clone(), 1) };
mem::replace(&mut self.end, n)
} else {
self.exhausted = true;
Expand Down Expand Up @@ -811,7 +810,8 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
let mut accum = init;

while self.start < self.end {
let n = Step::backward(self.end.clone(), 1);
// SAFETY: just checked precondition
let n = unsafe { Step::backward_unchecked(self.end.clone(), 1) };
let n = mem::replace(&mut self.end, n);
accum = f(accum, n)?;
}
Expand Down

0 comments on commit 7779a11

Please sign in to comment.