Macro abi_stable::rtry
source · macro_rules! rtry { ($expr:expr) => { ... }; }
Expand description
Equivalent to ?
for RResult
.
Accepts both Result
and RResult
arguments.
§Example
Defining an extern function that returns a result.
use abi_stable::{
std_types::{RResult, ROk, RBoxError, RStr, Tuple3},
rtry,
sabi_extern_fn,
};
#[sabi_extern_fn]
fn parse_tuple(s: RStr<'_>) -> RResult<Tuple3<u32, u32, u32>, RBoxError> {
let mut iter = s.split(',').map(|x| x.trim());
ROk(Tuple3(
rtry!(iter.next().unwrap_or("").parse().map_err(RBoxError::new)),
rtry!(iter.next().unwrap_or("").parse().map_err(RBoxError::new)),
rtry!(iter.next().unwrap_or("").parse().map_err(RBoxError::new)),
))
}
assert_eq!(parse_tuple("3, 5, 8".into()).unwrap(), Tuple3(3, 5, 8));
parse_tuple("".into()).unwrap_err();