Crates.io | const-arithmetic |
lib.rs | const-arithmetic |
version | 1.0.4 |
source | src |
created_at | 2023-06-01 01:55:31.589861 |
updated_at | 2023-06-14 06:36:58.387709 |
description | Exploits Rust's type generic system to perform integer arithmetics at compile time. |
homepage | https://github.com/darinchau/const-arithmetic |
repository | https://github.com/darinchau/const-arithmetic |
max_upload_size | |
id | 879378 |
size | 389,466 |
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]
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::*;
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
Please refer to the API documentation for detailed information and more examples.
Please open a PR if you want to change anything. Contributions are welcome.
This crate is distributed under the terms of the MIT License.