1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2025 GreenYun Organization
// SPDX-License-Identifier: MIT
//! 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:
//!
//! ```text
//! 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:
//!
//! ```ignore
//! # struct AnotherType<T, const N: usize>
//! where BoolConst<{ N.is_multiple_of(2) }>: TrueConst
//! # {
//! # inner: [T; N],
//! # }
//! ```
/// The `TrueConst` trait.
///
/// *[See crate-level documentation.](crate).*
/// The `BoolConst` type.
///
/// *[See crate-level documentation.](crate).*
;