bool_const

Crates.iobool_const
lib.rsbool_const
version1.0.0
created_at2025-07-06 15:29:10.587495+00
updated_at2025-07-06 15:29:10.587495+00
descriptionA crate for generic const boolean expressions.
homepage
repositoryhttps://github.com/GreenYun/bool_const
max_upload_size
id1740197
size5,515
(GreenYun)

documentation

README

Rust Crate for Specifying Bounds With Boolean Expressions

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.

License

MIT

Commit count: 0

cargo fmt