bool_const 1.0.0

A crate for generic const boolean expressions.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 5.01 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 740.47 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • GreenYun/bool_const
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • GreenYun

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