Module assert
Runtime assertion and panic functions.
Both panic and assert accept any type that implements the ToString
trait — its .to_string().to_cstr() result (a NUL-terminated
ArrayList(u8)) is handed to the built-in __yo_panic through its data
pointer. The process is terminating via abort; the buffer need not be
freed.
Usage: assert(x > i32(0), "x must be positive"); assert(opt.is_some(), "Expected Some"); panic("unexpected condition");
Stability
stable in shape, with one caveat worth stating before the freeze.
panic, assert, assert_eq, assert_ne and assert_approx have the
names and the meanings Rust gives them, they accept any ToString, and no
additions are planned. The caveat is that these are RUNTIME assertions
only: there is no debug_assert that compiles out, so every assert in a
release build pays for its condition and its message. Adding one later is
additive; changing these five is not expected.
Do not reach for comptime_assert as the compile-time counterpart without
reading its own note — it fires only where the condition folds to a
concrete false.
Functions
Terminate the program with a message.
msg must implement ToString.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
msg | T |
Returns: unit
Runtime assertion. If flag is false, call panic(msg).
msg must implement ToString.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
flag | bool | |
msg | T | default: "Assertion failed." |
Returns: unit
Assert two values are equal; on failure the panic PRINTS BOTH SIDES —
the diff-printing upgrade over assert(a == b, "..."), whose message
can't show what the values actually were.
assert_eq(got, i32(42));
assert_eq(name, yo, "config name round-trip");
Type Parameters
| Name | Type | Notes |
|---|---|---|
A | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
left | A | |
right | A | |
msg | str | default: "" |
Returns: unit
Assert two values are NOT equal; prints the (shared) value on failure.
Type Parameters
| Name | Type | Notes |
|---|---|---|
A | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
left | A | |
right | A | |
msg | str | default: "" |
Returns: unit
Assert two floats are within epsilon of each other (absolute
difference). The default epsilon (1e-9) suits values near 1.0; pass an
explicit epsilon for very large or very small magnitudes.
Parameters
| Name | Type | Notes |
|---|---|---|
left | f64 | |
right | f64 | |
epsilon | f64 | default: f64(0.000000001) |
msg | str | default: "" |
Returns: unit