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

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

#2 in #checked-arithmetic

Download history 187/week @ 2025-12-21 255/week @ 2025-12-28 274/week @ 2026-01-04 177/week @ 2026-01-11 295/week @ 2026-01-18 179/week @ 2026-01-25 268/week @ 2026-02-01 199/week @ 2026-02-08 277/week @ 2026-02-15 381/week @ 2026-02-22 343/week @ 2026-03-01 258/week @ 2026-03-08 204/week @ 2026-03-15 166/week @ 2026-03-22 352/week @ 2026-03-29 321/week @ 2026-04-05

1,058 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

~105–460KB
~11K SLoC