smooth-operator-impl

Crates.iosmooth-operator-impl
lib.rssmooth-operator-impl
version0.7.2
created_at2024-12-13 14:17:00.072407+00
updated_at2024-12-13 14:17:00.072407+00
descriptionProcedural macro that transforms regular infix arithmetic expressions into checked arithmetic expressions.
homepage
repositoryhttps://github.com/anoma/smooth-operator
max_upload_size
id1482226
size13,522
Heliax (github:anoma:heliax)

documentation

README

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)
}
Commit count: 48

cargo fmt