Implements procedural macro for doing math on integer literals for use in Rust macros.
To use this crate in one of your projects, simply add it to your Cargo.toml file:
[dependencies]
literal-arithmetic = { git = "https://github.com/Lut99/literal-arithmetic-rs" }You can also depend on a specific tag if you specify it so:
[dependencies]
literal-arithmetic = { git = "https://github.com/Lut99/literal-arithmetic-rs", tag = "v0.1.0" }The crate revolves around the calc!(...)-macro, which accepts (simple) integer expressions and expands to a literal with the target value.
For example:
use literal_arithmetic::calc;
assert_eq!(calc!(1 + 1), 2);Tip: use the
cargo-expand-subcommand to verify the2literal is being produced instead of the sum.
Only basic operations are supported. Specifically:
- Integer literals
- This includes both plain literals (
1) or typed literals (1usize). The resulting literal will carry over the type according to the following rules:- Any sum involving a plain and a typed literal will become typed;
- If there are two typed, then the result will be the larger of the two and signed if one of the two is signed.
- This includes both plain literals (
- Binary operators
- Arithmetic operators (
+,-,*,/or%); and - Bitwise operators (
&,|,^,>>or<<).
- Arithmetic operators (
- Unary operators
-
- Parenthesis
- Casts (as long as its to other integer types)
Some examples can be found in the procedural macro's documentation.
Any ideas, suggestions and fixes are welcome! Feel free to raise an issue or create a pull request.
This project is licensed under the Apache 2.0 license. See LICENSE for more information.