#ever #numbers #now #wrap #wanted #value #wraparounds

wrapnum

Ever wanted to make easy arbitrary number wraparounds? Now you can

6 releases

new 0.3.2 Dec 16, 2024
0.3.1 Dec 16, 2024
0.2.1 Dec 16, 2024
0.1.0 Dec 15, 2024

#4 in #now

Download history

147 downloads per month

GPL-3.0-only

12KB
296 lines

WrapNum

Have you ever found yourself needing a counter with an arbitrary wraparound limit and having to handroll some mediocre implementation? Well here you go. Just set the max value and forget about ever needing to wonder, "did I need to call my wrap function here?"

Installation

To add WrapNum to your project, just run:

cargo add wrapnum

And you're all good to go!

Usage

The main entrypoint for WrapNum is with the wrap! macro. There are multiple ways of using the macro, but the most common use-case is with one value: the max limit:

use wrapnum::wrap;

fn main() {
    let mut my_num = wrap!(5);
    assert_eq!(my_num + 6, 1)
}

It also works with subtraction 😜 (as it should, idk why I wrote that).

You can also pass in two values to specify a min and max, or a range:

use wrapnum::wrap;

fn main() {
    let my_num = wrap!(5..=50); // Same as `wrap!(5, 49)`.
}

Refer to the docs for more information on how to create a WrapNum.

Other than that, there isn't much else. It should behave exactly like an integer type should, and if not, open an issue and we can get it fixed.

Dependencies

~465KB