1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/// This module just implements a simple verison of step_by() since
/// the function from the standard library is currently unstable.
/// This should be removed once that function becomes stable.

use std::ops::{Add, Range};

#[derive(Clone)]
pub struct StepUp<T> {
    next: T,
    end: T,
    ammount: T
}

impl <T> Iterator for StepUp<T> where
        T: Add<T, Output = T> + PartialOrd + Copy {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        if self.next < self.end {
            let n = self.next;
            self.next = self.next + self.ammount;
            Some(n)
        } else {
            None
        }
    }
}

pub trait RangeExt<T> {
    fn step_up(self, ammount: T) -> StepUp<T>;
}

impl <T> RangeExt<T> for Range<T> where
        T: Add<T, Output = T> + PartialOrd + Copy {
    fn step_up(self, ammount: T) -> StepUp<T> {
        StepUp {
            next: self.start,
            end: self.end,
            ammount: ammount
        }
    }
}