#checked-arithmetic #overflow #no-panic #checked #no-alloc

no-std smooth-operator

Procedural macro that transforms regular infix arithmetic expressions into checked arithmetic expressions

1 unstable release

0.7.2 Dec 13, 2024

#387 in Math

Download history 285/week @ 2024-12-12 73/week @ 2024-12-19 106/week @ 2024-12-26 129/week @ 2025-01-02 204/week @ 2025-01-09 274/week @ 2025-01-16 339/week @ 2025-01-23 598/week @ 2025-01-30 1060/week @ 2025-02-06 1770/week @ 2025-02-13 1614/week @ 2025-02-20 1370/week @ 2025-02-27 2206/week @ 2025-03-06 990/week @ 2025-03-13 854/week @ 2025-03-20

5,706 downloads per month
Used in 44 crates (16 directly)

MIT license

7KB
77 lines

smooth-operator

Procedural macro that transforms regular infix arithmetic expressions into checked arithmetic expressions.

Example

The following invocation of checked!():

fn the_answer() -> Result<i32, Error> {
    let answer = checked!(410 / 10 + 1)?;
    Ok(answer)
}

Results in this output:

fn the_answer() -> Result<i32, Error> {
    let answer = (|| -> ::core::result::Result<_, crate::Error> {
        type Err = crate::Error;
        const ORIGINAL_EXPR: &'static str = "410 / 10 + 1";
        Ok(
            #[allow(clippy::needless_question_mark)]
            #[allow(unused_parens)]
            {
                410.checked_div(10)
                    .ok_or(Err {
                        expr: ORIGINAL_EXPR,
                        __op_ix: 5usize,
                        __op_len: 1usize,
                    })?
                    .checked_add(1)
                    .ok_or(Err {
                        expr: ORIGINAL_EXPR,
                        __op_ix: 10usize,
                        __op_len: 1usize,
                    })?
            },
        )
    })()?;
    Ok(answer)
}

Dependencies

~200–630KB
~15K SLoC