Crates.io | check |
lib.rs | check |
version | 1.0.0 |
source | src |
created_at | 2017-05-07 20:03:17.642143 |
updated_at | 2022-03-17 17:13:00.801617 |
description | Convenience assert!-like macros which return instead of panicking |
homepage | |
repository | https://codeberg.org/come_744/check-rs |
max_upload_size | |
id | 13625 |
size | 7,981 |
Convenience assert!
-like macros which immediately return None
or Err(...)
instead of panicking.
In a function returning an Option<T>
, invoke the macro with just enough
parameters to get a condition to check.
check!(a < n);
check_eq!(a, b);
This will expand to:
if !(a < n) {
return None;
}
if a != b {
return None;
}
In a function returning a Result<T, E>
, invoke the macro with an extra
argument, which is the error to return if the check fails (and must have type
E
), just like you can add arguments to choose a panic message with assert!
.
check!(a < n, MyError::TooBig);
check_eq!(a, b, MyError::NotEqual);
This will expand to:
if !(a < n) {
return Err(MyError::TooBig);
}
if a != b {
return Err(MyError::NotEqual);
}