crcxx/internals/
cg_assert.rs

1// Extracted from `heapless` crate.
2// https://github.com/japaric/heapless/blob/main/src/sealed.rs
3
4#![allow(clippy::no_effect)]
5#![allow(clippy::erasing_op)]
6
7/// Const functions that can be used to assert the const generics.
8
9#[allow(dead_code)]
10#[allow(path_statements)]
11pub const fn assert_eq<const L: usize, const R: usize>() {
12    Assert::<L, R>::EQ;
13}
14
15#[allow(dead_code)]
16#[allow(path_statements)]
17pub const fn assert_ne<const L: usize, const R: usize>() {
18    Assert::<L, R>::NOT_EQ;
19}
20
21#[allow(dead_code)]
22#[allow(path_statements)]
23pub const fn assert_lt<const N: usize, const MAX: usize>() {
24    Assert::<N, MAX>::LESS;
25}
26
27#[allow(dead_code)]
28#[allow(path_statements)]
29pub const fn assert_gt<const N: usize, const MIN: usize>() {
30    Assert::<N, MIN>::GREATER;
31}
32
33#[allow(dead_code)]
34#[allow(path_statements)]
35pub const fn assert_lt_eq<const N: usize, const MAX: usize>() {
36    Assert::<N, MAX>::LESS_EQ;
37}
38
39#[allow(dead_code)]
40#[allow(path_statements)]
41pub const fn assert_gt_eq<const N: usize, const MIN: usize>() {
42    Assert::<N, MIN>::GREATER_EQ;
43}
44
45#[allow(dead_code)]
46#[allow(path_statements)]
47pub const fn assert_power_of_two<const N: usize>() {
48    Assert::<N, 0>::GREATER;
49    Assert::<N, 0>::POWER_OF_TWO;
50}
51
52#[allow(dead_code)]
53/// Const assert hack
54pub struct Assert<const L: usize, const R: usize>;
55
56#[allow(dead_code)]
57impl<const L: usize, const R: usize> Assert<L, R> {
58    /// Const assert hack
59    pub const EQ: usize = (R - L) + (L - R);
60    /// Const assert hack
61    pub const GREATER: usize = L - R - 1;
62    /// Const assert hack
63    pub const GREATER_EQ: usize = L - R;
64    /// Const assert hack
65    pub const LESS: usize = R - L - 1;
66    /// Const assert hack
67    pub const LESS_EQ: usize = R - L;
68    /// Const assert hack
69    pub const NOT_EQ: isize = 0 / (R as isize - L as isize);
70    /// Const assert hack
71    pub const POWER_OF_TWO: usize = 0 - (L & (L - 1));
72}