Expand description
A set of macros similar to the standard library’s assert_*
macros, but return early instead of panicking.
§Example
use soft_assert::*;
fn not_twenty(x: i32) -> Option<i32> {
// This will return `Option::default()`, which is `None`
soft_assert_ne!(x, 20);
Some(x)
}
fn double_if_greater_than_5(x: i32) -> i32 {
// But here we don't want to return `i32::default()`,
// so we specify a return value.
soft_assert!(x > 5, x);
x * 2
}
fn main() {
assert!(not_twenty(10).is_some());
assert!(not_twenty(20).is_none());
let doubled = double_if_greater_than_5(13);
assert_eq!(doubled, 26);
let not_doubled = double_if_greater_than_5(2);
assert_eq!(not_doubled, 2);
}
This crate is #![no_std]
Macros§
- soft_
assert - Asserts a condition is true, returning otherwise.
- soft_
assert_ eq - Asserts two values are equal, returning otherwise.
- soft_
assert_ matches - Asserts a value matches a pattern, returning otherwise.
- soft_
assert_ ne - Asserts two values are not equal, returning otherwise.
- soft_
debug_ assert - If debug assertions are enabled, asserts a condition is true, returning otherwise.
- soft_
debug_ assert_ eq - If debug assertions are enabled, asserts two values are equal, returning otherwise.
- soft_
debug_ assert_ matches - Asserts a value matches a pattern, returning otherwise.
- soft_
debug_ assert_ ne - If debug assertions are enabled, asserts two values are not equal, returning otherwise.