rust-crypto 0.2.32

A (mostly) pure-Rust implementation of various common cryptographic algorithms.
Documentation
// 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
        }
    }
}

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

impl <T> Iterator for StepDown<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>;
    fn step_down(self, ammount: T) -> StepDown<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
        }
    }

    fn step_down(self, ammount: T) -> StepDown<T> {
        StepDown {
            next: self.start,
            end: self.end,
            ammount: ammount
        }
    }
}

fn main() {
    #![feature(step_by)]
    for i in (31..-1).step_down(-1) {
        println!("{}", i);
    }
}