Crates.io | wrapping_proc_macros |
lib.rs | wrapping_proc_macros |
version | 1.0.0 |
source | src |
created_at | 2022-06-30 15:40:57.515014 |
updated_at | 2022-06-30 15:40:57.515014 |
description | A proc macro for cleaner wrapping arithmetic. |
homepage | |
repository | https://github.com/Maneren/wrapping_macros |
max_upload_size | |
id | 616468 |
size | 9,932 |
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()
.Add this to your Cargo.toml
:
wrapping_macros = "*"
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);
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