#整数算术 #const-generics #算术 #const #编译时 #整数 #泛型

const-arithmetic

利用 Rust 的类型泛型系统在编译时执行整数算术

4 个稳定版本

1.0.4 2023年6月14日
1.0.3 2023年6月2日
1.0.2 2023年6月1日

算法中排名 767

每月下载量 36

MIT 许可证

190KB
2K SLoC

const_arithmetic

Crates.io Documentation

const_arithmetic 是一个 crate,致力于利用 Rust 强大的编译器和类型泛型系统在编译时执行整数算术。这可以在稳定版上工作,以绕过(某种程度上)#[generic_const_exprs]

使用方法

const_arithmetic crate 添加到你的 Cargo.toml 文件中的依赖项

[dependencies]
const_arithmetic = "1.0.3"

导入 const_arithmetic crate 中的特性和类型

use const_arithmetic::*;

示例

假设我们想验证 6 x 4 = 24。以下是代码片段,如果上述语句为真,则不执行任何操作;如果上述语句为假,则无法编译。

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);

相应地,这无法编译

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

文档

请参阅API 文档以获取详细信息和其他示例。

贡献

如果您想做出任何更改,请打开一个 PR。欢迎贡献。

许可

此 crate 在MIT 许可证的条款下分发。

依赖项

~1.5MB
~36K SLoC