Crate bool_const

Source
Expand description

The boolean constant library.

The library benefits the use of generic const boolean expressions as type bounds.

This library can be compiled with the latest stable Rust compiler, but it is useless unless the generic_const_exprs feature is enabled, which requires nightly Rust toolchains.

§Example

#![feature(generic_const_exprs)]

use bool_const::*;

struct MyEvenLengthArray<T, const N: usize>
where
    BoolConst<{ N.is_multiple_of(2) }>: TrueConst,
{
    inner: [T; N],
}

§Handling Compilation Errors

If there is a type bound error on TrueConst, the Rust compiler may throw the “unconstrained generic constant” error, and the message may look like this:

required by a bound in `MyEvenLengthArray`
try adding a `where` bound: ` where [(); { N.is_multiple_of(2) } as usize]:`

However, you should not simply apply that suggested code, because it is not allowed by the type checker (since MyEvenLengthArray is constrained by BoolConst<{ ... }>: TrueConst). Instead, you should write:

where BoolConst<{ N.is_multiple_of(2) }>: TrueConst

Structs§

BoolConst
The BoolConst type.

Traits§

TrueConst
The TrueConst trait.