Fix range reversed #421

This commit is contained in:
Homer McMillan
2019-02-09 15:49:51 -05:00
parent ac37026010
commit 4e02962289

View File

@@ -95,14 +95,25 @@ impl RangeType {
#[inline]
pub fn reversed(&self) -> Self {
// compute the last element that is actually contained within the range
// this is the new start
let remainder = ((&self.end - &self.start) % &self.step).abs();
let start = if rem.is_zero() {
&self.end - &self.step
} else {
&self.end - &remainder
};
match self.step.sign() {
Sign::Plus => RangeType {
start: &self.end - 1,
end: &self.start - 1,
step: -&self.step,
Sign::Plus => {
RangeType {
start,
end: &self.start - 1,
step: -&self.step,
}
},
Sign::Minus => RangeType {
start: &self.end + 1,
start,
end: &self.start + 1,
step: -&self.step,
},