iceoryx2_bb_elementary/
static_assert.rs

1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13//! Static assertions in Rust.
14//!
15//! Useful for compile time assertions.
16//!
17//! # Example
18//!
19//! ```
20//! use iceoryx2_bb_elementary::static_assert::*;
21//!
22//! use core::mem::{align_of, size_of};
23//!
24//! static_assert_eq::<{ size_of::<u64>() }, 8>();
25//! static_assert_ge::<{ size_of::<u64>() }, { size_of::<u32>() }>();
26//! static_assert_gt::<{ size_of::<u64>() }, { size_of::<u32>() }>();
27//! static_assert_le::<{ size_of::<u32>() }, { size_of::<u64>() }>();
28//! static_assert_lt::<{ size_of::<u32>() }, { size_of::<u64>() }>();
29//! ```
30
31/// A compile time assert to check for equal values
32///
33/// # Examples
34///
35/// This does compile!
36///
37/// ```
38/// use iceoryx2_bb_elementary::static_assert::*;
39///
40/// static_assert_eq::<1, 1>();
41///
42/// ```
43///
44/// This does not compile!
45///
46/// ```compile_fail
47/// use iceoryx2_bb_elementary::static_assert::*;
48///
49/// static_assert_eq::<1, 2>();
50/// static_assert_eq::<2, 1>();
51///
52/// ```
53pub const fn static_assert_eq<const L: usize, const R: usize>() {
54    let () = AssertEq::<L, R>::OK;
55}
56
57struct AssertEq<const L: usize, const R: usize>;
58
59impl<const L: usize, const R: usize> AssertEq<L, R> {
60    const OK: () = assert!(L == R, "L must be equal to R");
61}
62
63/// A compile time assert to check for greater than or equal values
64///
65/// # Examples
66///
67/// This does compile!
68///
69/// ```
70/// use iceoryx2_bb_elementary::static_assert::*;
71///
72/// static_assert_ge::<1, 1>();
73/// static_assert_ge::<2, 1>();
74///
75/// ```
76///
77/// This does not compile!
78///
79/// ```compile_fail
80/// use iceoryx2_bb_elementary::static_assert::*;
81///
82/// static_assert_ge::<1, 2>();
83///
84/// ```
85pub const fn static_assert_ge<const L: usize, const R: usize>() {
86    let () = AssertGe::<L, R>::OK;
87}
88
89struct AssertGe<const L: usize, const R: usize>;
90
91impl<const L: usize, const R: usize> AssertGe<L, R> {
92    const OK: () = assert!(L >= R, "L must be greater than or equal to R");
93}
94
95/// A compile time assert to check for greater than values
96///
97/// # Examples
98///
99/// This does compile!
100///
101/// ```
102/// use iceoryx2_bb_elementary::static_assert::*;
103///
104/// static_assert_gt::<2, 1>();
105///
106/// ```
107///
108/// This does not compile!
109///
110/// ```compile_fail
111/// use iceoryx2_bb_elementary::static_assert::*;
112///
113/// static_assert_gt::<1, 1>();
114/// static_assert_gt::<1, 2>();
115///
116/// ```
117pub const fn static_assert_gt<const L: usize, const R: usize>() {
118    let () = AssertGt::<L, R>::OK;
119}
120
121struct AssertGt<const L: usize, const R: usize>;
122
123impl<const L: usize, const R: usize> AssertGt<L, R> {
124    const OK: () = assert!(L > R, "L must be greater than R");
125}
126
127/// A compile time assert to check for less than or equal values
128///
129/// # Examples
130///
131/// This does compile!
132///
133/// ```
134/// use iceoryx2_bb_elementary::static_assert::*;
135///
136/// static_assert_le::<1, 1>();
137/// static_assert_le::<1, 2>();
138///
139/// ```
140///
141/// This does not compile!
142///
143/// ```compile_fail
144/// use iceoryx2_bb_elementary::static_assert::*;
145///
146/// static_assert_le::<2, 1>();
147///
148/// ```
149pub const fn static_assert_le<const L: usize, const R: usize>() {
150    let () = AssertLe::<L, R>::OK;
151}
152
153struct AssertLe<const L: usize, const R: usize>;
154
155impl<const L: usize, const R: usize> AssertLe<L, R> {
156    const OK: () = assert!(L <= R, "L must be less than or equal to R");
157}
158
159/// A compile time assert to check for less than values
160///
161/// # Examples
162///
163/// This does compile!
164///
165/// ```
166/// use iceoryx2_bb_elementary::static_assert::*;
167///
168/// static_assert_lt::<1, 2>();
169///
170/// ```
171///
172/// This does not compile!
173///
174/// ```compile_fail
175/// use iceoryx2_bb_elementary::static_assert::*;
176///
177/// static_assert_lt::<1, 1>();
178/// static_assert_lt::<2, 1>();
179///
180/// ```
181pub const fn static_assert_lt<const L: usize, const R: usize>() {
182    let () = AssertLt::<L, R>::OK;
183}
184
185struct AssertLt<const L: usize, const R: usize>;
186
187impl<const L: usize, const R: usize> AssertLt<L, R> {
188    const OK: () = assert!(L < R, "L must be less than R");
189}