#proc-macro #wrapping #arithmetic #cleaner

yanked wrapping_proc_macros

A proc macro for cleaner wrapping arithmetic

Uses old Rust 2015

1.0.0 Jun 30, 2022

#25 in #cleaner

MIT license

7KB
89 lines

wrapping_macros

A macro for scoped wrapping arithmetic.

Any code within a wrapping! { .. } block will be transformed as follows:

  • a + b becomes a.wrapping_add(b). Similarly for -, *, /, %, <<, >>.
  • a += b becomes a = a.wrapping_add(b). Similarly for -=, *=, /=, %=, <<=, >>=.
  • -a becomes a.wrapping_neg().

Cargo

Add this to your Cargo.toml:

wrapping_macros = "*"

Examples

let a = 128u8;
let b = wrapping! { a * 2 };

assert_eq!(b, 0);
let mut a = 250u8;
let mut b = 4u8;
let mut c = 100u8;
wrapping! {
    a += 10;
    b -= 10;
    c *= 10;
}

assert_eq!(a, 4);
assert_eq!(b, 250);
assert_eq!(c, 232);
use wrapping_macros::wrapping;

fn main() {
    let mut sum = 0u8;
    wrapping! {
        for x in 0u8..50 {
            sum += x;
        }
    }
}
assert_eq!(sum, 201);

Caveat

You cannot nest another macro invocation within a wrapping! block. For example, this will not work:

let x = 128u8;
wrapping! {
    println!("The answer is {}", x + 128) // Error
}

Instead, move the macro call out of the wrapping! block:

let x = 128u8;
println!("The answer is {}", wrapping! { x + 128 })

Inspired by wrapping_macros crate by lfairy

Dependencies

~1.5MB
~35K SLoC