check

Crates.iocheck
lib.rscheck
version1.0.0
sourcesrc
created_at2017-05-07 20:03:17.642143
updated_at2022-03-17 17:13:00.801617
descriptionConvenience assert!-like macros which return instead of panicking
homepage
repositoryhttps://codeberg.org/come_744/check-rs
max_upload_size
id13625
size7,981
Côme (numero-744)

documentation

README

Check

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);
}
Commit count: 0

cargo fmt