#arithmetic #crypto #wrapping #proc-macro #operator #u32 #equivalents

macro wrapping_arithmetic

Proc macro #[wrappit] to rewrite operators into their wrapping equivalents

1 unstable release

0.1.0 Oct 17, 2019

#4 in #equivalents

Download history 1/week @ 2024-11-13 7/week @ 2024-11-20 15/week @ 2024-11-27 21/week @ 2024-12-04 50/week @ 2024-12-11 11/week @ 2024-12-18 5/week @ 2025-01-01 11/week @ 2025-01-08 36/week @ 2025-01-15 18/week @ 2025-01-22 14/week @ 2025-01-29 75/week @ 2025-02-05 36/week @ 2025-02-12 20/week @ 2025-02-19 29/week @ 2025-02-26

163 downloads per month
Used in rand_krull

Apache-2.0

7KB
95 lines

This crate provides a proc macro that rewrites arithemtic operators +,-,* into their wrapping equivalents wrapping_add, wrapping_sub, wrapping_mul as well as their assigning versions +=,-=,*=.

The following function for example

#[wrappit]
fn mix(a: u32, b: u32, c: [u32; 8]) -> u32 {
    let mut r = a + b;
    for u in c {
        r *= u;
    }
    r
}

is rewritten to

fn mix(a: u32, b: u32, c: [u32; 8]) -> u32 {
    let mut r = a.wrapping_add(b);
    for u in c {
        r = r.wrapping_mul(u);
    }
    r
}

Dependencies

~1.5MB
~39K SLoC