Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use step_unchecked more liberally in range iter impls #73490

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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