Skip to content

Lut99/literal-arithmetic-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

literal-arithmetic-rs

Implements procedural macro for doing math on integer literals for use in Rust macros.

Installation

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" }

Usage

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 the 2 literal 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.
  • Binary operators
    • Arithmetic operators (+, -, *, / or %); and
    • Bitwise operators (&, |, ^, >> or <<).
  • Unary operators
    • -
  • Parenthesis
  • Casts (as long as its to other integer types)

Some examples can be found in the procedural macro's documentation.

Contributing

Any ideas, suggestions and fixes are welcome! Feel free to raise an issue or create a pull request.

License

This project is licensed under the Apache 2.0 license. See LICENSE for more information.

About

Implements procedural macro for doing math on integer literals for use in Rust macros.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages