1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 4 // option. This file may not be copied, modified, or distributed 5 // except according to those terms. 6 7 /// This module just implements a simple verison of step_by() since 8 /// the function from the standard library is currently unstable. 9 /// This should be removed once that function becomes stable. 10 11 use std::ops::{Add, Range}; 12 13 #[derive(Clone)] 14 pub struct StepUp<T> { 15 next: T, 16 end: T, 17 ammount: T 18 } 19 20 impl <T> Iterator for StepUp<T> where 21 T: Add<T, Output = T> + PartialOrd + Copy { 22 type Item = T; 23 24 #[inline] next(&mut self) -> Option<T>25 fn next(&mut self) -> Option<T> { 26 if self.next < self.end { 27 let n = self.next; 28 self.next = self.next + self.ammount; 29 Some(n) 30 } else { 31 None 32 } 33 } 34 } 35 36 pub trait RangeExt<T> { step_up(self, ammount: T) -> StepUp<T>37 fn step_up(self, ammount: T) -> StepUp<T>; 38 } 39 40 impl <T> RangeExt<T> for Range<T> where 41 T: Add<T, Output = T> + PartialOrd + Copy { step_up(self, ammount: T) -> StepUp<T>42 fn step_up(self, ammount: T) -> StepUp<T> { 43 StepUp { 44 next: self.start, 45 end: self.end, 46 ammount: ammount 47 } 48 } 49 } 50 51