const-arithmetic

Crates.ioconst-arithmetic
lib.rsconst-arithmetic
version1.0.4
sourcesrc
created_at2023-06-01 01:55:31.589861
updated_at2023-06-14 06:36:58.387709
descriptionExploits Rust's type generic system to perform integer arithmetics at compile time.
homepagehttps://github.com/darinchau/const-arithmetic
repositoryhttps://github.com/darinchau/const-arithmetic
max_upload_size
id879378
size389,466
Darin Chau (darinchau)

documentation

README

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.

Commit count: 57

cargo fmt