How about a macro?
pub struct CallbackArgs{}
unsafe extern "C" fn c_handler(args: *mut CallbackArgs) -> MyStatus
{
macro_rules! my_try {
($expr:expr) => {
match $expr {
Some(x) => x,
None => return MyStatus::ERROR,
}
};
};
// ...
let value = my_try!(any_variable.option_my_enum);
// ...
MyStatus::OK
}
Playground
I am not sure if this is what you want, because in your first example you showed you intended to return either the enum or the error, but that is not what your second example is doing.
If you intend to something similar to your first example, you have to remember that you need to return something valid in C, like the following:
#[repr(C)]
struct CHandlerReturn {
status: MyStatus,
value: MaybeUninit<MyEnum>,
}
unsafe extern "C" fn c_handler(args: *mut CallbackArgs) -> CHandlerReturn {
macro_rules! my_try {
($expr:expr) => {
match $expr {
Some(x) => x,
None => {
return CHandlerReturn {
status: MyStatus::ERROR,
value: MaybeUninit::uninit(),
}
}
}
};
};
// ...
let value = my_try!(any_variable.option_my_enum);
// ...
CHandlerReturn {
status: MyStatus::OK,
value: MaybeUninit::new(value),
}
}
Playground