https://github.com/anoma/smooth-operator
Rust checked arithmetic without the visual clutter
https://github.com/anoma/smooth-operator
checked-arithmetic procedural-macro rust syntax
Last synced: 11 months ago
JSON representation
Rust checked arithmetic without the visual clutter
- Host: GitHub
- URL: https://github.com/anoma/smooth-operator
- Owner: anoma
- License: mit
- Created: 2024-04-26T16:24:01.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-16T11:20:21.000Z (over 1 year ago)
- Last Synced: 2025-07-02T02:17:53.839Z (11 months ago)
- Topics: checked-arithmetic, procedural-macro, rust, syntax
- Language: Rust
- Homepage:
- Size: 39.1 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# smooth-operator
Procedural macro that transforms regular infix arithmetic expressions into
checked arithmetic expressions.
## Example
The following invocation of `checked!()`:
```rs
fn the_answer() -> Result {
let answer = checked!(410 / 10 + 1)?;
Ok(answer)
}
```
Results in this output:
```rs
fn the_answer() -> Result {
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)
}
```