step

Crates.iostep
lib.rsstep
version0.2.0
sourcesrc
created_at2017-02-12 11:16:27.294754
updated_at2017-02-16 15:11:14.857376
descriptionA trait that allows for stepping numeric types.
homepage
repositoryhttps://github.com/ryanq/step
max_upload_size
id8485
size4,688
Ryan Quattlebaum (ryanq)

documentation

https://docs.rs/step/0.1.0/step/

README

step

Build Status Crate on crates.io

step is a crate that provides the trait Step, which allows for unit steps and arbitrary steps on numeric types.

Documentation can be found on docs.rs.

Using step

Add the crate to the dependencies section of Cargo.toml:

[dependencies]
step = { git = "https://github.com/ryanq/step" }

Then import the crate and type in your source:

extern crate step;

use step::Step;

Then you can use the functions for incrementing and decrementing numbers or implement it on your own types:

let number = 42;

assert_eq!(number.next(), 43);
assert_eq!(number.next_by(&3), 45);
assert_eq!(number.prev(), 41);
assert_eq!(number.prev_by(&3), 39);
struct Foo {
    bar: i32,
}

impl Step for Foo {
    fn next(&self) -> Self { Foo { bar: self.bar + 1 } }
    fn next_by(&self, by: &Self) -> Self { Foo { bar: self.bar + *by } }
    fn prev(&self) -> Self { Foo { bar: self.bar - 1 } }
    fn prev_by(&self, by: &Self) -> Self { Foo { bar: self.bar - *by } }
}
Commit count: 0

cargo fmt