This isn't quite right, but it did give me the confidence to write the macro I do want.
As I mentioned above, ?
is brilliant because it will either return the unwrapped Ok()
value for me to use or transfer control out of the function. I want something I can use inside the outer function (that forms the C API) as I translate from the Rust API, so I don't want to map final error codes, but have something that I can repeatedly use to extract "good" results or to bail with if things go wrong.
The C-like thing to do is (not always but commonly) to always return some Status
enum and passing pointers for output. So I don't really want the macro/mapper to map anything to Status::Success
; that's for the outer function to do when it thinks things have gone well.
This does what I want, I think.
The key thing is my modification of your wrapper, and the From
function.
macro_rules! unwrap_or_return {
($expr:expr) => {
match $expr {
Ok(v) => v,
Err(e) => {
return e.into();
}
}
};
}