-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow error type conversions in ensure! and ensure_eq! #474
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
macro_rules! ensure { | ||
($cond:expr, $e:expr) => { | ||
if !($cond) { | ||
return Err($e); | ||
return Err(std::convert::From::from($e)); | ||
} | ||
}; | ||
} | ||
|
@@ -43,9 +43,7 @@ macro_rules! fail_if { | |
#[macro_export] | ||
macro_rules! ensure_eq { | ||
($a:expr, $b:expr, $e:expr) => { | ||
if $a != $b { | ||
return Err($e); | ||
} | ||
ensure!($a == $b, $e); | ||
}; | ||
} | ||
|
||
|
@@ -55,6 +53,19 @@ mod test { | |
|
||
#[test] | ||
fn ensure_works() { | ||
fn check(a: usize, b: usize) -> Result<(), StdError> { | ||
ensure!(a == b, StdError::generic_err("foobar")); | ||
Ok(()) | ||
} | ||
|
||
let err = check(5, 6).unwrap_err(); | ||
assert!(matches!(err, StdError::GenericErr { .. })); | ||
|
||
check(5, 5).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn ensure_can_infer_error_type() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd love to understand why this works. How does Rust infer the error type in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woah, this compiles??? I guess it uses the |
||
let check = |a, b| { | ||
ensure!(a == b, StdError::generic_err("foobar")); | ||
Ok(()) | ||
|
@@ -66,6 +77,28 @@ mod test { | |
check(5, 5).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn ensure_can_convert_into() { | ||
#[derive(Debug)] | ||
struct ContractError; | ||
|
||
impl From<StdError> for ContractError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice minimal case 👍 |
||
fn from(_original: StdError) -> Self { | ||
ContractError | ||
} | ||
} | ||
|
||
fn check(a: usize, b: usize) -> Result<(), ContractError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, here is the real test of the compiler's intelligence: If you remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An But yes, if you turn it into a closure and remove the return type, compiler can infer it. |
||
ensure!(a == b, StdError::generic_err("foobar")); | ||
Ok(()) | ||
} | ||
|
||
let err = check(5, 6).unwrap_err(); | ||
assert!(matches!(err, ContractError)); | ||
|
||
check(5, 5).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn fail_if_works() { | ||
let check = |a, b| { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better reuse of code 👍