#iterator #range #step

nightly staticstep

Provides truly zero-cost alternatives to Iterator::step_by for both incrementing and decrementing any type that satisfies RangeBounds<T: Copy + Default + Step>

7 releases

0.4.2 Jan 5, 2022
0.4.1 Jan 1, 2022
0.4.0 Dec 31, 2021
0.3.0 Dec 30, 2021
0.1.0 Mar 31, 2021

#971 in Algorithms

Download history 4/week @ 2024-02-25 78/week @ 2024-03-31

78 downloads per month

MIT/Apache

16KB
189 lines

Latest Version Rustc Version nightly

Build status docs.rs

Provides truly zero-cost alternatives to Iterator::step_by for both incrementing and decrementing any type that satisfies RangeBounds<T: Copy + Default + Step>.

The assembly code generated for the two trait methods this crate implements, inc_by and dec_by, should in the overwhelming majority of cases be nearly or completely identical to the assembly code that would be generated for an incrementing "step"-based while loop or decrementing "step"-based while loop, respectively. If you come across a scenario where it is not, please feel free to open an issue about it.

Minimum supported Rust version: this is a nightly-only crate at the moment due to the use of the Step trait, which has not yet been stabilized.

#![no_std] compatibility: this crate is fully #![no_std] compatible by default.

A basic usage example:

use staticstep::*;

// Apart from aiming to provide a properly-optimized Rust equivalent to the sort of C-style for-loop
// that ends in `i += number` or `i -= number` as opposed to `i++` or `i--`, this crate also aims to
// (and does) support backwards ranges in a meaningful way that's logically equivalent to how
// forwards ranges are generally dealt with in Rust.

fn main() {
  // Exclusive, so 48 is the last number printed.
  for i in (0..64).inc_by::<16>() {
    print!("{} ", i);
  }
  println!("");

  // Inclusive, so 64 is the last number printed.
  for i in (0..=64).inc_by::<16>() {
    print!("{} ", i);
  }
  println!("");

  // Exclusive, so 16 is the last number printed.
  for i in (64..0).dec_by::<16>() {
    print!("{} ", i);
  }
  println!("");

  // Inclusive, so 0 is the last number printed.
  for i in (64..=0).dec_by::<16>() {
    print!("{} ", i);
  }

  // Note that `inc_by` will always immediately return `None` if given a reverse range, while
  // `dec_by` will always immediately return `None` if given a "normal" forwards range.
}

License:

Licensed under either the MIT license or version 2.0 of the Apache License. Your choice as to which! Any source code contributions will be dual-licensed in the same fashion.

No runtime deps