Crates.io | rassert-rs |
lib.rs | rassert-rs |
version | 3.0.0 |
source | src |
created_at | 2022-03-30 13:50:47.657113 |
updated_at | 2022-08-27 18:13:15.786105 |
description | Simple macro for expressing Result-returning assertions. |
homepage | https://github.com/mscofield0/rassert |
repository | https://github.com/mscofield0/rassert |
max_upload_size | |
id | 559194 |
size | 5,669 |
Simple macro for expressing Result-returning assertions and notifying, hard-error assertions (useful for functions where you can't propagate an error upstream so you want to log it).
use rassert::{rassert, rassert_notify};
enum MyError {
NotAnswerToLife,
}
struct SomeOutput;
pub fn foo(input: usize) -> Result<SomeOutput, MyError> {
rassert!(input == 42, MyError::NotAnswerToLife);
let output = ...;
Ok(output)
}
pub fn bar(input: usize) {
rassert_notify!(1 != 1, error!("Well, that's not true."));
println!("Hi everyone"); // Never reached since the above rassert_notify fails and returns
}
Because the alternative is rather ugly and does not obviously express that the expression is a precondition.
enum MyError {
NotAnswerToLife,
}
struct SomeOutput;
pub fn foo(input: usize) -> Result<SomeOutput, MyError> {
if input != 42 {
return Err(MyError::NotAnswerToLife);
}
let output = ...;
Ok(output)
}
I found myself just copy-pasting the same rassert macro over and over in my projects, so might as well put it on Cargo.