custom_try

Attribute Macro custom_try 

Source
#[custom_try]
Expand description

Replace expr? occurrences in the decorated item with r#try!( expr ).

The r#try! default path can be overridden by giving it as an argument to the attribute macro:

§Examples

§1. For FFI

  • use ::custom_try::custom_try;
    
    #[repr(transparent)]
    pub struct FfiResult {
        pub status_code: ::std::os::raw::c_int,
    }
    
    impl FfiResult {
        pub const OK: Self = Self { status_code: 0 };
        pub const ERR: Self = Self { status_code: -1 };
    }
    
    macro_rules! unwrap_option {( $option:expr $(,)? ) => (
        match $option {
            Some(thing) => thing,
            None => return $crate::FfiResult::ERR,
        }
    )}
    
    #[custom_try(unwrap_option!)]
    extern "C" fn ffi_function() -> FfiResult {
        let x = the_answer_to_life_the_universe_and_everything()?;
        println!("{}", x);
        FfiResult::OK
    }
    
    /// If you only have one case of `?` semantics, you can default to that one
    /// using the default `r#try!` macro name.
    use unwrap_option as r#try;
    
    #[custom_try]
    extern "C" fn ffi_function2() -> FfiResult {
        let x = the_answer_to_life_the_universe_and_everything()?;
        println!("{}", x);
        FfiResult::OK
    }