module
AssertsAsserts provides various compile-time and runtime asserts and similar facilities, some of which are wrappers for the standard C asserts, for enforcing invariants, run-time constraints/contracts, and compile-time requirements
Example
// i must be less than 10 void func(i32 i) { // enforce that i is less than 10 cnx_assert(i < 10, "func called with i >= 10 (i must be less than 10"); // use i somehow... } void* my_alloc(usize size) { // allocation code... if(allocation_failed) { // runtime abort with error message cnx_panic("my_alloc failed to allocate memory!"); } } #ifndef THING // THING must be less than 20 #define THING 12 #endif // enforce THING < 20 at compile-time cnx_static_assert(THING < 20, "THING defined as >= 20 (THING must be less than 20)"); #ifndef THING // THING must be a usize #define THING static_cast(usize)(12) #endif // enforce THING is a usize at compile-time cnx_type_assert_v(usize, THING);
Defines
- #define cnx_panic(error_message)
- Invokes a panic with the given error message.
- #define cnx_assert(condition, error_message)
- Asserts that the given condition is
true
. - #define cnx_static_assert(condition, error_message)
- Asserts that the given compile-time condition is
true
. - #define cnx_type_assert(type1, type2)
- Asserts that the two types are equivalent at compile-time.
- #define cnx_type_assert_v(type, value)
- Asserts that the type
type
and the type ofvalue
are equivalent at compile-time. - #define cnx_type_assert_vs(value1, value2)
- Asserts that the types of the two given values are equivalent at compile-time.
Define documentation
#define cnx_panic(error_message)
#include <include/Cnx/Assert.h>
Invokes a panic with the given error message.
Parameters | |
---|---|
error_message | - The error message to print to stderr before aborting. |
A panic is an immediate abort with error message. Panics should only be used in extreme circumstances of absolute program failure (for example, when an invariant is irrecoverably broken or on OOM).
#define cnx_assert(condition,
error_message)
#include <include/Cnx/Assert.h>
Asserts that the given condition is true
.
Parameters | |
---|---|
condition | - The condition that must be true |
error_message | - The associated error message if condition is false |
If condition
is false, this will print the file and line at which the error occurred, along with the given error message, to stderr
, then abort.
#define cnx_static_assert(condition,
error_message)
#include <include/Cnx/Assert.h>
Asserts that the given compile-time condition is true
.
Parameters | |
---|---|
condition | - The condition that must be true at compile time |
error_message | - The associated error message if condition is false |
If condition
is false
, this will print the given error message and abort compilation.
#define cnx_type_assert(type1,
type2)
#include <include/Cnx/Assert.h>
Asserts that the two types are equivalent at compile-time.
Parameters | |
---|---|
type1 | - The first type to compare |
type2 | - The second type to compare |
If type1
and type2
are not strictly equivalent, this will abort compilation with an error message.
#define cnx_type_assert_v(type,
value)
#include <include/Cnx/Assert.h>
Asserts that the type type
and the type of value
are equivalent at compile-time.
Parameters | |
---|---|
type | - The type to compare |
value | - The value to compare the type of |
If type
and the type of value
are not strictly equivalent, this will abort compilation with an error message.
#define cnx_type_assert_vs(value1,
value2)
#include <include/Cnx/Assert.h>
Asserts that the types of the two given values are equivalent at compile-time.
Parameters | |
---|---|
value1 | - The first value to compare the type of |
value2 | - The second value to compare the type of |
If the type of value1
and the type of value2
are not strictly equivalent, this will abort compilation with an error message.