#integer-arithmetic #const-generics #generics #compile-time #arithmetic #integer #const

const-arithmetic

Exploits Rust's type generic system to perform integer arithmetics at compile time

4 stable releases

1.0.4 Jun 14, 2023
1.0.3 Jun 2, 2023
1.0.2 Jun 1, 2023

#838 in Algorithms

36 downloads per month

MIT license

190KB
2K SLoC

const_arithmetic

Crates.io Documentation

const_arithmetic is a crate dedicated to exploiting Rust's powerful compiler and type generic system to perform integer arithmetics at compile time. This works on stable to circumvent (sort of) #[generic_const_exprs]

Usage

Add the const_arithmetic crate as a dependency in your Cargo.toml file:

[dependencies]
const_arithmetic = "1.0.3"

Import the traits and types from the const_arithmetic crate:

use const_arithmetic::*;

Example:

Let's say we want to verify 6 x 4 = 24. Here is the code snippet that does nothing if the above statement is true and fails to compile if the above statement is false.

use const_arithmetic::*;
let a = parse_integer!(6);
let b = parse_integer!(4);
let c = parse_integer!(24);

fn verify_me<P, Q, R>(_p: P, _q: Q, _r: R) where
P: IsInteger,
Q: IsInteger,
R: IsInteger,
P: TypedMul<Q, Output = R>
{}

verify_me(a, b, c);

Accordingly, this fails to compile:

use const_arithmetic::*;
let a = parse_integer!(6);
let b = parse_integer!(5);
let c = parse_integer!(25);

fn verify_me<P, Q, R>(_p: P, _q: Q, _r: R) where
P: IsInteger,
Q: IsInteger,
R: IsInteger,
P: TypedMul<Q, Output = R>
{}

verify_me(a, b, c); // Compilation error

Documentation

Please refer to the API documentation for detailed information and more examples.

Contributing

Please open a PR if you want to change anything. Contributions are welcome.

License

This crate is distributed under the terms of the MIT License.

Dependencies

~1.5MB
~36K SLoC