| Crates.io | bool_const |
| lib.rs | bool_const |
| version | 1.0.0 |
| created_at | 2025-07-06 15:29:10.587495+00 |
| updated_at | 2025-07-06 15:29:10.587495+00 |
| description | A crate for generic const boolean expressions. |
| homepage | |
| repository | https://github.com/GreenYun/bool_const |
| max_upload_size | |
| id | 1740197 |
| size | 5,515 |
This crate works with no_std.
This crate can be compiled with the latest stable Rust toolchain. However, it is
useless without the unstable feature generic_const_exprs.
A simple example is listed below:
#![feature(generic_const_exprs)]
use bool_const::*;
struct MyEvenLengthArray<T, const N: usize>
where
BoolConst<{ N % 2 == 0 }>: TrueConst,
{
inner: [T; N],
}
bool_const provides simpler interfaces compared to the code that the Rust
compiler will suggest:
struct MyEvenLengthArray<T, const N: usize>
where
[(); { N % 2 == 0 } as usize]:,
{
inner: [T; N];
}
The Rust compiler rejects overly complex generic const expression, so you should
write a const fn to satisfy it:
#![feature(generic_const_exprs)]
use bool_const::*;
#[allow(dead_code)]
struct TheLeapYearType<const N: i64>
where
BoolConst<{ is_leap_year(N) }>: TrueConst;
const fn is_leap_year(year: i64) -> bool {
...
}
More examples can be found in [examples] directory.
MIT